Merge "Fix typo in match types yang model"
[controller.git] / opendaylight / md-sal / sal-common-impl / src / main / java / org / opendaylight / controller / md / sal / common / impl / ListenerRegistry.java
1 /*
2  * Copyright (c) 2014 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.controller.md.sal.common.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import java.util.Collections;
13 import java.util.EventListener;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19
20 public class ListenerRegistry<T extends EventListener> {
21
22     final Set<ListenerRegistration<T>> listeners;
23     final Set<ListenerRegistration<T>> unmodifiableView;
24
25     public ListenerRegistry() {
26         listeners = new HashSet<>();
27         unmodifiableView = Collections.unmodifiableSet(listeners);
28     }
29
30     public Iterable<ListenerRegistration<T>> getListeners() {
31         return unmodifiableView;
32     }
33
34
35     public ListenerRegistration<T> register(T listener) {
36         checkNotNull(listener, "Listener should not be null.");
37         ListenerRegistrationImpl<T> ret = new ListenerRegistrationImpl<T>(listener);
38         listeners.add(ret);
39         return ret;
40     }
41
42
43     @SuppressWarnings("rawtypes")
44     private void remove(ListenerRegistrationImpl registration) {
45         listeners.remove(registration);
46     }
47
48     private class ListenerRegistrationImpl<P extends EventListener> //
49             extends AbstractObjectRegistration<P> //
50             implements ListenerRegistration<P> {
51
52         public ListenerRegistrationImpl(P instance) {
53             super(instance);
54         }
55
56         @Override
57         protected void removeRegistration() {
58             ListenerRegistry.this.remove(this);
59         }
60     }
61 }