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.sal.binding.impl;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
15 import org.opendaylight.yangtools.yang.binding.Notification;
17 import com.google.common.base.Predicate;
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.Iterables;
24 import com.google.common.collect.Multimap;
27 * An immutable view of the current generation of listeners.
29 final class ListenerMapGeneration {
30 private static final int CACHE_MAX_ENTRIES = 1000;
33 * Constant map of notification type to subscribed listeners.
35 private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> typeToListeners;
38 * Dynamic cache of notification implementation to matching listeners. This cache loads entries based on
39 * the contents of the {@link #typeToListeners} map.
41 private final LoadingCache<Class<?>, Iterable<NotificationListenerRegistration<?>>> implementationToListeners =
42 CacheBuilder.newBuilder()
44 .maximumSize(CACHE_MAX_ENTRIES)
45 .build(new CacheLoader<Class<?>, Iterable<NotificationListenerRegistration<?>>>() {
47 public Iterable<NotificationListenerRegistration<?>> load(final Class<?> key) {
48 final Set<NotificationListenerRegistration<?>> regs = new HashSet<>();
50 for (final Class<?> type : getNotificationTypes(key)) {
51 @SuppressWarnings("unchecked")
52 final Collection<NotificationListenerRegistration<?>> l = typeToListeners.get((Class<? extends Notification>) type);
58 return ImmutableSet.copyOf(regs);
62 ListenerMapGeneration() {
63 typeToListeners = ImmutableMultimap.of();
66 ListenerMapGeneration(final Multimap<Class<? extends Notification>, 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 Iterables.filter(Arrays.asList(ifaces), new Predicate<Class<?>>() {
98 public boolean apply(final Class<?> input) {
99 if (Notification.class.equals(input)) {
102 return Notification.class.isAssignableFrom(input);