4f95770df0717b065da64f4ee77955e44b02d926
[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 org.opendaylight.mdsal.dom.api.DOMNotification;
11 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import com.lmax.disruptor.EventFactory;
17 import java.util.Collection;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
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     public static final EventFactory<DOMNotificationRouterEvent> FACTORY = new EventFactory<DOMNotificationRouterEvent>() {
26         @Override
27         public DOMNotificationRouterEvent newInstance() {
28             return new DOMNotificationRouterEvent();
29         }
30     };
31
32     private Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers;
33     private DOMNotification notification;
34     private SettableFuture<Void> future;
35
36     private DOMNotificationRouterEvent() {
37         // Hidden on purpose, initialized in initialize()
38     }
39
40     ListenableFuture<Void> initialize(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
41         this.notification = Preconditions.checkNotNull(notification);
42         this.subscribers = Preconditions.checkNotNull(subscribers);
43         this.future = SettableFuture.create();
44         return this.future;
45     }
46
47     void deliverNotification() {
48         for (ListenerRegistration<? extends DOMNotificationListener> r : subscribers) {
49             final DOMNotificationListener l = r.getInstance();
50             if (l != null) {
51                 l.onNotification(notification);
52             }
53         }
54     }
55
56     void setFuture() {
57         future.set(null);
58     }
59
60 }