8743fc90d72f038853794a20eb414b7c1a370f81
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMNotificationRouterEvent.java
1 /*
2  * Copyright (c) 2014 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.mdsal.dom.broker;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import com.lmax.disruptor.EventFactory;
15 import java.util.Collection;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
18 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
19
20 /**
21  * A single notification event in the disruptor ringbuffer. These objects are reused,
22  * so they do have mutable state.
23  */
24 final class DOMNotificationRouterEvent {
25     static final EventFactory<DOMNotificationRouterEvent> FACTORY = DOMNotificationRouterEvent::new;
26
27     private Collection<AbstractListenerRegistration<? extends DOMNotificationListener>> subscribers;
28     private DOMNotification notification;
29     private SettableFuture<Void> future;
30
31     private DOMNotificationRouterEvent() {
32         // Hidden on purpose, initialized in initialize()
33     }
34
35     @SuppressWarnings("checkstyle:hiddenField")
36     ListenableFuture<Void> initialize(final DOMNotification notification,
37             final Collection<AbstractListenerRegistration<? extends DOMNotificationListener>> subscribers) {
38         this.notification = requireNonNull(notification);
39         this.subscribers = requireNonNull(subscribers);
40         this.future = SettableFuture.create();
41         return this.future;
42     }
43
44     void deliverNotification() {
45         for (AbstractListenerRegistration<? extends DOMNotificationListener> r : subscribers) {
46             if (r.notClosed()) {
47                 r.getInstance().onNotification(notification);
48             }
49         }
50     }
51
52     void setFuture() {
53         future.set(null);
54     }
55
56 }