2 * Copyright (c) 2013 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.collect.HashMultimap;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Iterables;
21 import com.google.common.collect.Multimap;
22 import com.google.common.collect.Multimaps;
24 final class NotificationListenerMap {
25 private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners =
26 Multimaps.synchronizedSetMultimap(HashMultimap.<Class<? extends Notification>, NotificationListenerRegistration<?>>create());
28 private static Iterable<Class<?>> getNotificationTypes(final Notification notification) {
29 final Class<?>[] ifaces = notification.getClass().getInterfaces();
30 return Iterables.filter(Arrays.asList(ifaces), new Predicate<Class<?>>() {
32 public boolean apply(final Class<?> input) {
33 if (Notification.class.equals(input)) {
36 return Notification.class.isAssignableFrom(input);
41 Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
42 final Set<NotificationListenerRegistration<?>> toNotify = new HashSet<>();
44 for (final Class<?> type : getNotificationTypes(notification)) {
45 final Collection<NotificationListenerRegistration<?>> l = listeners.get((Class<? extends Notification>) type);
54 Iterable<Class<? extends Notification>> getKnownTypes() {
55 return ImmutableList.copyOf(listeners.keySet());
58 void addRegistrations(final NotificationListenerRegistration<?>... registrations) {
59 for (NotificationListenerRegistration<?> reg : registrations) {
60 listeners.put(reg.getType(), reg);
64 void removeRegistrations(final NotificationListenerRegistration<?>... registrations) {
65 for (NotificationListenerRegistration<?> reg : registrations) {
66 listeners.remove(reg.getType(), reg);