Merge changes I14f284cc,I65bfdb56
[yangtools.git] / concepts / src / main / java / org / opendaylight / yangtools / concepts / AbstractRegistration.java
1 package org.opendaylight.yangtools.concepts;\r
2 \r
3 /**\r
4  * Utility registration handle. It is a convenience for register-style method\r
5  * which can return an AutoCloseable realized by a subclass of this class.\r
6  * Invoking the close() method triggers unregistration of the state the method\r
7  * installed.\r
8  */\r
9 public abstract class AbstractRegistration<T> implements Registration<T> {\r
10 \r
11     private boolean closed = false;\r
12     private final T instance;\r
13 \r
14     public AbstractRegistration(T instance) {\r
15         this.instance = instance;\r
16     }\r
17 \r
18     @Override\r
19     public T getInstance() {\r
20         return instance;\r
21     }\r
22 \r
23     /**\r
24      * Remove the state referenced by this registration. This method is\r
25      * guaranteed to be called at most once. The referenced state must be\r
26      * retained until this method is invoked.\r
27      */\r
28     protected abstract void removeRegistration();\r
29 \r
30     @Override\r
31     public void close() throws Exception {\r
32         if (!closed) {\r
33             closed = true;\r
34             removeRegistration();\r
35         }\r
36     }\r
37 \r
38 }\r