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 java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
15 import java.util.stream.Collectors;
16 import org.opendaylight.yangtools.yang.binding.Notification;
18 import com.google.common.cache.CacheBuilder;
19 import com.google.common.cache.CacheLoader;
20 import com.google.common.cache.LoadingCache;
21 import com.google.common.collect.ImmutableMultimap;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Multimap;
26 * An immutable view of the current generation of listeners.
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 = typeToListeners.get((Class<? extends Notification>) type);
57 return ImmutableSet.copyOf(regs);
61 ListenerMapGeneration() {
62 typeToListeners = ImmutableMultimap.of();
65 ListenerMapGeneration(final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners) {
66 this.typeToListeners = ImmutableMultimap.copyOf(listeners);
70 * Current listeners. Exposed for creating the next generation.
72 * @return Current type-to-listener map.
74 Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> getListeners() {
75 return typeToListeners;
79 * Look up the listeners which need to see this notification delivered.
81 * @param notification Notification object
82 * @return Iterable of listeners, guaranteed to be nonnull.
84 public Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
85 // Safe to use, as our loader does not throw checked exceptions
86 return implementationToListeners.getUnchecked(notification.getClass());
89 public Iterable<Class<? extends Notification>> getKnownTypes() {
90 return typeToListeners.keySet();
93 private static Iterable<Class<?>> getNotificationTypes(final Class<?> cls) {
94 final Class<?>[] ifaces = cls.getInterfaces();
95 return Arrays.stream(ifaces)
96 .filter(input -> !Notification.class.equals(input) && Notification.class.isAssignableFrom(input))
97 .collect(Collectors.toList());