Split ListenerSupport into two classes.
[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.EventListener;
24 import java.util.List;
25
26 /**
27  * Helper class for {@link EventListener} management.
28  *
29  * @param <L>
30  *            The type of the event listener
31  * @author David Roden &lt;droden@gmail.com&gt;
32  */
33 public class ListenerSupport<L extends EventListener> {
34
35         /** The list of registered listeners. */
36         private final List<L> listeners = new ArrayList<L>();
37
38         /**
39          * Adds a listener to the list of registered listeners.
40          *
41          * @param listener
42          *            The listener to add
43          */
44         public void addListener(L listener) {
45                 synchronized (listeners) {
46                         listeners.add(listener);
47                 }
48         }
49
50         /**
51          * Removes a listener from the list of registered listeners.
52          *
53          * @param listener
54          *            The listener to remove
55          */
56         public void removeListener(L listener) {
57                 synchronized (listeners) {
58                         listeners.remove(listener);
59                 }
60         }
61
62         /**
63          * Returns a list of all listeners. This list is a copy of the internally
64          * kept list and as thus will not be modified by calls to
65          * {@link #addListener(EventListener)} or
66          * {@link #removeListener(EventListener)}.
67          *
68          * @return The list of all listeners
69          */
70         protected List<L> getListeners() {
71                 synchronized (listeners) {
72                         return new ArrayList<L>(listeners);
73                 }
74         }
75
76         /* TODO */
77
78 }