From: Robert Varga Date: Mon, 1 Oct 2018 19:01:01 +0000 (+0200) Subject: Add NoOp{Listener,Object}Registration X-Git-Tag: v2.0.12~10 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=dd015bd4f633ed9049e20e0c8ff2590801b5e20f;p=yangtools.git Add NoOp{Listener,Object}Registration 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 --- diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java index 22ab069268..02d0d60925 100644 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java @@ -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 Type of registered listener */ public interface ListenerRegistration extends ObjectRegistration { /** 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 index 0000000000..9780715c63 --- /dev/null +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpListenerRegistration.java @@ -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 Type of registered listener + */ +@Beta +@NonNullByDefault +public final class NoOpListenerRegistration extends NoOpObjectRegistration + implements ListenerRegistration { + private NoOpListenerRegistration(final T instance) { + super(instance); + } + + public static ListenerRegistration 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 index 0000000000..8610680624 --- /dev/null +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/NoOpObjectRegistration.java @@ -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 Type of registered object + */ +@Beta +@NonNullByDefault +public class NoOpObjectRegistration implements Immutable, ObjectRegistration { + private final T instance; + + NoOpObjectRegistration(final T instance) { + this.instance = requireNonNull(instance); + } + + public static ObjectRegistration 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(); + } +} diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java index ca0ce1e76c..6d1844f900 100644 --- a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java @@ -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 Type of registered object */ public interface ObjectRegistration extends Registration { /** @@ -21,7 +23,4 @@ public interface ObjectRegistration extends Registration { * @return Registered object. */ @Nonnull T getInstance(); - - @Override - void close(); }