Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 18 | import java.util.List; |
| 19 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.event.Event; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 21 | |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Base implementation of a store. |
| 26 | */ |
| 27 | public class AbstractStore<E extends Event, D extends StoreDelegate<E>> |
| 28 | implements Store<E, D> { |
| 29 | |
| 30 | protected D delegate; |
| 31 | |
| 32 | @Override |
| 33 | public void setDelegate(D delegate) { |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 34 | checkState(this.delegate == null || this.delegate == delegate, |
| 35 | "Store delegate already set"); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 36 | this.delegate = delegate; |
| 37 | } |
| 38 | |
| 39 | @Override |
tom | f80c972 | 2014-09-24 14:49:18 -0700 | [diff] [blame] | 40 | public void unsetDelegate(D delegate) { |
| 41 | if (this.delegate == delegate) { |
| 42 | this.delegate = null; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean hasDelegate() { |
| 48 | return delegate != null; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Notifies the delegate with the specified event. |
| 53 | * |
| 54 | * @param event event to delegate |
| 55 | */ |
| 56 | protected void notifyDelegate(E event) { |
| 57 | if (delegate != null) { |
| 58 | delegate.notify(event); |
| 59 | } |
| 60 | } |
Madan Jampani | 255a58b | 2014-10-09 12:08:20 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Notifies the delegate with the specified list of events. |
| 64 | * |
| 65 | * @param events list of events to delegate |
| 66 | */ |
| 67 | protected void notifyDelegate(List<E> events) { |
| 68 | for (E event: events) { |
| 69 | notifyDelegate(event); |
| 70 | } |
| 71 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 72 | } |