BUG-8665: fix memory leak around RangeSets
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DelayedListenerRegistration.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.ListenerRegistrationMessage;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15
16 abstract class DelayedListenerRegistration<L extends EventListener, M extends ListenerRegistrationMessage>
17         implements ListenerRegistration<L> {
18     private final M registrationMessage;
19     private final ActorRef registrationActor;
20
21     @GuardedBy("this")
22     private boolean closed;
23
24     protected DelayedListenerRegistration(M registrationMessage, ActorRef registrationActor) {
25         this.registrationMessage = registrationMessage;
26         this.registrationActor = registrationActor;
27     }
28
29     M getRegistrationMessage() {
30         return registrationMessage;
31     }
32
33     synchronized void createDelegate(final AbstractDataListenerSupport<L, M, ?> support) {
34         if (!closed) {
35             support.doRegistration(registrationMessage, registrationActor);
36         }
37     }
38
39     @Override
40     public L getInstance() {
41         // We could return null if the delegate is not set yet. In reality though, we do not and should not ever call
42         // this method on DelayedListenerRegistration instances but, since we have to provide an implementation to
43         // satisfy the interface, we throw UnsupportedOperationException to avoid possibly returning null.
44         throw new UnsupportedOperationException(
45                 "getInstance should not be called on this instance since it could be null");
46     }
47
48     @Override
49     public synchronized void close() {
50         closed = true;
51     }
52 }