Merge "TCP_Flag extension model additions for OFPXMC_NXM_1 class"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / GenerationalListenerMap.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
23 final class GenerationalListenerMap {
24     private final Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners =
25             HashMultimap.create();
26
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<?>>() {
30             @Override
31             public boolean apply(final Class<?> input) {
32                 if (Notification.class.equals(input)) {
33                     return false;
34                 }
35                 return Notification.class.isAssignableFrom(input);
36             }
37         });
38     }
39
40     synchronized Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
41         final Set<NotificationListenerRegistration<?>> toNotify = new HashSet<>();
42
43         for (final Class<?> type : getNotificationTypes(notification)) {
44             final Collection<NotificationListenerRegistration<?>> l = listeners.get((Class<? extends Notification>) type);
45             if (l != null) {
46                 toNotify.addAll(l);
47             }
48         }
49
50         return toNotify;
51     }
52
53     synchronized Iterable<Class<? extends Notification>> getKnownTypes() {
54         return ImmutableList.copyOf(listeners.keySet());
55     }
56
57     synchronized void addRegistrations(final NotificationListenerRegistration<?>... registrations) {
58         for (NotificationListenerRegistration<?> reg : registrations) {
59             listeners.put(reg.getType(), reg);
60         }
61     }
62
63     synchronized void removeRegistrations(final NotificationListenerRegistration<?>... registrations) {
64         for (NotificationListenerRegistration<?> reg : registrations) {
65             listeners.remove(reg.getType(), reg);
66         }
67     }
68 }