blob: e7668d0340bec9e17b8a04ceabd2663376d59e46 [file] [log] [blame]
tom0755a362014-09-24 11:54:43 -07001package org.onlab.onos.store;
2
Madan Jampani255a58b2014-10-09 12:08:20 -07003import java.util.List;
4
tom0755a362014-09-24 11:54:43 -07005import org.onlab.onos.event.Event;
6
tomf80c9722014-09-24 14:49:18 -07007import static com.google.common.base.Preconditions.checkState;
8
tom0755a362014-09-24 11:54:43 -07009/**
10 * Base implementation of a store.
11 */
12public class AbstractStore<E extends Event, D extends StoreDelegate<E>>
13 implements Store<E, D> {
14
15 protected D delegate;
16
17 @Override
18 public void setDelegate(D delegate) {
tomf80c9722014-09-24 14:49:18 -070019 checkState(this.delegate == null || this.delegate == delegate,
20 "Store delegate already set");
tom0755a362014-09-24 11:54:43 -070021 this.delegate = delegate;
22 }
23
24 @Override
tomf80c9722014-09-24 14:49:18 -070025 public void unsetDelegate(D delegate) {
26 if (this.delegate == delegate) {
27 this.delegate = null;
28 }
29 }
30
31 @Override
32 public boolean hasDelegate() {
33 return delegate != null;
tom0755a362014-09-24 11:54:43 -070034 }
35
36 /**
37 * Notifies the delegate with the specified event.
38 *
39 * @param event event to delegate
40 */
41 protected void notifyDelegate(E event) {
42 if (delegate != null) {
43 delegate.notify(event);
44 }
45 }
Madan Jampani255a58b2014-10-09 12:08:20 -070046
47 /**
48 * Notifies the delegate with the specified list of events.
49 *
50 * @param events list of events to delegate
51 */
52 protected void notifyDelegate(List<E> events) {
53 for (E event: events) {
54 notifyDelegate(event);
55 }
56 }
tom0755a362014-09-24 11:54:43 -070057}