Renamed AbstractListenerRegistry to ListenerRegistry (since it is not abstract).
- added checkForNonRegistrant() predicate and augmented unit tests.
Change-Id: I7ecc618f01fdc4cead6f143a1e5e0ddd03268efc
diff --git a/core/api/src/main/java/org/onosproject/event/AbstractListenerRegistry.java b/core/api/src/main/java/org/onosproject/event/ListenerRegistry.java
similarity index 79%
rename from core/api/src/main/java/org/onosproject/event/AbstractListenerRegistry.java
rename to core/api/src/main/java/org/onosproject/event/ListenerRegistry.java
index 38575a4..4164441 100644
--- a/core/api/src/main/java/org/onosproject/event/AbstractListenerRegistry.java
+++ b/core/api/src/main/java/org/onosproject/event/ListenerRegistry.java
@@ -28,15 +28,20 @@
* Base implementation of an event sink and a registry capable of tracking
* listeners and dispatching events to them as part of event sink processing.
*/
-public class AbstractListenerRegistry<E extends Event, L extends EventListener<E>>
+public class ListenerRegistry<E extends Event, L extends EventListener<E>>
implements EventSink<E> {
private final Logger log = getLogger(getClass());
- protected final Set<L> listeners = new CopyOnWriteArraySet<>();
private volatile boolean shutdown = false;
/**
+ * Set of listeners that have registered.
+ */
+ protected final Set<L> listeners = new CopyOnWriteArraySet<>();
+
+
+ /**
* Adds the specified listener.
*
* @param listener listener to be added
@@ -53,7 +58,9 @@
*/
public void removeListener(L listener) {
checkNotNull(listener, "Listener cannot be null");
- checkArgument(listeners.remove(listener), "Listener not registered");
+ if (checkForNonRegistrant()) {
+ checkArgument(listeners.remove(listener), "Listener not registered");
+ }
}
@Override
@@ -68,6 +75,19 @@
}
/**
+ * Predicate indicating whether we should throw an exception if the
+ * argument to {@link #removeListener} is not in the current set of
+ * listeners.
+ * <p>
+ * This default implementation returns <code>true</code>.
+ *
+ * @return true if non-listed listeners should cause exception on remove
+ */
+ protected boolean checkForNonRegistrant() {
+ return true;
+ }
+
+ /**
* Reports a problem encountered while processing an event.
*
* @param event event being processed
@@ -92,5 +112,4 @@
public void deactivate() {
shutdown = true;
}
-
}