Filter registered listeners 56/88156/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 11:18:28 +0000 (12:18 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 11:21:49 +0000 (12:21 +0100)
There is a small race window where the registration could be marked
as unregistered and not removed from the map. Check that condition
before letting the listener through.

Change-Id: I1938e6459faaaadf69bc7907ba6ba71e65fe9e8f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java

index d042b8be6141cfee45c539f7dc36ba64758ff3eb..100acc9eb904112199f64f080db16c8e97b2d899 100644 (file)
@@ -26,7 +26,7 @@ import org.opendaylight.yangtools.concepts.Mutable;
  * @param <T> Type of listeners this registry handles
  */
 public final class ListenerRegistry<T extends EventListener> implements Mutable {
-    private final Set<ListenerRegistration<? extends T>> listeners = ConcurrentHashMap.newKeySet();
+    private final Set<RegistrationImpl<? extends T>> listeners = ConcurrentHashMap.newKeySet();
     private final String name;
 
     private ListenerRegistry(final String name) {
@@ -42,7 +42,7 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
     }
 
     public void clear() {
-        listeners.stream().forEach(ListenerRegistration::close);
+        listeners.stream().forEach(RegistrationImpl::close);
     }
 
     public boolean isEmpty() {
@@ -50,11 +50,11 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
     }
 
     public Stream<? extends T> streamListeners() {
-        return listeners.stream().map(ListenerRegistration::getInstance);
+        return listeners.stream().filter(RegistrationImpl::notClosed).map(RegistrationImpl::getInstance);
     }
 
     public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
-        final ListenerRegistration<L> ret = new ListenerRegistrationImpl<>(listener, listeners);
+        final RegistrationImpl<L> ret = new RegistrationImpl<>(listener, listeners);
         listeners.add(ret);
         return ret;
     }
@@ -67,11 +67,10 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
                 .toString();
     }
 
-    private static final class ListenerRegistrationImpl<T extends EventListener>
-            extends AbstractListenerRegistration<T> {
+    private static final class RegistrationImpl<T extends EventListener> extends AbstractListenerRegistration<T> {
         private Collection<?> removeFrom;
 
-        ListenerRegistrationImpl(final T instance, final Collection<?> removeFrom) {
+        RegistrationImpl(final T instance, final Collection<?> removeFrom) {
             super(instance);
             this.removeFrom = requireNonNull(removeFrom);
         }