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