Code Clean Up
[bgpcep.git] / concepts / src / main / java / org / opendaylight / protocol / concepts / AbstractRegistration.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.concepts;
9
10 import javax.annotation.concurrent.GuardedBy;
11 import javax.annotation.concurrent.ThreadSafe;
12
13 /**
14  * Utility registration handle. It is a convenience for register-style method which can return an AutoCloseable realized
15  * by a subclass of this class. Invoking the close() method triggers unregistration of the state the method installed.
16  */
17 @ThreadSafe
18 public abstract class AbstractRegistration implements AutoCloseable {
19     @GuardedBy("this")
20     private boolean closed = false;
21
22     /**
23      * Remove the state referenced by this registration. This method is guaranteed to be called at most once. The
24      * referenced state must be retained until this method is invoked.
25      */
26     protected abstract void removeRegistration();
27
28     @Override
29     public final synchronized void close() {
30         if (!this.closed) {
31             this.closed = true;
32             removeRegistration();
33         }
34     }
35 }