tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store; |
| 2 | |
| 3 | import org.onlab.onos.event.Event; |
| 4 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 5 | import static com.google.common.base.Preconditions.checkState; |
| 6 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 7 | /** |
| 8 | * Base implementation of a store. |
| 9 | */ |
| 10 | public 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) { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 17 | checkState(this.delegate == null || this.delegate == delegate, |
| 18 | "Store delegate already set"); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 19 | this.delegate = delegate; |
| 20 | } |
| 21 | |
| 22 | @Override |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame^] | 23 | 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; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 32 | } |
| 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 | } |