public abstract class AbstractListenerRegistration<T extends EventListener> extends AbstractObjectRegistration<T>
implements ListenerRegistration<T> {
- public AbstractListenerRegistration(T listener) {
+ protected AbstractListenerRegistration(final T listener) {
super(listener);
}
-
}
+
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.concepts;\r
-\r
-/**\r
- * Utility registration handle. It is a convenience for register-style method\r
- * which can return an AutoCloseable realized by a subclass of this class.\r
- * Invoking the close() method triggers unregistration of the state the method\r
- * installed.\r
- */\r
-public abstract class AbstractObjectRegistration<T> extends AbstractRegistration implements Registration<T> {\r
-\r
- \r
- private final T instance;\r
-\r
- public AbstractObjectRegistration(T instance) {\r
- this.instance = instance;\r
- }\r
-\r
- @Override\r
- public final T getInstance() {\r
- return instance;\r
- }\r
-\r
-}\r
+/*
+ * 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.concepts;
+
+/**
+ * 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 Registration<T> {
+ private final T instance;
+
+ protected AbstractObjectRegistration(final T instance) {
+ this.instance = instance;
+ }
+
+ @Override
+ public final T getInstance() {
+ return instance;
+ }
+}
+
*/
package org.opendaylight.yangtools.concepts;
+import java.util.concurrent.atomic.AtomicBoolean;
+
/**
* Utility registration handle. It is a convenience for register-style method
* which can return an AutoCloseable realized by a subclass of this class.
* installed.
*/
public abstract class AbstractRegistration implements AutoCloseable {
-
- private boolean closed = false;
+ private AtomicBoolean closed = new AtomicBoolean();
/**
* Remove the state referenced by this registration. This method is
protected abstract void removeRegistration();
@Override
- public synchronized void close() throws Exception {
- if (!closed) {
- closed = true;
+ public final void close() {
+ if (closed.compareAndSet(false, true)) {
removeRegistration();
}
}
-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.concepts;\r
-\r
-import java.util.EventListener;\r
-\r
-public interface ListenerRegistration<T extends EventListener> extends Registration<T> {\r
-\r
-}\r
+/*
+ * 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.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.
+ */
+public interface ListenerRegistration<T extends EventListener> extends Registration<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 occurence 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();
+}