Update concepts.Registration 45/64745/5
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 25 Oct 2017 20:17:27 +0000 (22:17 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Oct 2017 14:26:34 +0000 (16:26 +0200)
Do not allow Registration.close() to throw checked exceptions. While it
is a good practice to check for exceptions in foreign code, it is purely
optional and should include RuntimeExceptions as well.

Also update AbstractRegistration to enforce identity hashCode/equals and
also toString() format.

Change-Id: I8d70273e1a42943288a16ac8edf40381707da186
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractObjectRegistration.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/AbstractRegistration.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ListenerRegistration.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/ObjectRegistration.java
common/concepts/src/main/java/org/opendaylight/yangtools/concepts/Registration.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/AbstractSchemaSourceRegistration.java

index 820f8af02c2957ce26d0ca20ce9e2ea7db5fc237..7fb1b39173704da360352b59531d57fd0a2a6ffb 100644 (file)
@@ -9,13 +9,12 @@ package org.opendaylight.yangtools.concepts;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.base.MoreObjects.ToStringHelper;
 import javax.annotation.Nonnull;
 
 /**
- * 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.
+ * 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.
  */
 public abstract class AbstractObjectRegistration<T> extends AbstractRegistration implements ObjectRegistration<T> {
     private final T instance;
@@ -30,8 +29,8 @@ public abstract class AbstractObjectRegistration<T> extends AbstractRegistration
     }
 
     @Override
-    public String toString() {
-        return "AbstractObjectRegistration{instance=" + instance + '}';
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("instance", instance);
     }
 }
 
index c07b38b3ff7515ba064ff63d1701e537616b4a11..f0e6238e339a54dc52f1b576ac22e601f5509178 100644 (file)
@@ -7,33 +7,31 @@
  */
 package org.opendaylight.yangtools.concepts;
 
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
 
 /**
- * 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.
+ * 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.
  */
-public abstract class AbstractRegistration implements AutoCloseable {
+public abstract class AbstractRegistration implements Registration {
     private static final AtomicIntegerFieldUpdater<AbstractRegistration> CLOSED_UPDATER =
             AtomicIntegerFieldUpdater.newUpdater(AbstractRegistration.class, "closed");
     private volatile int closed = 0;
 
     /**
-     * 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.
+     * Remove the state referenced by this registration. This method is guaranteed to be called at most once.
+     * Referenced state must be retained until this method is invoked.
      */
     protected abstract void removeRegistration();
 
     /**
-     * Query the state of this registration. Returns true if it was
-     * closed.
+     * Query the state of this registration. Returns true if it was closed.
      *
      * @return true if the registration was closed, false otherwise.
      */
-    protected final boolean isClosed() {
+    public final boolean isClosed() {
         return closed != 0;
     }
 
@@ -43,4 +41,13 @@ public abstract class AbstractRegistration implements AutoCloseable {
             removeRegistration();
         }
     }
+
+    @Override
+    public final String toString() {
+        return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
+    }
+
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return toStringHelper.add("closed", closed);
+    }
 }
index a798f2ef6d0053707bc0693dd4b4f5cd467fcd96..a9ecf0bc825eb1e7daa4b032c07f84e6073baa1a 100644 (file)
@@ -10,18 +10,19 @@ package org.opendaylight.yangtools.concepts;
 import java.util.EventListener;
 
 /**
- * Class representing a {@link Registration} of an {@link EventListener}. This
- * is interface provides the additional guarantee that the process of
- * unregistration cannot fail for predictable reasons.
+ * 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.
  */
 public interface ListenerRegistration<T extends EventListener> extends ObjectRegistration<T> {
     /**
-     * Unregister the listener. No events should be delivered to the listener
-     * once this method returns successfully. While the interface contract
-     * allows an implementation to ignore the occurrence of RuntimeExceptions,
-     * implementations are strongly encouraged to deal with such exceptions
-     * internally and to ensure invocations of this method do not fail in such
-     * circumstances.
+     * Unregister the listener. Note that invocations enqueued to the listener are not subject to synchronization
+     * rules, and events may be delivered to the listener after this method completes.
+     * Unregister the listener.
+     *
+     * <p>
+     * While the interface contract allows an implementation to ignore the occurrence of RuntimeExceptions,
+     * implementations are strongly encouraged to deal with such exceptions internally and to ensure invocations of
+     * this method do not fail in such circumstances.
      */
     @Override
     void close();
index f2eafef209c0a60a4732ef7bc806231d82b20ff2..ca0ce1e76cbd2d5660a03986a4758efb3783775d 100644 (file)
@@ -10,11 +10,9 @@ package org.opendaylight.yangtools.concepts;
 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.
+ * 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.
  */
 public interface ObjectRegistration<T> extends Registration {
     /**
@@ -24,13 +22,6 @@ public interface ObjectRegistration<T> extends Registration {
      */
     @Nonnull T getInstance();
 
-    /**
-     * Unregisters the object. This operation is required not to invoke
-     * blocking operations. Implementations which require interaction
-     * with outside world must provide guarantees that any work is done
-     * behind the scenes and the unregistration process looks as if it
-     * has already succeeded once this method returns.
-     */
     @Override
-    void close() throws Exception;
+    void close();
 }
index 43635f4f7a0123a599b320e0e111a602bedf4303..c166eb639407333f1112bcf412f56f01c0c84ee1 100644 (file)
@@ -8,17 +8,15 @@
 package org.opendaylight.yangtools.concepts;
 
 /**
- * Class representing a registration. Such a registration is a proper
- * resource and should be cleaned up when no longer required.
+ * Class representing a registration. Such a registration is a proper resource and should be cleaned up when no longer
+ * required.
  */
 public interface Registration extends AutoCloseable {
     /**
-     * Unregisters the object. This operation is required not to invoke
-     * blocking operations. Implementations which require interaction
-     * with outside world must provide guarantees that any work is done
-     * behind the scenes and the unregistration process looks as if it
-     * has already succeeded once this method returns.
+     * Unregisters the object. This operation is required not to invoke blocking operations. Implementations which
+     * require interaction with outside world must provide guarantees that any work is done behind the scenes and
+     * the unregistration process looks as if it has already succeeded once this method returns.
      */
     @Override
-    void close() throws Exception;
+    void close();
 }
index 239144518ac856299d61d31349133b3760777aae..a6e12b7d4c85ada8542d346607d1a41864d2be3b 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.model.repo.util;
 
 import static java.util.Objects.requireNonNull;
 
+import com.google.common.base.MoreObjects.ToStringHelper;
 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
@@ -25,7 +26,12 @@ public abstract class AbstractSchemaSourceRegistration<T extends SchemaSourceRep
         this.provider = requireNonNull(provider);
     }
 
-    protected final SchemaSourceProvider<?> getProvider() {
+    public final SchemaSourceProvider<?> getProvider() {
         return provider;
     }
+
+    @Override
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("provider", provider);
+    }
 }