4c31cc55268959ce8f4e4e5646cd3c0f46724a8b
[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.Collections;
14 import java.util.EventListener;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.function.Consumer;
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
31     private final Set<ListenerRegistration<? extends T>> listeners = ConcurrentHashMap.newKeySet();
32     // This conversion is known to be safe.
33     @SuppressWarnings({ "rawtypes", "unchecked" })
34     private final Set<ListenerRegistration<T>> unmodifiableView = (Set) Collections.unmodifiableSet(listeners);
35
36     private final String name;
37
38     private ListenerRegistry(final String name) {
39         this.name = name;
40     }
41
42     public static <T extends EventListener> @NonNull ListenerRegistry<T> create() {
43         return new ListenerRegistry<>(null);
44     }
45
46     public static <T extends EventListener> @NonNull ListenerRegistry<T> create(final @NonNull String name) {
47         return new ListenerRegistry<>(requireNonNull(name));
48     }
49
50     @Deprecated(forRemoval = true)
51     public @NonNull Set<? extends ListenerRegistration<? extends T>> getRegistrations() {
52         return unmodifiableView;
53     }
54
55     public void clear() {
56         listeners.stream().forEach(ListenerRegistration::close);
57     }
58
59     public boolean isEmpty() {
60         return listeners.isEmpty();
61     }
62
63     public Stream<? extends T> streamListeners() {
64         return listeners.stream().map(ListenerRegistration::getInstance);
65     }
66
67     public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
68         final ListenerRegistration<L> ret = new ListenerRegistrationImpl<>(listener, listeners::remove);
69         listeners.add(ret);
70         return ret;
71     }
72
73     @Override
74     public String toString() {
75         return MoreObjects.toStringHelper(this).omitNullValues()
76                 .add("name", name)
77                 .add("size", listeners.size())
78                 .toString();
79     }
80
81     private static final class ListenerRegistrationImpl<T extends EventListener>
82             extends AbstractListenerRegistration<T> {
83         private Consumer<ListenerRegistration<? super T>> removeCall;
84
85         ListenerRegistrationImpl(final T instance, final Consumer<ListenerRegistration<? super T>> removeCall) {
86             super(instance);
87             this.removeCall = requireNonNull(removeCall);
88         }
89
90         @Override
91         protected void removeRegistration() {
92             removeCall.accept(this);
93             // Do not retain reference to that state
94             removeCall = null;
95         }
96     }
97 }