import static java.util.Objects.requireNonNull;
import com.google.common.base.MoreObjects;
-import java.util.Collections;
import java.util.EventListener;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
* @param <T> Type of listeners this registry handles
*/
public final class ListenerRegistry<T extends EventListener> implements Mutable {
-
private final Set<ListenerRegistration<? extends T>> listeners = ConcurrentHashMap.newKeySet();
- // This conversion is known to be safe.
- @SuppressWarnings({ "rawtypes", "unchecked" })
- private final Set<ListenerRegistration<T>> unmodifiableView = (Set) Collections.unmodifiableSet(listeners);
-
private final String name;
private ListenerRegistry(final String name) {
return new ListenerRegistry<>(requireNonNull(name));
}
- @Deprecated(forRemoval = true)
- public @NonNull Set<? extends ListenerRegistration<? extends T>> getRegistrations() {
- return unmodifiableView;
- }
-
public void clear() {
listeners.stream().forEach(ListenerRegistration::close);
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import com.google.common.collect.ImmutableSet;
import java.util.EventListener;
import org.junit.Before;
import org.junit.Test;
@Test
public void testGetListenersMethod() {
- assertEquals("Listener registry should have any listeners.", ImmutableSet.of(), registry.getRegistrations());
+ assertEquals("Listener registry should not have any listeners.", 0, registry.streamListeners().count());
}
@Test