Filter registered listeners
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ListenerRegistry.java
1 /*
2  * Copyright (c) 2013 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.yangtools.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.EventListener;
16 import java.util.Set;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.concepts.Mutable;
23
24 /**
25  * A registry of EventListeners, maintaining a set of registrations. This class is thread-safe.
26  *
27  * @param <T> Type of listeners this registry handles
28  */
29 public final class ListenerRegistry<T extends EventListener> implements Mutable {
30     private final Set<RegistrationImpl<? extends T>> listeners = ConcurrentHashMap.newKeySet();
31     // This conversion is known to be safe.
32     @SuppressWarnings({ "rawtypes", "unchecked" })
33     private final Set<ListenerRegistration<T>> unmodifiableView = (Set) Collections.unmodifiableSet(listeners);
34
35     private final String name;
36
37     private ListenerRegistry(final String name) {
38         this.name = name;
39     }
40
41     public static <T extends EventListener> @NonNull ListenerRegistry<T> create() {
42         return new ListenerRegistry<>(null);
43     }
44
45     public static <T extends EventListener> @NonNull ListenerRegistry<T> create(final @NonNull String name) {
46         return new ListenerRegistry<>(requireNonNull(name));
47     }
48
49     @Deprecated(forRemoval = true)
50     public @NonNull Set<? extends ListenerRegistration<? extends T>> getRegistrations() {
51         return unmodifiableView;
52     }
53
54     public void clear() {
55         listeners.stream().forEach(RegistrationImpl::close);
56     }
57
58     public boolean isEmpty() {
59         return listeners.isEmpty();
60     }
61
62     public Stream<? extends T> streamListeners() {
63         return listeners.stream().filter(RegistrationImpl::notClosed).map(RegistrationImpl::getInstance);
64     }
65
66     public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
67         final RegistrationImpl<L> ret = new RegistrationImpl<>(listener, listeners);
68         listeners.add(ret);
69         return ret;
70     }
71
72     @Override
73     public String toString() {
74         return MoreObjects.toStringHelper(this).omitNullValues()
75                 .add("name", name)
76                 .add("size", listeners.size())
77                 .toString();
78     }
79
80     private static final class RegistrationImpl<T extends EventListener> extends AbstractListenerRegistration<T> {
81         private Collection<?> removeFrom;
82
83         RegistrationImpl(final T instance, final Collection<?> removeFrom) {
84             super(instance);
85             this.removeFrom = requireNonNull(removeFrom);
86         }
87
88         @Override
89         protected void removeRegistration() {
90             removeFrom.remove(this);
91             // Do not retain reference to that state
92             removeFrom = null;
93         }
94     }
95 }