blob: b42cdc2868a60fbca610f5b26d503efe29e3545d [file] [log] [blame]
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
HIGUCHI Yuta9092db82016-01-03 18:45:01 -08003 *
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
Yuta HIGUCHId90dbc92016-08-04 18:35:21 -070018import static com.google.common.base.Preconditions.checkNotNull;
19
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080020import java.util.ArrayList;
21import java.util.List;
22
23import javax.annotation.concurrent.NotThreadSafe;
24
25import org.apache.commons.lang3.tuple.Pair;
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080026import com.google.common.annotations.Beta;
27import com.google.common.collect.Lists;
28
29/**
30 * Utility to keeps track of registered Listeners.
31 * <p>
32 * Usage Example:
33 * <pre>
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080034 private ListenerTracker listeners;
35
36 {@code @Activate}
37 protected void activate() {
38 listeners = new ListenerTracker();
39 listeners.addListener(mastershipService, new InternalMastershipListener())
40 .addListener(deviceService, new InternalDeviceListener())
41 .addListener(linkService, new InternalLinkListener())
42 .addListener(topologyService, new InternalTopologyListener())
43 .addListener(hostService, new InternalHostListener());
44 }
45
46 {@code @Deactivate}
47 protected void deactivate() {
48 listeners.removeListeners();
49 }
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080050 * </pre>
51 */
52@Beta
53@NotThreadSafe
54public class ListenerTracker {
55
56 @SuppressWarnings("rawtypes")
57 private List<Pair<ListenerService, EventListener>> listeners = new ArrayList<>();
58
59 /**
60 * Adds {@link EventListener} to specified {@link ListenerService}.
61 *
Jian Li7f256f52016-01-24 15:08:05 -080062 * @param <E> event
63 * @param <L> listener
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080064 * @param service {@link ListenerService}
65 * @param listener {@link EventListener}
66 * @return self
67 */
68 public <E extends Event<?, ?>, L extends EventListener<E>>
69 ListenerTracker addListener(ListenerService<E, L> service, L listener) {
Yuta HIGUCHId90dbc92016-08-04 18:35:21 -070070
71 checkNotNull(service);
72 checkNotNull(listener);
HIGUCHI Yuta9092db82016-01-03 18:45:01 -080073 service.addListener(listener);
74 listeners.add(Pair.of(service, listener));
75 return this;
76 }
77
78 /**
79 * Removes all listeners in reverse order they have been registered.
80 */
81 public void removeListeners() {
82 Lists.reverse(listeners)
83 .forEach(r -> r.getLeft().removeListener(r.getRight()));
84 listeners.clear();
85 }
86}