Skip to content

Commit

Permalink
Move Announcer from API to Styx-Common module (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvosper authored and mikkokar committed Sep 17, 2018
1 parent fa37aba commit 8bc6637
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.MapDifference;
import com.hotels.styx.api.extension.Announcer;
import com.hotels.styx.api.Id;
import com.hotels.styx.api.Identifiable;
import org.slf4j.Logger;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

Expand All @@ -45,7 +46,7 @@
public abstract class AbstractRegistry<T extends Identifiable> implements Registry<T> {
private static final Logger LOG = getLogger(AbstractRegistry.class);

private final Announcer<Registry.ChangeListener> announcer = Announcer.to(ChangeListener.class);
private final List<ChangeListener<T>> listeners = new CopyOnWriteArrayList<>();
private final AtomicReference<Iterable<T>> snapshot = new AtomicReference<>(emptyList());
private final Predicate<Collection<T>> resourceConstraint;

Expand All @@ -72,13 +73,13 @@ public Iterable<T> get() {
}

protected void notifyListeners(Changes<T> changes) {
LOG.info("notifying about services={} to listeners={}", changes, announcer.listeners());
announcer.announce().onChange(changes);
LOG.info("notifying about services={} to listeners={}", changes, listeners);
listeners.forEach(listener -> listener.onChange(changes));
}

@Override
public Registry<T> addListener(ChangeListener<T> changeListener) {
announcer.addListener(changeListener);
listeners.add(changeListener);
changeListener.onChange(added(snapshot.get()));
return this;
}
Expand Down Expand Up @@ -109,7 +110,7 @@ private Changes<T> added(Iterable<T> ch) {

@Override
public Registry<T> removeListener(ChangeListener<T> changeListener) {
announcer.removeListener(changeListener);
listeners.remove(changeListener);
return this;
}

Expand Down

0 comments on commit 8bc6637

Please sign in to comment.