Merge "BUG-1402: removed hardcoded plugin version for testing."
[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
16 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18
19 /**
20  * @deprecated Use {@link org.opendaylight.yangtools.util.ListenerRegistry} instead.
21  *
22  * @param <T>
23  */
24 @Deprecated
25 public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
26
27     private final ConcurrentHashMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
28     final Set<ListenerRegistration<T>> unmodifiableView;
29
30     @SuppressWarnings("unchecked")
31     public ListenerRegistry() {
32         listeners = new ConcurrentHashMap<>();
33         // This conversion is known to be safe.
34         @SuppressWarnings("rawtypes")
35         final Set rawSet = Collections.unmodifiableSet(listeners.keySet());
36         unmodifiableView = rawSet;
37     }
38
39     public Iterable<ListenerRegistration<T>> getListeners() {
40         return unmodifiableView;
41     }
42
43     public ListenerRegistration<T> register(final T listener) {
44         if (listener == null) {
45             throw new IllegalArgumentException("Listener should not be null.");
46         }
47         ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
48         listeners.put(ret,ret);
49         return ret;
50     }
51
52     public <L extends T> ListenerRegistration<L> registerWithType(final L listener) {
53         ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
54         listeners.put(ret,ret);
55         return ret;
56     }
57
58     @Override
59     public java.util.Iterator<ListenerRegistration<T>> iterator() {
60         return unmodifiableView.iterator();
61     }
62
63     @SuppressWarnings("rawtypes")
64     private void remove(final ListenerRegistrationImpl registration) {
65         listeners.remove(registration);
66     }
67
68     private class ListenerRegistrationImpl<P extends EventListener> extends AbstractObjectRegistration<P> implements ListenerRegistration<P> {
69
70         public ListenerRegistrationImpl(final P instance) {
71             super(instance);
72         }
73
74         @Override
75         protected void removeRegistration() {
76             ListenerRegistry.this.remove(this);
77         }
78     }
79
80     public static <T extends EventListener> ListenerRegistry<T> create() {
81         return new ListenerRegistry<>();
82     }
83 }