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;
23 final class NotificationListenerMap {
24 private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners =
25 HashMultimap.create();
27 private static Iterable<Class<?>> getNotificationTypes(final Notification notification) {
28 final Class<?>[] ifaces = notification.getClass().getInterfaces();
29 return Iterables.filter(Arrays.asList(ifaces), new Predicate<Class<?>>() {
31 public boolean apply(final Class<?> input) {
32 if (Notification.class.equals(input)) {
35 return Notification.class.isAssignableFrom(input);
40 synchronized Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
41 final Set<NotificationListenerRegistration<?>> toNotify = new HashSet<>();
43 for (final Class<?> type : getNotificationTypes(notification)) {
44 final Collection<NotificationListenerRegistration<?>> l = listeners.get((Class<? extends Notification>) type);
53 synchronized Iterable<Class<? extends Notification>> getKnownTypes() {
54 return ImmutableList.copyOf(listeners.keySet());
57 synchronized void addRegistrations(final NotificationListenerRegistration<?>... registrations) {
58 for (NotificationListenerRegistration<?> reg : registrations) {
59 listeners.put(reg.getType(), reg);
63 synchronized void removeRegistrations(final NotificationListenerRegistration<?>... registrations) {
64 for (NotificationListenerRegistration<?> reg : registrations) {
65 listeners.remove(reg.getType(), reg);