Specialize JSONCodec to JSONValueWriter
[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  * @deprecated Use {@link ObjectRegistry} instead
28  */
29 @Deprecated(since = "12.0.0", forRemoval = true)
30 public final class ListenerRegistry<T extends EventListener> implements Mutable {
31     private final Set<RegistrationImpl<? extends T>> listeners = ConcurrentHashMap.newKeySet();
32     private final String name;
33
34     private ListenerRegistry(final String name) {
35         this.name = name;
36     }
37
38     public static <T extends EventListener> @NonNull ListenerRegistry<T> create() {
39         return new ListenerRegistry<>(null);
40     }
41
42     public static <T extends EventListener> @NonNull ListenerRegistry<T> create(final @NonNull String name) {
43         return new ListenerRegistry<>(requireNonNull(name));
44     }
45
46     public void clear() {
47         listeners.stream().forEach(RegistrationImpl::close);
48     }
49
50     public boolean isEmpty() {
51         return listeners.isEmpty();
52     }
53
54     public Stream<? extends T> streamListeners() {
55         return listeners.stream().filter(RegistrationImpl::notClosed).map(RegistrationImpl::getInstance);
56     }
57
58     public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
59         final RegistrationImpl<L> ret = new RegistrationImpl<>(listener, listeners);
60         listeners.add(ret);
61         return ret;
62     }
63
64     @Override
65     public String toString() {
66         return MoreObjects.toStringHelper(this).omitNullValues()
67                 .add("name", name)
68                 .add("size", listeners.size())
69                 .toString();
70     }
71
72     private static final class RegistrationImpl<T extends EventListener> extends AbstractListenerRegistration<T> {
73         private Collection<?> removeFrom;
74
75         RegistrationImpl(final T instance, final Collection<?> removeFrom) {
76             super(instance);
77             this.removeFrom = requireNonNull(removeFrom);
78         }
79
80         @Override
81         protected void removeRegistration() {
82             removeFrom.remove(this);
83             // Do not retain reference to that state
84             removeFrom = null;
85         }
86     }
87 }