blob: 5d76e0fc80873a07e8d2895db7064b100c48d69f [file] [log] [blame]
tom0755a362014-09-24 11:54:43 -07001package org.onlab.onos.store;
2
3import org.onlab.onos.event.Event;
4
tomf80c9722014-09-24 14:49:18 -07005import static com.google.common.base.Preconditions.checkState;
6
tom0755a362014-09-24 11:54:43 -07007/**
8 * Base implementation of a store.
9 */
10public class AbstractStore<E extends Event, D extends StoreDelegate<E>>
11 implements Store<E, D> {
12
13 protected D delegate;
14
15 @Override
16 public void setDelegate(D delegate) {
tomf80c9722014-09-24 14:49:18 -070017 checkState(this.delegate == null || this.delegate == delegate,
18 "Store delegate already set");
tom0755a362014-09-24 11:54:43 -070019 this.delegate = delegate;
20 }
21
22 @Override
tomf80c9722014-09-24 14:49:18 -070023 public void unsetDelegate(D delegate) {
24 if (this.delegate == delegate) {
25 this.delegate = null;
26 }
27 }
28
29 @Override
30 public boolean hasDelegate() {
31 return delegate != null;
tom0755a362014-09-24 11:54:43 -070032 }
33
34 /**
35 * Notifies the delegate with the specified event.
36 *
37 * @param event event to delegate
38 */
39 protected void notifyDelegate(E event) {
40 if (delegate != null) {
41 delegate.notify(event);
42 }
43 }
44}