Merge "Fixed publishDataChangeEvent in 2phase commit"
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / ListenerRegistry.java
1 /*
2  * Copyright (c) 2014 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.controller.md.sal.common.impl;
9
10 import java.util.Collections;
11 import java.util.EventListener;
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.concurrent.ConcurrentHashMap;
15
16 import static com.google.common.base.Preconditions.*;
17
18 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20
21 public class ListenerRegistry<T extends EventListener> {
22
23     final Set<ListenerRegistration<T>> listeners;
24     final Set<ListenerRegistration<T>> unmodifiableView;
25
26     public ListenerRegistry() {
27         listeners = new HashSet<>();
28         unmodifiableView = Collections.unmodifiableSet(listeners);
29     }
30
31     public Iterable<ListenerRegistration<T>> getListeners() {
32         return unmodifiableView;
33     }
34
35     
36     public ListenerRegistration<T> register(T listener) {
37         checkNotNull(listener, "Listener should not be null.");
38         ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
39         listeners.add(ret);
40         return ret;
41     }
42     
43     
44     @SuppressWarnings("rawtypes")
45     private void remove(ListenerRegistrationImpl registration) {
46         listeners.remove(registration);
47     }
48
49     private class ListenerRegistrationImpl<P extends EventListener> //
50             extends AbstractObjectRegistration<P> //
51             implements ListenerRegistration<P> {
52
53         public ListenerRegistrationImpl(P instance) {
54             super(instance);
55         }
56
57         @Override
58         protected void removeRegistration() {
59             ListenerRegistry.this.remove(this);
60         }
61     }
62 }