Add NoOp{Listener,Object}Registration 51/76551/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 1 Oct 2018 19:01:01 +0000 (21:01 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 1 Oct 2018 22:21:35 +0000 (22:21 +0000)
As it turns out, there are use cases for registration which do
nothing in their close() method. Add utility classes to support
those use cases. Also update javadocs a bit.

Change-Id: Ife196f26229cf54173e9b9046ece22bd0138c2ea
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpListenerRegistration.java [new file with mode: 0644]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpObjectRegistration.java [new file with mode: 0644]
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java

index 22ab06926897c763796fb181a06ea0c81a74fedb..02d0d609259a08e35ef6e71fda1fc52ecdaf6df3 100644 (file)
@@ -12,6 +12,8 @@ import java.util.EventListener;
 /**
  * Class representing a {@link Registration} of an {@link EventListener}. This interface provides the additional
  * guarantee that the process of unregistration cannot fail for predictable reasons.
+ *
+ * @param <T> Type of registered listener
  */
 public interface ListenerRegistration<T extends EventListener> extends ObjectRegistration<T> {
     /**
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpListenerRegistration.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpListenerRegistration.java
new file mode 100644 (file)
index 0000000..9780715
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.concepts;
+
+import com.google.common.annotations.Beta;
+import java.util.EventListener;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * Implementation of {@link ListenerRegistration} which does nothing in its {@link #close()} method.
+ *
+ * @param <T> Type of registered listener
+ */
+@Beta
+@NonNullByDefault
+public final class NoOpListenerRegistration<T extends EventListener> extends NoOpObjectRegistration<T>
+        implements ListenerRegistration<T> {
+    private NoOpListenerRegistration(final T instance) {
+        super(instance);
+    }
+
+    public static <T extends EventListener> ListenerRegistration<T> of(final T instance) {
+        return new NoOpListenerRegistration<>(instance);
+    }
+}
diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpObjectRegistration.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpObjectRegistration.java
new file mode 100644 (file)
index 0000000..8610680
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.concepts;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * Implementation of {@link ObjectRegistration} which does nothing in its {@link #close()} method.
+ *
+ * @param <T> Type of registered object
+ */
+@Beta
+@NonNullByDefault
+public class NoOpObjectRegistration<T> implements Immutable, ObjectRegistration<T> {
+    private final T instance;
+
+    NoOpObjectRegistration(final T instance) {
+        this.instance = requireNonNull(instance);
+    }
+
+    public static <T> ObjectRegistration<T> of(final T instance) {
+        return new NoOpObjectRegistration<>(instance);
+    }
+
+    @Override
+    public final T getInstance() {
+        return instance;
+    }
+
+    @Override
+    public final void close() {
+        // No-op
+    }
+
+    @Override
+    public final String toString() {
+        return MoreObjects.toStringHelper(this).add("instance", instance).toString();
+    }
+}
index ca0ce1e76cbd2d5660a03986a4758efb3783775d..6d1844f9001f5013c98f0af3b85abd3585d9a640 100644 (file)
@@ -13,6 +13,8 @@ import javax.annotation.Nonnull;
  * Class representing a registration of an object. Such a registration is a proper resource and should be cleaned up
  * when no longer required, so references to the object can be removed. This mechanism lies above the usual Java
  * reference mechanism, as the entity where the object is registered may reside outside of the Java Virtual Machine.
+ *
+ * @param <T> Type of registered object
  */
 public interface ObjectRegistration<T> extends Registration {
     /**
@@ -21,7 +23,4 @@ public interface ObjectRegistration<T> extends Registration {
      * @return Registered object.
      */
     @Nonnull T getInstance();
-
-    @Override
-    void close();
 }