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.List;
26 * Helper class to ease listener management.
29 * The type of the source
31 * The type of the listeners
32 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34 public class ListenerSupport<S, L> {
36 /** The source of the events. */
37 private final S source;
39 /** The list of registered listeners. */
40 private final List<L> listeners = new ArrayList<L>();
43 * Creates a new listener support.
46 * The source of all events
48 public ListenerSupport(S source) {
53 * Adds a listener to the list of registered listeners.
58 public void addListener(L listener) {
59 synchronized (listeners) {
60 listeners.add(listener);
65 * Removes a listener from the list of registered listeners.
68 * The listener to remove
70 public void removeListener(L listener) {
71 synchronized (listeners) {
72 listeners.remove(listener);
81 * Returns the source of all events.
83 * @return The source of all events
85 protected S getSource() {
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)}.
94 * @return The list of all listeners
96 protected List<L> getListeners() {
97 synchronized (listeners) {
98 return new ArrayList<L>(listeners);