+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 AbstractRegistration<T> implements Registration<T> {\r
+\r
+ private boolean closed = false;\r
+ private final T instance;\r
+\r
+ public AbstractRegistration(T instance) {\r
+ this.instance = instance;\r
+ }\r
+\r
+ @Override\r
+ public T getInstance() {\r
+ return instance;\r
+ }\r
+\r
+ /**\r
+ * Remove the state referenced by this registration. This method is\r
+ * guaranteed to be called at most once. The referenced state must be\r
+ * retained until this method is invoked.\r
+ */\r
+ protected abstract void removeRegistration();\r
+\r
+ @Override\r
+ public void close() throws Exception {\r
+ if (!closed) {\r
+ closed = true;\r
+ removeRegistration();\r
+ }\r
+ }\r
+\r
+}\r