Rework ListenerRegistration to be more usable 44/1644/2
authorRobert Varga <rovarga@cisco.com>
Thu, 3 Oct 2013 13:02:33 +0000 (15:02 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 3 Oct 2013 13:46:04 +0000 (15:46 +0200)
Change-Id: I2c8d2ddae3c59ceb6caa44bf6ee3ef008a4ac6d0
Signed-off-by: Robert Varga <rovarga@cisco.com>
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/BGPImpl.java
bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/EventBusRegistration.java
concepts/src/main/java/org/opendaylight/protocol/concepts/AbstractRegistration.java [new file with mode: 0644]
concepts/src/main/java/org/opendaylight/protocol/concepts/ListenerRegistration.java

index 6ddde23f6e7b140b172ec9d1b327152a55cc0797..a963fc5d956596df6b199f91963e39e996629702 100644 (file)
@@ -28,26 +28,18 @@ public class BGPImpl implements BGP, Closeable {
        /**
         * Wrapper class to give listener a close method.
         */
-       public class BGPListenerRegistration implements ListenerRegistration<BGPSessionListener> {
-
-               private final BGPSessionListener listener;
-
+       public class BGPListenerRegistration extends ListenerRegistration<BGPSessionListener> {
                private final BGPSession session;
 
                public BGPListenerRegistration(final BGPSessionListener l, final BGPSession session) {
-                       this.listener = l;
+                       super(l);
                        this.session = session;
                }
 
                @Override
-               public void close() {
+               public void removeRegistration() {
                        this.session.close();
                }
-
-               @Override
-               public BGPSessionListener getListener() {
-                       return this.listener;
-               }
        }
 
        private final BGPDispatcher dispatcher;
index 6cce4fe68f7daddee0de32571e9257831e35490c..bf809b60c64dbb33e4898fcf22cfb977163dd13c 100644 (file)
@@ -10,8 +10,6 @@ package org.opendaylight.protocol.bgp.rib.mock;
 import java.util.List;
 import java.util.Set;
 
-import javax.annotation.concurrent.GuardedBy;
-
 import org.opendaylight.protocol.bgp.parser.BGPSession;
 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
 import org.opendaylight.protocol.bgp.parser.BGPTableType;
@@ -31,11 +29,8 @@ import com.google.common.eventbus.Subscribe;
  * This class has @Subscribe annotated methods which receive events from {@link EventBus} . Events are produced by
  * {@link BGPMock}, and each instance notifies exactly one {@link BGPSessionListener}.
  */
-class EventBusRegistration implements ListenerRegistration<BGPSessionListener> {
+class EventBusRegistration extends ListenerRegistration<BGPSessionListener> {
        private final EventBus eventBus;
-       private final BGPSessionListener listener;
-       @GuardedBy("this")
-       private boolean closed = false;
 
        public static EventBusRegistration createAndRegister(final EventBus eventBus, final BGPSessionListener listener,
                        final List<Notification> allPreviousMessages) {
@@ -45,8 +40,8 @@ class EventBusRegistration implements ListenerRegistration<BGPSessionListener> {
        }
 
        private EventBusRegistration(final EventBus eventBus, final BGPSessionListener listener, final List<Notification> allPreviousMessages) {
+               super(listener);
                this.eventBus = eventBus;
-               this.listener = listener;
                for (final Notification message : allPreviousMessages) {
                        sendMessage(listener, message);
                }
@@ -58,12 +53,8 @@ class EventBusRegistration implements ListenerRegistration<BGPSessionListener> {
        }
 
        @Override
-       public synchronized void close() {
-               if (this.closed) {
-                       return;
-               }
+       public synchronized void removeRegistration() {
                this.eventBus.unregister(this);
-               this.closed = true;
        }
 
        private static void sendMessage(final BGPSessionListener listener, final Notification message) {
@@ -98,9 +89,4 @@ class EventBusRegistration implements ListenerRegistration<BGPSessionListener> {
                        listener.onMessage(null, message);
                }
        }
-
-       @Override
-       public BGPSessionListener getListener() {
-               return this.listener;
-       }
 }
diff --git a/concepts/src/main/java/org/opendaylight/protocol/concepts/AbstractRegistration.java b/concepts/src/main/java/org/opendaylight/protocol/concepts/AbstractRegistration.java
new file mode 100644 (file)
index 0000000..9ba802c
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.concepts;
+
+import javax.annotation.concurrent.GuardedBy;
+import javax.annotation.concurrent.ThreadSafe;
+
+/**
+ * Utility registration handle. It is a convenience for register-style method
+ * which can return an AutoCloseable realized by a subclass of this class.
+ * Invoking the close() method triggers unregistration of the state the method
+ * installed.
+ */
+@ThreadSafe
+public abstract class AbstractRegistration implements AutoCloseable {
+       @GuardedBy("this")
+       private boolean closed = false;
+
+       /**
+        * Remove the state referenced by this registration. This method is
+        * guaranteed to be called at most once. The referenced state must be
+        * retained until this method is invoked.
+        */
+       protected abstract void removeRegistration();
+
+       @Override
+       public final synchronized void close() {
+               if (!closed) {
+                       closed = true;
+                       removeRegistration();
+               }
+       }
+}
index 7cf59b89368e754345702052f8d7ed56a12f1f62..b569de6ab95d88ba7daf1b13c620db0a30318de3 100644 (file)
@@ -7,9 +7,12 @@
  */
 package org.opendaylight.protocol.concepts;
 
-import java.io.Closeable;
 import java.util.EventListener;
 
+import javax.annotation.concurrent.ThreadSafe;
+
+import com.google.common.base.Preconditions;
+
 /**
  * An interface representing a listener registration. Objects offering
  * the ability to register listener should return an implementation of this
@@ -18,15 +21,21 @@ import java.util.EventListener;
  *
  * @param <T> template reference to associated EventListener implementation
  */
-public interface ListenerRegistration<T extends EventListener> extends Closeable {
+@ThreadSafe
+public abstract class ListenerRegistration<T extends EventListener> extends AbstractRegistration {
+       protected final T listener;
+
+       protected ListenerRegistration(final T listener) {
+               this.listener = Preconditions.checkNotNull(listener);
+       }
+
        /**
         * Access the listener object associated with this registration.
         *
         * @return Associated listener.
         */
-       public T getListener();
-
-       @Override
-       public void close();
+       public final T getListener() {
+               return listener;
+       }
 }