Skip to content

Commit

Permalink
Merge pull request #520 from csmith/master
Browse files Browse the repository at this point in the history
Use Multimap instead of MapList.
  • Loading branch information
greboid committed Jan 17, 2015
2 parents e14c117 + 459ef2b commit e2d6353
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 78 deletions.
8 changes: 5 additions & 3 deletions src/com/dmdirc/commandparser/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.ui.input.TabCompleter;
import com.dmdirc.ui.input.TabCompletionType;
import com.dmdirc.util.collections.MapList;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.util.HashMap;
import java.util.List;
Expand All @@ -52,7 +54,7 @@ public class CommandManager implements CommandController {
/** A list of commands that have been instantiated. */
private final Map<CommandInfo, Command> commands = new HashMap<>();
/** A list of command parsers that have been instantiated. */
private final MapList<CommandType, CommandParser> parsers = new MapList<>();
private final Multimap<CommandType, CommandParser> parsers = ArrayListMultimap.create();
/** The manager to use to iterate servers. */
private final ConnectionManager connectionManager;
/** Provider to use to retrieve the global window. */
Expand Down Expand Up @@ -220,7 +222,7 @@ public void loadCommands(final CommandParser parser,
parser.registerCommand(pair.getValue(), pair.getKey());
}

parsers.add(type, parser);
parsers.put(type, parser);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import com.dmdirc.parser.common.ChannelJoinRequest;
import com.dmdirc.ui.input.AdditionalTabTargets;
import com.dmdirc.ui.messages.Styliser;
import com.dmdirc.util.collections.MapList;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -64,7 +66,7 @@ public class JoinChannelCommand extends Command implements IntelligentCommand {
CommandType.TYPE_SERVER);
/** A map of channel name mentions. */
@GuardedBy("mentionsLock")
private final MapList<FrameContainer, String> mentions = new MapList<>();
private final Multimap<FrameContainer, String> mentions = ArrayListMultimap.create();
/** Lock to synchronise on when accessing mentions. */
private final Object mentionsLock = new Object();

Expand Down Expand Up @@ -117,7 +119,7 @@ public void handleClientLineAdded(final ClientLineAddedEvent event) {
synchronized (mentionsLock) {
for (int i = 1; i < parts.length; i += 2) {
// All of the odd parts of the array are channel names
mentions.add(event.getFrameContainer(), parts[i]);
mentions.put(event.getFrameContainer(), parts[i]);
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/com/dmdirc/config/ConfigBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import com.dmdirc.interfaces.config.ConfigChangeListener;
import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.util.collections.MapList;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Executable;
Expand All @@ -48,7 +50,7 @@
public class ConfigBinder {

/** A map of instances to created listeners. */
private final MapList<Object, ConfigChangeListener> listeners = new MapList<>();
private final Multimap<Object, ConfigChangeListener> listeners = ArrayListMultimap.create();
/** The default domain to use. */
private final Optional<String> defaultDomain;
/** The configuration manager to use to retrieve settings. */
Expand Down Expand Up @@ -219,7 +221,7 @@ private Class<?> getTargetClass(final AccessibleObject element) {
private void addListeners(final Object instance,
final Collection<ConfigChangeListener> newListeners) {
synchronized (listeners) {
listeners.add(instance, newListeners);
listeners.putAll(instance, newListeners);
}
}

Expand All @@ -230,8 +232,8 @@ private void addListeners(final Object instance,
*/
public void unbind(final Object instance) {
synchronized (listeners) {
listeners.safeGet(instance).forEach(manager::removeListener);
listeners.remove(instance);
listeners.get(instance).forEach(manager::removeListener);
listeners.removeAll(instance);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/com/dmdirc/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import com.dmdirc.interfaces.config.ConfigProviderListener;
import com.dmdirc.interfaces.config.ConfigProviderMigrator;
import com.dmdirc.util.ClientInfo;
import com.dmdirc.util.collections.MapList;
import com.dmdirc.util.validators.Validator;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -59,7 +61,7 @@ class ConfigManager implements ConfigChangeListener, ConfigProviderListener,
/** A list of sources for this config manager. */
private final List<ConfigProvider> sources = new ArrayList<>();
/** The listeners registered for this manager. */
private final MapList<String, ConfigChangeListener> listeners = new MapList<>();
private final Multimap<String, ConfigChangeListener> listeners = ArrayListMultimap.create();
/** The config binder to use for this manager. */
private final ConfigBinder binder;
/** The manager to use to fetch global state. */
Expand Down Expand Up @@ -433,7 +435,7 @@ public void addChangeListener(final String domain, final String key,
@Override
public void removeListener(final ConfigChangeListener listener) {
synchronized (listeners) {
listeners.removeFromAll(listener);
listeners.keys().forEach(k -> listeners.remove(k, listener));
}
}

Expand All @@ -446,7 +448,7 @@ public void removeListener(final ConfigChangeListener listener) {
private void addListener(final String key,
final ConfigChangeListener listener) {
synchronized (listeners) {
listeners.add(key, listener);
listeners.put(key, listener);
}
}

Expand Down
62 changes: 33 additions & 29 deletions src/com/dmdirc/config/IdentityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
import com.dmdirc.interfaces.config.IdentityFactory;
import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.util.ClientInfo;
import com.dmdirc.util.collections.MapList;
import com.dmdirc.util.collections.WeakMapList;
import com.dmdirc.util.io.ConfigFile;
import com.dmdirc.util.io.FileUtils;
import com.dmdirc.util.io.InvalidConfigFileException;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URISyntaxException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
Expand All @@ -49,9 +51,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -80,7 +82,7 @@ public class IdentityManager implements IdentityFactory, IdentityController {
* Standard identities are inserted with a <code>null</code> key, custom identities use their
* custom type as the key.
*/
private final MapList<String, ConfigProvider> identities = new MapList<>();
private final Multimap<String, ConfigProvider> identities = ArrayListMultimap.create();
/** Map of paths to corresponding config providers, to facilitate reloading. */
private final Map<Path, ConfigProvider> configProvidersByPath = new ConcurrentHashMap<>();
/** The event bus to post events to. */
Expand All @@ -91,7 +93,8 @@ public class IdentityManager implements IdentityFactory, IdentityController {
* Listeners for standard identities are inserted with a <code>null</code> key, listeners for a
* specific custom type use their type as the key.
*/
private final MapList<String, ConfigProviderListener> listeners = new WeakMapList<>();
private final Multimap<String, WeakReference<ConfigProviderListener>> listeners =
ArrayListMultimap.create();
/** Client info objecty. */
private final ClientInfo clientInfo;
/** The identity file used for the global config. */
Expand Down Expand Up @@ -283,13 +286,7 @@ private void loadIdentity(final Path file) {
* @since 0.6.4
*/
private Iterable<ConfigProvider> getAllIdentities() {
final Collection<ConfigProvider> res = new LinkedHashSet<>();

for (Map.Entry<String, List<ConfigProvider>> entry : identities.entrySet()) {
res.addAll(entry.getValue());
}

return res;
return identities.values();
}

/**
Expand Down Expand Up @@ -372,20 +369,21 @@ public void addConfigProvider(final ConfigProvider identity) {

final String target = getGroup(identity);

if (identities.containsValue(target, identity)) {
if (identities.containsEntry(target, identity)) {
removeConfigProvider(identity);
}

synchronized (identities) {
identities.add(target, identity);
identities.put(target, identity);
}

LOG.debug("Adding identity: {} (group: {})", new Object[]{identity, target});

synchronized (listeners) {
for (ConfigProviderListener listener : listeners.safeGet(target)) {
listener.configProviderAdded(identity);
}
listeners.get(target).stream()
.map(WeakReference::get)
.filter(Objects::nonNull)
.forEach(l -> l.configProviderAdded(identity));
}
}

Expand All @@ -395,7 +393,7 @@ public void removeConfigProvider(final ConfigProvider identity) {

final String group = getGroup(identity);

checkArgument(identities.containsValue(group, identity));
checkArgument(identities.containsEntry(group, identity));

Path path = null;
for (Map.Entry<Path, ConfigProvider> entry : configProvidersByPath.entrySet()) {
Expand All @@ -413,9 +411,10 @@ public void removeConfigProvider(final ConfigProvider identity) {
}

synchronized (listeners) {
for (ConfigProviderListener listener : listeners.safeGet(group)) {
listener.configProviderRemoved(identity);
}
listeners.get(group).stream()
.map(WeakReference::get)
.filter(Objects::nonNull)
.forEach(l -> l.configProviderRemoved(identity));
}
}

Expand All @@ -426,21 +425,26 @@ public void registerIdentityListener(final ConfigProviderListener listener) {

@Override
public void unregisterIdentityListener(final ConfigProviderListener listener) {
listeners.removeFromAll(listener);
synchronized (listeners) {
listeners.entries().stream().filter(e -> {
final ConfigProviderListener value = e.getValue().get();
return value == null || value.equals(listener);
}).forEach(e -> listeners.remove(e.getKey(), e.getValue()));
}
}

@Override
public void registerIdentityListener(final String type, final ConfigProviderListener listener) {
checkNotNull(listener);

synchronized (listeners) {
listeners.add(type, listener);
listeners.put(type, new WeakReference<>(listener));
}
}

@Override
public List<ConfigProvider> getProvidersByType(final String type) {
return Collections.unmodifiableList(identities.safeGet(type));
public Collection<ConfigProvider> getProvidersByType(final String type) {
return Collections.unmodifiableCollection(identities.get(type));
}

/**
Expand All @@ -455,7 +459,7 @@ List<ConfigProvider> getIdentitiesForManager(final ConfigManager manager) {
final List<ConfigProvider> sources = new ArrayList<>();

synchronized (identities) {
sources.addAll(identities.safeGet(null).stream()
sources.addAll(identities.get(null).stream()
.filter(manager::identityApplies)
.collect(Collectors.toList()));
}
Expand Down Expand Up @@ -491,7 +495,7 @@ public ConfigProvider createChannelConfig(final String network, final String cha
final String myTarget = (channel + '@' + network).toLowerCase();

synchronized (identities) {
for (ConfigProvider identity : identities.safeGet(null)) {
for (ConfigProvider identity : identities.get(null)) {
if (identity.getTarget().getType() == ConfigTarget.TYPE.CHANNEL
&& identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
return identity;
Expand All @@ -516,7 +520,7 @@ public ConfigProvider createNetworkConfig(final String network) {
final String myTarget = network.toLowerCase();

synchronized (identities) {
for (ConfigProvider identity : identities.safeGet(null)) {
for (ConfigProvider identity : identities.get(null)) {
if (identity.getTarget().getType() == ConfigTarget.TYPE.NETWORK
&& identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
return identity;
Expand All @@ -541,7 +545,7 @@ public ConfigProvider createServerConfig(final String server) {
final String myTarget = server.toLowerCase();

synchronized (identities) {
for (ConfigProvider identity : identities.safeGet(null)) {
for (ConfigProvider identity : identities.get(null)) {
if (identity.getTarget().getType() == ConfigTarget.TYPE.SERVER
&& identity.getTarget().getData().equalsIgnoreCase(myTarget)) {
return identity;
Expand Down
4 changes: 2 additions & 2 deletions src/com/dmdirc/interfaces/config/IdentityController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

package com.dmdirc.interfaces.config;

import java.util.List;
import java.util.Collection;

/**
* Defines the interface implemented by the object in charge of DMDirc's identities.
Expand Down Expand Up @@ -68,7 +68,7 @@ public interface IdentityController {
*
* @since 0.6.4
*/
List<ConfigProvider> getProvidersByType(String type);
Collection<ConfigProvider> getProvidersByType(String type);

/**
* Loads user-defined identity files.
Expand Down
10 changes: 6 additions & 4 deletions src/com/dmdirc/plugins/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.updater.components.PluginComponent;
import com.dmdirc.updater.manager.UpdateManager;
import com.dmdirc.util.collections.MapList;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import java.io.File;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -366,7 +368,7 @@ public Collection<PluginMetaData> getAllPlugins() {
}
}

final MapList<String, String> newServices = new MapList<>();
final Multimap<String, String> newServices = ArrayListMultimap.create();
final Map<String, PluginMetaData> newPluginsByName = new HashMap<>();
final Map<String, PluginMetaData> newPluginsByPath = new HashMap<>();

Expand All @@ -389,13 +391,13 @@ public Collection<PluginMetaData> getAllPlugins() {

for (String service : targetMetaData.getServices()) {
final String[] parts = service.split(" ", 2);
newServices.add(parts[1], parts[0]);
newServices.put(parts[1], parts[0]);
}

for (String export : targetMetaData.getExports()) {
final String[] parts = export.split(" ");
final String name = parts.length > 4 ? parts[4] : parts[0];
newServices.add("export", name);
newServices.put("export", name);
}
}
} catch (MalformedURLException mue) {
Expand Down

0 comments on commit e2d6353

Please sign in to comment.