Do not instantiate a Consumer for registration 68/88168/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 10:51:34 +0000 (11:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 15:43:13 +0000 (16:43 +0100)
Taking a Consumer forces us to instantiate it to capture listeners
Set. We can do the same by simply moving the reference away. For
safety we only get a Collection<?>, for which remove(Object) is just
what we want.

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

index 4c31cc55268959ce8f4e4e5646cd3c0f46724a8b..ca1b79af06884a0839dfd4de2bc89c0e2186b06d 100644 (file)
@@ -10,11 +10,11 @@ package org.opendaylight.yangtools.util;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.EventListener;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.function.Consumer;
 import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
@@ -65,7 +65,7 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
     }
 
     public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
-        final ListenerRegistration<L> ret = new ListenerRegistrationImpl<>(listener, listeners::remove);
+        final ListenerRegistration<L> ret = new ListenerRegistrationImpl<>(listener, listeners);
         listeners.add(ret);
         return ret;
     }
@@ -80,18 +80,18 @@ public final class ListenerRegistry<T extends EventListener> implements Mutable
 
     private static final class ListenerRegistrationImpl<T extends EventListener>
             extends AbstractListenerRegistration<T> {
-        private Consumer<ListenerRegistration<? super T>> removeCall;
+        private Collection<?> removeFrom;
 
-        ListenerRegistrationImpl(final T instance, final Consumer<ListenerRegistration<? super T>> removeCall) {
+        ListenerRegistrationImpl(final T instance, final Collection<?> removeFrom) {
             super(instance);
-            this.removeCall = requireNonNull(removeCall);
+            this.removeFrom = requireNonNull(removeFrom);
         }
 
         @Override
         protected void removeRegistration() {
-            removeCall.accept(this);
+            removeFrom.remove(this);
             // Do not retain reference to that state
-            removeCall = null;
+            removeFrom = null;
         }
     }
 }