2 * Copyright (c) 2014 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.controller.md.sal.binding.compat;
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import com.google.common.collect.ImmutableMultimap;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Multimap;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.HashSet;
21 import java.util.stream.Collectors;
22 import org.opendaylight.yangtools.yang.binding.Notification;
25 * An immutable view of the current generation of listeners.
27 @Deprecated(forRemoval = true)
28 final class ListenerMapGeneration {
29 private static final int CACHE_MAX_ENTRIES = 1000;
32 * Constant map of notification type to subscribed listeners.
34 private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> typeToListeners;
37 * Dynamic cache of notification implementation to matching listeners. This cache loads entries based on
38 * the contents of the {@link #typeToListeners} map.
40 private final LoadingCache<Class<?>, Iterable<NotificationListenerRegistration<?>>> implementationToListeners =
41 CacheBuilder.newBuilder()
43 .maximumSize(CACHE_MAX_ENTRIES)
44 .build(new CacheLoader<Class<?>, Iterable<NotificationListenerRegistration<?>>>() {
46 public Iterable<NotificationListenerRegistration<?>> load(final Class<?> key) {
47 final Set<NotificationListenerRegistration<?>> regs = new HashSet<>();
49 for (final Class<?> type : getNotificationTypes(key)) {
50 @SuppressWarnings("unchecked")
51 final Collection<NotificationListenerRegistration<?>> l =
52 typeToListeners.get((Class<? extends Notification>) type);
58 return ImmutableSet.copyOf(regs);
62 ListenerMapGeneration() {
63 typeToListeners = ImmutableMultimap.of();
66 ListenerMapGeneration(final Multimap<Class<? extends Notification>,
67 NotificationListenerRegistration<?>> listeners) {
68 this.typeToListeners = ImmutableMultimap.copyOf(listeners);
72 * Current listeners. Exposed for creating the next generation.
74 * @return Current type-to-listener map.
76 Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> getListeners() {
77 return typeToListeners;
81 * Look up the listeners which need to see this notification delivered.
83 * @param notification Notification object
84 * @return Iterable of listeners, guaranteed to be nonnull.
86 public Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
87 // Safe to use, as our loader does not throw checked exceptions
88 return implementationToListeners.getUnchecked(notification.getClass());
91 public Iterable<Class<? extends Notification>> getKnownTypes() {
92 return typeToListeners.keySet();
95 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
96 justification = "https://github.com/spotbugs/spotbugs/issues/811")
97 private static Iterable<Class<?>> getNotificationTypes(final Class<?> cls) {
98 return Arrays.stream(cls.getInterfaces())
99 .filter(input -> !Notification.class.equals(input) && Notification.class.isAssignableFrom(input))
100 .collect(Collectors.toList());