blob: b81ab6fb6c378f33f5cad3e465b946da6f1014b4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store;
tom0755a362014-09-24 11:54:43 -070017
Madan Jampani255a58b2014-10-09 12:08:20 -070018import java.util.List;
19
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.event.Event;
tom0755a362014-09-24 11:54:43 -070021
tomf80c9722014-09-24 14:49:18 -070022import static com.google.common.base.Preconditions.checkState;
23
tom0755a362014-09-24 11:54:43 -070024/**
25 * Base implementation of a store.
26 */
27public 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) {
tomf80c9722014-09-24 14:49:18 -070034 checkState(this.delegate == null || this.delegate == delegate,
35 "Store delegate already set");
tom0755a362014-09-24 11:54:43 -070036 this.delegate = delegate;
37 }
38
39 @Override
tomf80c9722014-09-24 14:49:18 -070040 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;
tom0755a362014-09-24 11:54:43 -070049 }
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 Jampani255a58b2014-10-09 12:08:20 -070061
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 }
tom0755a362014-09-24 11:54:43 -070072}