Add ListenerRegistry.clear() 67/88167/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 10:30:28 +0000 (11:30 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 15:42:12 +0000 (16:42 +0100)
There are callers who mean to only clear the registry for which
they are accessing the public view. Provide a method to do that
and mark getRegistrations() as deprecated.

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

index 598b96d1900b0678b142fa48faf41e9a74c40201..4c31cc55268959ce8f4e4e5646cd3c0f46724a8b 100644 (file)
@@ -47,10 +47,15 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
         return new ListenerRegistry<>(requireNonNull(name));
     }
 
+    @Deprecated(forRemoval = true)
     public @NonNull Set<? extends ListenerRegistration<? extends T>> getRegistrations() {
         return unmodifiableView;
     }
 
+    public void clear() {
+        listeners.stream().forEach(ListenerRegistration::close);
+    }
+
     public boolean isEmpty() {
         return listeners.isEmpty();
     }
@@ -59,7 +64,7 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
         return listeners.stream().map(ListenerRegistration::getInstance);
     }
 
-    public <L extends T> @NonNull  ListenerRegistration<L> register(final L listener) {
+    public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
         final ListenerRegistration<L> ret = new ListenerRegistrationImpl<>(listener, listeners::remove);
         listeners.add(ret);
         return ret;
index d8b3f792dee1894358f5a8b940cc5a96a40a36e8..6c156a1998a59bc50a4545e69bf13e4a4d9fceb7 100644 (file)
@@ -17,13 +17,11 @@ import org.junit.Test;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
 public class ListenerRegistryTest {
-    private TestEventListener testEventListener;
     private ExtendedTestEventListener extendedTestEventListener;
     private ListenerRegistry<TestEventListener> registry;
 
     @Before
     public void init() {
-        testEventListener = new TestEventListener() {};
         extendedTestEventListener = new ExtendedTestEventListener() {};
         registry = ListenerRegistry.create();
     }
@@ -34,7 +32,7 @@ public class ListenerRegistryTest {
     }
 
     @Test
-    public void tetGetListenersMethod() {
+    public void testGetListenersMethod() {
         assertEquals("Listener registry should have any listeners.", ImmutableSet.of(), registry.getRegistrations());
     }