1 package org.opendaylight.yangtools.concepts.util;
3 import java.util.Collections;
4 import java.util.EventListener;
6 import java.util.concurrent.ConcurrentHashMap;
8 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
9 import org.opendaylight.yangtools.concepts.ListenerRegistration;
11 public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
13 final ConcurrentHashMap<ListenerRegistration<T>,ListenerRegistration<T>> listeners;
14 final Set<ListenerRegistration<T>> unmodifiableView;
16 public ListenerRegistry() {
17 listeners = new ConcurrentHashMap<>();
18 unmodifiableView = Collections.unmodifiableSet(listeners.keySet());
21 public Iterable<ListenerRegistration<T>> getListeners() {
22 return unmodifiableView;
25 public ListenerRegistration<T> register(T listener) {
26 if (listener == null) {
27 throw new IllegalArgumentException("Listener should not be null.");
29 ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
30 listeners.put(ret,ret);
35 public java.util.Iterator<ListenerRegistration<T>> iterator() {
36 return unmodifiableView.iterator();
39 @SuppressWarnings("rawtypes")
40 private void remove(ListenerRegistrationImpl registration) {
41 listeners.remove(registration);
44 private class ListenerRegistrationImpl<P extends EventListener> //
45 extends AbstractObjectRegistration<P> //
46 implements ListenerRegistration<P> {
48 public ListenerRegistrationImpl(P instance) {
53 protected void removeRegistration() {
54 ListenerRegistry.this.remove(this);