Add attributes to nodes that can take attributes
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / util / ListenerRegistry.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.yangtools.concepts.util;
9
10
11 import java.util.Collections;
12 import java.util.EventListener;
13 import java.util.Set;
14 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17
18
19 public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
20
21     private final ConcurrentHashMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
22     final Set<ListenerRegistration<T>> unmodifiableView;
23
24     @SuppressWarnings("unchecked")
25     public ListenerRegistry() {
26         listeners = new ConcurrentHashMap<>();
27         // This conversion is known to be safe.
28         @SuppressWarnings("rawtypes")
29         final Set rawSet = Collections.unmodifiableSet(listeners.keySet());
30         unmodifiableView = rawSet;
31     }
32
33     public Iterable<ListenerRegistration<T>> getListeners() {
34         return unmodifiableView;
35     }
36
37     public ListenerRegistration<T> register(T listener) {
38         if (listener == null) {
39             throw new IllegalArgumentException("Listener should not be null.");
40         }
41         ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
42         listeners.put(ret,ret);
43         return ret;
44     }
45     
46     public <L extends T> ListenerRegistration<L> registerWithType(L listener) {
47         ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
48         listeners.put(ret,ret);
49         return ret;
50     }
51     
52     @Override
53     public java.util.Iterator<ListenerRegistration<T>> iterator() {
54         return unmodifiableView.iterator();
55     }
56
57     @SuppressWarnings("rawtypes")
58     private void remove(ListenerRegistrationImpl registration) {
59         listeners.remove(registration);
60     }
61
62     private class ListenerRegistrationImpl<P extends EventListener> //
63             extends AbstractObjectRegistration<P> //
64             implements ListenerRegistration<P> {
65
66         public ListenerRegistrationImpl(P instance) {
67             super(instance);
68         }
69
70         @Override
71         protected void removeRegistration() {
72             ListenerRegistry.this.remove(this);
73         }
74     }
75
76     public static <T extends EventListener> ListenerRegistry<T> create() {
77         return new ListenerRegistry<>();
78     }
79 }