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 java.util.Arrays;
17 import java.util.Collection;
18 import java.util.HashSet;
20 import java.util.stream.Collectors;
21 import org.opendaylight.yangtools.yang.binding.Notification;
24 * An immutable view of the current generation of listeners.
27 final class ListenerMapGeneration {
28 private static final int CACHE_MAX_ENTRIES = 1000;
31 * Constant map of notification type to subscribed listeners.
33 private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> typeToListeners;
36 * Dynamic cache of notification implementation to matching listeners. This cache loads entries based on
37 * the contents of the {@link #typeToListeners} map.
39 private final LoadingCache<Class<?>, Iterable<NotificationListenerRegistration<?>>> implementationToListeners =
40 CacheBuilder.newBuilder()
42 .maximumSize(CACHE_MAX_ENTRIES)
43 .build(new CacheLoader<Class<?>, Iterable<NotificationListenerRegistration<?>>>() {
45 public Iterable<NotificationListenerRegistration<?>> load(final Class<?> key) {
46 final Set<NotificationListenerRegistration<?>> regs = new HashSet<>();
48 for (final Class<?> type : getNotificationTypes(key)) {
49 @SuppressWarnings("unchecked")
50 final Collection<NotificationListenerRegistration<?>> l =
51 typeToListeners.get((Class<? extends Notification>) type);
57 return ImmutableSet.copyOf(regs);
61 ListenerMapGeneration() {
62 typeToListeners = ImmutableMultimap.of();
65 ListenerMapGeneration(final Multimap<Class<? extends Notification>,
66 NotificationListenerRegistration<?>> listeners) {
67 this.typeToListeners = ImmutableMultimap.copyOf(listeners);
71 * Current listeners. Exposed for creating the next generation.
73 * @return Current type-to-listener map.
75 Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> getListeners() {
76 return typeToListeners;
80 * Look up the listeners which need to see this notification delivered.
82 * @param notification Notification object
83 * @return Iterable of listeners, guaranteed to be nonnull.
85 public Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
86 // Safe to use, as our loader does not throw checked exceptions
87 return implementationToListeners.getUnchecked(notification.getClass());
90 public Iterable<Class<? extends Notification>> getKnownTypes() {
91 return typeToListeners.keySet();
94 private static Iterable<Class<?>> getNotificationTypes(final Class<?> cls) {
95 final Class<?>[] ifaces = cls.getInterfaces();
96 return Arrays.stream(ifaces)
97 .filter(input -> !Notification.class.equals(input) && Notification.class.isAssignableFrom(input))
98 .collect(Collectors.toList());