2 * Copyright (c) 2014 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.controller.md.sal.common.impl;
10 import java.util.Collections;
11 import java.util.EventListener;
12 import java.util.HashSet;
14 import java.util.concurrent.ConcurrentHashMap;
16 import static com.google.common.base.Preconditions.*;
18 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 public class ListenerRegistry<T extends EventListener> {
23 final Set<ListenerRegistration<T>> listeners;
24 final Set<ListenerRegistration<T>> unmodifiableView;
26 public ListenerRegistry() {
27 listeners = new HashSet<>();
28 unmodifiableView = Collections.unmodifiableSet(listeners);
31 public Iterable<ListenerRegistration<T>> getListeners() {
32 return unmodifiableView;
36 public ListenerRegistration<T> register(T listener) {
37 checkNotNull(listener, "Listener should not be null.");
38 ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
44 @SuppressWarnings("rawtypes")
45 private void remove(ListenerRegistrationImpl registration) {
46 listeners.remove(registration);
49 private class ListenerRegistrationImpl<P extends EventListener> //
50 extends AbstractObjectRegistration<P> //
51 implements ListenerRegistration<P> {
53 public ListenerRegistrationImpl(P instance) {
58 protected void removeRegistration() {
59 ListenerRegistry.this.remove(this);