HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [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 | */ |
| 16 | package org.onosproject.event; |
| 17 | |
Yuta HIGUCHI | d90dbc9 | 2016-08-04 18:35:21 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 20 | import java.util.ArrayList; |
| 21 | import java.util.List; |
| 22 | |
| 23 | import javax.annotation.concurrent.NotThreadSafe; |
| 24 | |
| 25 | import org.apache.commons.lang3.tuple.Pair; |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 26 | import com.google.common.annotations.Beta; |
| 27 | import com.google.common.collect.Lists; |
| 28 | |
| 29 | /** |
| 30 | * Utility to keeps track of registered Listeners. |
| 31 | * <p> |
| 32 | * Usage Example: |
| 33 | * <pre> |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 34 | 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 Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 50 | * </pre> |
| 51 | */ |
| 52 | @Beta |
| 53 | @NotThreadSafe |
| 54 | public 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 Li | 7f256f5 | 2016-01-24 15:08:05 -0800 | [diff] [blame] | 62 | * @param <E> event |
| 63 | * @param <L> listener |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 64 | * @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 HIGUCHI | d90dbc9 | 2016-08-04 18:35:21 -0700 | [diff] [blame] | 70 | |
| 71 | checkNotNull(service); |
| 72 | checkNotNull(listener); |
HIGUCHI Yuta | 9092db8 | 2016-01-03 18:45:01 -0800 | [diff] [blame] | 73 | 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 | } |