BUG-1120: introduce NotificationListenerMap
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / NotificationListenerMap.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.impl;
9
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.opendaylight.yangtools.yang.binding.Notification;
16
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;
23
24 final class NotificationListenerMap {
25     private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners =
26             Multimaps.synchronizedSetMultimap(HashMultimap.<Class<? extends Notification>, NotificationListenerRegistration<?>>create());
27
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<?>>() {
31             @Override
32             public boolean apply(final Class<?> input) {
33                 if (Notification.class.equals(input)) {
34                     return false;
35                 }
36                 return Notification.class.isAssignableFrom(input);
37             }
38         });
39     }
40
41     Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
42         final Set<NotificationListenerRegistration<?>> toNotify = new HashSet<>();
43
44         for (final Class<?> type : getNotificationTypes(notification)) {
45             final Collection<NotificationListenerRegistration<?>> l = listeners.get((Class<? extends Notification>) type);
46             if (l != null) {
47                 toNotify.addAll(l);
48             }
49         }
50
51         return toNotify;
52     }
53
54     Iterable<Class<? extends Notification>> getKnownTypes() {
55         return ImmutableList.copyOf(listeners.keySet());
56     }
57
58     void addRegistrations(final NotificationListenerRegistration<?>... registrations) {
59         for (NotificationListenerRegistration<?> reg : registrations) {
60             listeners.put(reg.getType(), reg);
61         }
62     }
63
64     void removeRegistrations(final NotificationListenerRegistration<?>... registrations) {
65         for (NotificationListenerRegistration<?> reg : registrations) {
66             listeners.remove(reg.getType(), reg);
67         }
68     }
69
70 }