Add ListenerSupport class.
[jSite2.git] / src / net / pterodactylus / util / event / ListenerSupport.java
1 /*
2  * jSite-next - ListenerSupport.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.util.event;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26  * Helper class to ease listener management.
27  *
28  * @param <S>
29  *            The type of the source
30  * @param <L>
31  *            The type of the listeners
32  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
33  */
34 public class ListenerSupport<S, L> {
35
36         /** The source of the events. */
37         private final S source;
38
39         /** The list of registered listeners. */
40         private final List<L> listeners = new ArrayList<L>();
41
42         /**
43          * Creates a new listener support.
44          *
45          * @param source
46          *            The source of all events
47          */
48         public ListenerSupport(S source) {
49                 this.source = source;
50         }
51
52         /**
53          * Adds a listener to the list of registered listeners.
54          *
55          * @param listener
56          *            The listener to add
57          */
58         public void addListener(L listener) {
59                 synchronized (listeners) {
60                         listeners.add(listener);
61                 }
62         }
63
64         /**
65          * Removes a listener from the list of registered listeners.
66          *
67          * @param listener
68          *            The listener to remove
69          */
70         public void removeListener(L listener) {
71                 synchronized (listeners) {
72                         listeners.remove(listener);
73                 }
74         }
75
76         //
77         // PROTECTED METHODS
78         //
79
80         /**
81          * Returns the source of all events.
82          *
83          * @return The source of all events
84          */
85         protected S getSource() {
86                 return source;
87         }
88
89         /**
90          * Returns a list of all listeners. This list is a copy of the internally
91          * kept list and as thus will not be modified by calls to
92          * {@link #addListener(Object)} or {@link #removeListener(Object)}.
93          *
94          * @return The list of all listeners
95          */
96         protected List<L> getListeners() {
97                 synchronized (listeners) {
98                         return new ArrayList<L>(listeners);
99                 }
100         }
101
102 }