Simplify DTCL registration support classes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DelayedDataTreeChangeListenerRegistration.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.cluster.datastore;
9
10 import akka.actor.ActorRef;
11 import java.util.EventListener;
12 import javax.annotation.concurrent.GuardedBy;
13 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15
16 class DelayedDataTreeChangeListenerRegistration<L extends EventListener> implements ListenerRegistration<L> {
17     private final RegisterDataTreeChangeListener registrationMessage;
18     private final ActorRef registrationActor;
19
20     @GuardedBy("this")
21     private boolean closed;
22
23     DelayedDataTreeChangeListenerRegistration(final RegisterDataTreeChangeListener registrationMessage,
24             final ActorRef registrationActor) {
25         this.registrationMessage = registrationMessage;
26         this.registrationActor = registrationActor;
27     }
28
29     synchronized void doRegistration(final DataTreeChangeListenerSupport support) {
30         if (!closed) {
31             support.doRegistration(registrationMessage, registrationActor);
32         }
33     }
34
35     @Override
36     public L getInstance() {
37         // ObjectRegistration annotates this method as @Nonnull but we could return null if the delegate is not set yet.
38         // In reality, we do not and should not ever call this method on DelayedDataTreeChangeListenerRegistration
39         // instances anyway but, since we have to provide an implementation to satisfy the interface, we throw
40         // UnsupportedOperationException to honor the API contract of not returning null and to avoid a FindBugs error
41         // for possibly returning null.
42         throw new UnsupportedOperationException(
43                 "getInstance should not be called on this instance since it could be null");
44     }
45
46     @Override
47     public synchronized void close() {
48         closed = true;
49     }
50 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.