2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import java.util.Collection;
14 import java.util.EventListener;
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;
24 * A registry of EventListeners, maintaining a set of registrations. This class is thread-safe.
26 * @param <T> Type of listeners this registry handles
27 * @deprecated Use {@link ObjectRegistry} instead
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;
34 private ListenerRegistry(final String name) {
38 public static <T extends EventListener> @NonNull ListenerRegistry<T> create() {
39 return new ListenerRegistry<>(null);
42 public static <T extends EventListener> @NonNull ListenerRegistry<T> create(final @NonNull String name) {
43 return new ListenerRegistry<>(requireNonNull(name));
47 listeners.stream().forEach(RegistrationImpl::close);
50 public boolean isEmpty() {
51 return listeners.isEmpty();
54 public Stream<? extends T> streamListeners() {
55 return listeners.stream().filter(RegistrationImpl::notClosed).map(RegistrationImpl::getInstance);
58 public <L extends T> @NonNull ListenerRegistration<L> register(final L listener) {
59 final RegistrationImpl<L> ret = new RegistrationImpl<>(listener, listeners);
65 public String toString() {
66 return MoreObjects.toStringHelper(this).omitNullValues()
68 .add("size", listeners.size())
72 private static final class RegistrationImpl<T extends EventListener> extends AbstractListenerRegistration<T> {
73 private Collection<?> removeFrom;
75 RegistrationImpl(final T instance, final Collection<?> removeFrom) {
77 this.removeFrom = requireNonNull(removeFrom);
81 protected void removeRegistration() {
82 removeFrom.remove(this);
83 // Do not retain reference to that state