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