Ignore maven’s “target/” directory.
[jFCPlib.git] / src / main / java / net / pterodactylus / util / event / AbstractListenerManager.java
1 /*
2  * jFCPlib - AbstractListenerManager.java - Copyright © 2009 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.util.event;
19
20 import java.util.EventListener;
21 import java.util.List;
22 import java.util.concurrent.CopyOnWriteArrayList;
23 import java.util.concurrent.Executor;
24
25 import net.pterodactylus.util.thread.CurrentThreadExecutor;
26
27 /**
28  * Abstract implementation of a listener support class. The listener support
29  * takes care of adding and removing {@link EventListener} implementations, and
30  * subclasses are responsible for firing appropriate events.
31  *
32  * @param <S>
33  *            The type of the source
34  *@param <L>
35  *            The type of the event listeners
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public abstract class AbstractListenerManager<S, L extends EventListener> {
39
40         /** The source that emits the events. */
41         private final S source;
42
43         /** The list of listeners. */
44         private final List<L> listeners = new CopyOnWriteArrayList<L>();
45
46         /** Service that executes event threads. */
47         private final Executor executor;
48
49         /**
50          * Creates a new listener support that emits events from the given source.
51          *
52          * @param source
53          *            The source of the events
54          */
55         public AbstractListenerManager(S source) {
56                 this(source, new CurrentThreadExecutor());
57         }
58
59         /**
60          * Creates a new listener support that emits events from the given source.
61          *
62          * @param source
63          *            The source of the events
64          * @param executor
65          *            The executor used to fire events
66          */
67         public AbstractListenerManager(S source, Executor executor) {
68                 this.source = source;
69                 this.executor = executor;
70         }
71
72         /**
73          * Adds the given listener to the list of reigstered listeners.
74          *
75          * @param listener
76          *            The listener to add
77          */
78         public void addListener(L listener) {
79                 synchronized (listeners) {
80                         listeners.add(listener);
81                 }
82         }
83
84         /**
85          * Removes the given listener from the list of registered listeners.
86          *
87          * @param listener
88          *            The listener to remove
89          */
90         public void removeListener(L listener) {
91                 synchronized (listeners) {
92                         listeners.remove(listener);
93                 }
94         }
95
96         //
97         // PROTECTED METHODS
98         //
99
100         /**
101          * Returns the source for the events.
102          *
103          * @return The event source
104          */
105         protected S getSource() {
106                 return source;
107         }
108
109         /**
110          * Returns the executor for the event firing.
111          *
112          * @return The executor
113          */
114         protected Executor getExecutor() {
115                 return executor;
116         }
117
118         /**
119          * Returns a list of all registered listeners. The returned list is a copy
120          * of the original list so structural modifications will never occur when
121          * using the returned list.
122          *
123          * @return The list of all registered listeners
124          */
125         protected List<L> getListeners() {
126                 return listeners;
127         }
128
129 }