blob: 3ff63a5e35b76001237097d643974c8c0d3eb1e2 [file] [log] [blame]
Thomas Vachuska42e8cce2015-07-29 19:25:18 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska42e8cce2015-07-29 19:25:18 -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 */
16package org.onosproject.event;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.osgi.service.component.annotations.Reference;
19import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070020
21/**
22 * Basis for components which need to export listener mechanism.
23 */
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070024public abstract class AbstractListenerManager<E extends Event, L extends EventListener<E>>
25 implements ListenerService<E, L> {
26
27 protected final ListenerRegistry<E, L> listenerRegistry = new ListenerRegistry<>();
28
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070030 protected EventDeliveryService eventDispatcher;
31
32 @Override
33 public void addListener(L listener) {
34 listenerRegistry.addListener(listener);
35 }
36
37 @Override
38 public void removeListener(L listener) {
39 listenerRegistry.removeListener(listener);
40 }
41
42
43 /**
44 * Safely posts the specified event to the local event dispatcher.
45 * If there is no event dispatcher or if the event is null, this method
46 * is a noop.
47 *
48 * @param event event to be posted; may be null
49 */
50 protected void post(E event) {
51 if (event != null && eventDispatcher != null) {
52 eventDispatcher.post(event);
53 }
54 }
55
56}