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.
26 final class ListenerMapGeneration {
27 private static final int CACHE_MAX_ENTRIES = 1000;
30 * Constant map of notification type to subscribed listeners.
32 private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> typeToListeners;
35 * Dynamic cache of notification implementation to matching listeners. This cache loads entries based on
36 * the contents of the {@link #typeToListeners} map.
38 private final LoadingCache<Class<?>, Iterable<NotificationListenerRegistration<?>>> implementationToListeners =
39 CacheBuilder.newBuilder()
41 .maximumSize(CACHE_MAX_ENTRIES)
42 .build(new CacheLoader<Class<?>, Iterable<NotificationListenerRegistration<?>>>() {
44 public Iterable<NotificationListenerRegistration<?>> load(final Class<?> key) {
45 final Set<NotificationListenerRegistration<?>> regs = new HashSet<>();
47 for (final Class<?> type : getNotificationTypes(key)) {
48 @SuppressWarnings("unchecked")
49 final Collection<NotificationListenerRegistration<?>> l =
50 typeToListeners.get((Class<? extends Notification>) type);
56 return ImmutableSet.copyOf(regs);
60 ListenerMapGeneration() {
61 typeToListeners = ImmutableMultimap.of();
64 ListenerMapGeneration(final Multimap<Class<? extends Notification>,
65 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());