2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.concepts.util;
11 import java.util.Collections;
12 import java.util.EventListener;
14 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 * @deprecated Use {@link org.opendaylight.yangtools.util.ListenerRegistry} instead.
25 public class ListenerRegistry<T extends EventListener> implements Iterable<ListenerRegistration<T>> {
27 private final ConcurrentHashMap<ListenerRegistration<? extends T>,ListenerRegistration<? extends T>> listeners;
28 final Set<ListenerRegistration<T>> unmodifiableView;
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;
39 public Iterable<ListenerRegistration<T>> getListeners() {
40 return unmodifiableView;
43 public ListenerRegistration<T> register(final T listener) {
44 if (listener == null) {
45 throw new IllegalArgumentException("Listener should not be null.");
47 ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
48 listeners.put(ret,ret);
52 public <L extends T> ListenerRegistration<L> registerWithType(final L listener) {
53 ListenerRegistrationImpl<L> ret = new ListenerRegistrationImpl<L>(listener);
54 listeners.put(ret,ret);
59 public java.util.Iterator<ListenerRegistration<T>> iterator() {
60 return unmodifiableView.iterator();
63 @SuppressWarnings("rawtypes")
64 private void remove(final ListenerRegistrationImpl registration) {
65 listeners.remove(registration);
68 private class ListenerRegistrationImpl<P extends EventListener> extends AbstractObjectRegistration<P> implements ListenerRegistration<P> {
70 public ListenerRegistrationImpl(final P instance) {
75 protected void removeRegistration() {
76 ListenerRegistry.this.remove(this);
80 public static <T extends EventListener> ListenerRegistry<T> create() {
81 return new ListenerRegistry<>();