BUG-865: prepare to move ListenerRegistry 65/9465/2
authorRobert Varga <rovarga@cisco.com>
Wed, 30 Jul 2014 06:48:30 +0000 (08:48 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 30 Jul 2014 08:24:23 +0000 (10:24 +0200)
This will evacuate the concepts.util package, guiding users towards
plain util package.

Change-Id: I9b600761f6cd7950b366e2bf40fd85f4bc8d5e9b
Signed-off-by: Robert Varga <rovarga@cisco.com>
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/util/ListenerRegistry.java
common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java [new file with mode: 0644]

index e8f271c075360830e0fb3a5045ac0b47c0022956..07cc5ae6bed889c99ae38fad922e54a3801f886f 100644 (file)
@@ -12,10 +12,16 @@ import java.util.Collections;
 import java.util.EventListener;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+
 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
-
+/**
+ * @deprecated Use {@link org.opendaylight.yangtools.util.ListenerRegistry} instead.
+ *
+ * @param <T>
+ */
+@Deprecated
 public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
 
     private final ConcurrentHashMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
@@ -34,7 +40,7 @@ public class ListenerRegistry<T extends EventListener> implements Iterable<Liste
         return unmodifiableView;
     }
 
-    public ListenerRegistration<T> register(T listener) {
+    public ListenerRegistration<T> register(final T listener) {
         if (listener == null) {
             throw new IllegalArgumentException("Listener should not be null.");
         }
@@ -42,28 +48,26 @@ public class ListenerRegistry<T extends EventListener> implements Iterable<Liste
         listeners.put(ret,ret);
         return ret;
     }
-    
-    public <L extends T> ListenerRegistration<L> registerWithType(L listener) {
+
+    public <L extends T> ListenerRegistration<L> registerWithType(final L listener) {
         ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
         listeners.put(ret,ret);
         return ret;
     }
-    
+
     @Override
     public java.util.Iterator<ListenerRegistration<T>> iterator() {
         return unmodifiableView.iterator();
     }
 
     @SuppressWarnings("rawtypes")
-    private void remove(ListenerRegistrationImpl registration) {
+    private void remove(final ListenerRegistrationImpl registration) {
         listeners.remove(registration);
     }
 
-    private class ListenerRegistrationImpl<P extends EventListener> //
-            extends AbstractObjectRegistration<P> //
-            implements ListenerRegistration<P> {
+    private class ListenerRegistrationImpl<P extends EventListener> extends AbstractObjectRegistration<P> implements ListenerRegistration<P> {
 
-        public ListenerRegistrationImpl(P instance) {
+        public ListenerRegistrationImpl(final P instance) {
             super(instance);
         }
 
diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java b/common/util/src/main/java/org/opendaylight/yangtools/util/ListenerRegistry.java
new file mode 100644 (file)
index 0000000..e8b1a3d
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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.yangtools.util;
+
+
+import java.util.Collections;
+import java.util.EventListener;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+
+
+public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
+
+    private final ConcurrentHashMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
+    final Set<ListenerRegistration<T>> unmodifiableView;
+
+    @SuppressWarnings("unchecked")
+    public ListenerRegistry() {
+        listeners = new ConcurrentHashMap<>();
+        // This conversion is known to be safe.
+        @SuppressWarnings("rawtypes")
+        final Set rawSet = Collections.unmodifiableSet(listeners.keySet());
+        unmodifiableView = rawSet;
+    }
+
+    public Iterable<ListenerRegistration<T>> getListeners() {
+        return unmodifiableView;
+    }
+
+    public ListenerRegistration<T> register(T listener) {
+        if (listener == null) {
+            throw new IllegalArgumentException("Listener should not be null.");
+        }
+        ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
+        listeners.put(ret,ret);
+        return ret;
+    }
+    
+    public <L extends T> ListenerRegistration<L> registerWithType(L listener) {
+        ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
+        listeners.put(ret,ret);
+        return ret;
+    }
+    
+    @Override
+    public java.util.Iterator<ListenerRegistration<T>> iterator() {
+        return unmodifiableView.iterator();
+    }
+
+    @SuppressWarnings("rawtypes")
+    private void remove(ListenerRegistrationImpl registration) {
+        listeners.remove(registration);
+    }
+
+    private class ListenerRegistrationImpl<P extends EventListener> //
+            extends AbstractObjectRegistration<P> //
+            implements ListenerRegistration<P> {
+
+        public ListenerRegistrationImpl(P instance) {
+            super(instance);
+        }
+
+        @Override
+        protected void removeRegistration() {
+            ListenerRegistry.this.remove(this);
+        }
+    }
+
+    public static <T extends EventListener> ListenerRegistry<T> create() {
+        return new ListenerRegistry<>();
+    }
+}