Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
EventSupport |
|
| 2.75;2.75 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.editor.policy.gui.event; | |
11 | ||
12 | import java.util.ArrayList; | |
13 | import java.util.List; | |
14 | ||
15 | /** | |
16 | * Support class to manage listeners and fire events. | |
17 | * @param <T> the type of event listeners supported. | |
18 | * @since 0.1.0 | |
19 | */ | |
20 | public class EventSupport <T extends EventListener> { | |
21 | ||
22 | //---- State | |
23 | ||
24 | /** | |
25 | * The listeners of this support class. | |
26 | * @since 0.1.0 | |
27 | */ | |
28 | private final List<T> listeners; | |
29 | ||
30 | //---- Constructors | |
31 | ||
32 | /** | |
33 | * Creates a new, empty event support. | |
34 | * @since 0.1.0 | |
35 | */ | |
36 | 0 | public EventSupport () { |
37 | 0 | this.listeners = new ArrayList<T>(); |
38 | 0 | } |
39 | ||
40 | //---- Methods | |
41 | ||
42 | /** | |
43 | * Adds a listener for events fired by this event support. | |
44 | * <p>The same {@code listener} object may be added more than once, and will be called as many | |
45 | * times as it is added.</p> | |
46 | * <p>If the {@code listener} is {@code null}, no action is taken.</p> | |
47 | * @param listener the listener to add, may be {@code null}. | |
48 | * @since 0.1.0 | |
49 | */ | |
50 | public void addListener (T listener) { | |
51 | 0 | if (listener == null) { |
52 | 0 | return; |
53 | } | |
54 | 0 | this.listeners.add(listener); |
55 | 0 | } |
56 | ||
57 | /** | |
58 | * Removes a listener from this event support. | |
59 | * <p>If the same {@code listener} was added more than once, it will be notified one less | |
60 | * time after being removed.</p> | |
61 | * <p>If the {@code listener} is {@code null}, or was never added, no action is taken.</p> | |
62 | * @param listener the listener to be removed, may be {@code null}. | |
63 | */ | |
64 | public void removeListener (T listener) { | |
65 | 0 | if (listener == null) { |
66 | 0 | return; |
67 | } | |
68 | 0 | this.listeners.remove(listener); |
69 | 0 | } |
70 | ||
71 | /** | |
72 | * Dispatches an event to all registered listeners. | |
73 | * @param eventDispatcher the dispatcher to use. | |
74 | * @since 0.1.0 | |
75 | */ | |
76 | @SuppressWarnings("unchecked") | |
77 | public void dispatchEvent (EventDispatcher<T> eventDispatcher) { | |
78 | 0 | if (this.listeners.isEmpty()) { |
79 | 0 | return; |
80 | } | |
81 | 0 | final Object[] listenerArray = this.listeners.toArray(); |
82 | 0 | for (Object listener : listenerArray) { |
83 | // No support for generic arrays, therefore the cast... | |
84 | 0 | eventDispatcher.dispatch((T) listener); |
85 | } | |
86 | 0 | } |
87 | ||
88 | } |