Skip to content

Commit

Permalink
Remove use of WeakList.
Browse files Browse the repository at this point in the history
This is fairly trivial to implement with streams/lambdas,
no need to have our own hand-rolled list implementation
for it.
  • Loading branch information
csmith committed Jan 17, 2015
1 parent 892e161 commit 538a202
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/com/dmdirc/config/ConfigFileBackedConfigProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
import com.dmdirc.interfaces.config.ConfigChangeListener;
import com.dmdirc.interfaces.config.ConfigProvider;
import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.util.collections.WeakList;
import com.dmdirc.util.io.ConfigFile;
import com.dmdirc.util.io.InvalidConfigFileException;
import com.dmdirc.util.validators.Validator;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -44,6 +43,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.annotation.Nullable;

Expand All @@ -70,7 +70,8 @@ public class ConfigFileBackedConfigProvider implements ConfigProvider {
/** The global config manager. */
protected ConfigManager globalConfig;
/** The config change listeners for this source. */
protected final List<ConfigChangeListener> listeners = new WeakList<>();
protected final List<WeakReference<ConfigChangeListener>> listeners =
new CopyOnWriteArrayList<>();
/** The event bus to post error events to. */
private final DMDircMBassador eventBus;
/** Whether this identity needs to be saved. */
Expand Down Expand Up @@ -274,9 +275,10 @@ public void reload() throws IOException, InvalidConfigFileException {
* @since 0.6.3m1
*/
private void fireSettingChange(final String domain, final String key) {
for (ConfigChangeListener listener : new ArrayList<>(listeners)) {
listener.configChanged(domain, key);
}
listeners.stream()
.map(WeakReference::get)
.filter(Objects::nonNull)
.forEach(l -> l.configChanged(domain, key));
}

@Override
Expand Down Expand Up @@ -518,12 +520,15 @@ public ConfigTarget getTarget() {

@Override
public void addListener(final ConfigChangeListener listener) {
listeners.add(listener);
listeners.add(new WeakReference<>(listener));
}

@Override
public void removeListener(final ConfigChangeListener listener) {
listeners.remove(listener);
listeners.stream().filter(w -> {
final ConfigChangeListener target = w.get();
return target == null || target.equals(listener);
}).forEach(listeners::remove);
}

/**
Expand Down

0 comments on commit 538a202

Please sign in to comment.