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 static com.google.common.base.Preconditions.checkNotNull;
12 import java.util.Collections;
13 import java.util.EventListener;
14 import java.util.HashSet;
17 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 public class ListenerRegistry<T extends EventListener> {
22 final Set<ListenerRegistration<T>> listeners;
23 final Set<ListenerRegistration<T>> unmodifiableView;
25 public ListenerRegistry() {
26 listeners = new HashSet<>();
27 unmodifiableView = Collections.unmodifiableSet(listeners);
30 public Iterable<ListenerRegistration<T>> getListeners() {
31 return unmodifiableView;
35 public ListenerRegistration<T> register(T listener) {
36 checkNotNull(listener, "Listener should not be null.");
37 ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
43 @SuppressWarnings("rawtypes")
44 private void remove(ListenerRegistrationImpl registration) {
45 listeners.remove(registration);
48 private class ListenerRegistrationImpl<P extends EventListener> //
49 extends AbstractObjectRegistration<P> //
50 implements ListenerRegistration<P> {
52 public ListenerRegistrationImpl(P instance) {
57 protected void removeRegistration() {
58 ListenerRegistry.this.remove(this);