2 * jSite-next - ListenerSupport.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.util.event;
22 import java.util.ArrayList;
23 import java.util.EventListener;
24 import java.util.List;
27 * Helper class for {@link EventListener} management.
30 * The type of the event listener
31 * @author David Roden <droden@gmail.com>
33 public class ListenerSupport<L extends EventListener> {
35 /** The list of registered listeners. */
36 private final List<L> listeners = new ArrayList<L>();
39 * Adds a listener to the list of registered listeners.
44 public void addListener(L listener) {
45 synchronized (listeners) {
46 listeners.add(listener);
51 * Removes a listener from the list of registered listeners.
54 * The listener to remove
56 public void removeListener(L listener) {
57 synchronized (listeners) {
58 listeners.remove(listener);
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)}.
68 * @return The list of all listeners
70 protected List<L> getListeners() {
71 synchronized (listeners) {
72 return new ArrayList<L>(listeners);