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.concurrent.atomic.AtomicReference;
12 import javax.annotation.concurrent.GuardedBy;
14 import org.opendaylight.yangtools.yang.binding.Notification;
16 import com.google.common.collect.HashMultimap;
17 import com.google.common.collect.Multimap;
20 * A multi-generation, isolated notification type to listener map.
22 final class GenerationalListenerMap {
23 private final AtomicReference<ListenerMapGeneration> current = new AtomicReference<>(new ListenerMapGeneration());
25 Iterable<NotificationListenerRegistration<?>> listenersFor(final Notification notification) {
26 return current.get().listenersFor(notification);
29 Iterable<Class<? extends Notification>> getKnownTypes() {
30 // Note: this relies on current having immutable listeners
31 return current.get().getListeners().keySet();
35 private Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> mutableListeners() {
36 return HashMultimap.create(current.get().getListeners());
39 synchronized void addRegistrations(final NotificationListenerRegistration<?>... registrations) {
40 Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners =
43 for (NotificationListenerRegistration<?> reg : registrations) {
44 listeners.put(reg.getType(), reg);
47 current.set(new ListenerMapGeneration(listeners));
50 synchronized void removeRegistrations(final NotificationListenerRegistration<?>... registrations) {
51 Multimap<Class<? extends Notification>, NotificationListenerRegistration<?>> listeners =
54 for (NotificationListenerRegistration<?> reg : registrations) {
55 listeners.remove(reg.getType(), reg);
58 current.set(new ListenerMapGeneration(listeners));