Package org.openstreetmap.josm.tools
Class ListenerList<T>
- java.lang.Object
-
- org.openstreetmap.josm.tools.ListenerList<T>
-
- Type Parameters:
T- The type of listener contained in this list.
- Direct Known Subclasses:
ListenerList.TracingListenerList,ListenerList.UncheckedListenerList
public class ListenerList<T> extends java.lang.Object
This is a list of listeners. It does error checking and allows you to fire all listeners.- Since:
- 10824
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceListenerList.EventFirerer<T>This is a function that can be invoked for every listener.static classListenerList.TracingListenerList<T>This is a specialListenerListthat traces calls to the add/remove methods.private static classListenerList.UncheckedListenerList<T>private static classListenerList.WeakListener<T>
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.CopyOnWriteArrayList<T>listenersprivate java.util.concurrent.CopyOnWriteArrayList<ListenerList.WeakListener<T>>weakListeners
-
Constructor Summary
Constructors Modifier Constructor Description protectedListenerList()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(T listener)Adds a listener.voidaddWeakListener(T listener)Adds a listener.booleancontainsListener(T listener)Determines if this listener list contains the given listener.static <T> ListenerList<T>create()Create a new listener liststatic <T> ListenerList<T>createUnchecked()Creates a new listener list that does not fail if listeners are added or removed twice.private booleanensureNotInList(T listener)protected voidfailAdd(T listener)protected voidfailRemove(T listener)voidfireEvent(ListenerList.EventFirerer<T> eventFirerer)Fires an event to every listener.booleanhasListeners()Check if any listeners are registered.voidremoveListener(T listener)Removes a listener.
-
-
-
Field Detail
-
weakListeners
private final java.util.concurrent.CopyOnWriteArrayList<ListenerList.WeakListener<T>> weakListeners
-
-
Constructor Detail
-
ListenerList
protected ListenerList()
-
-
Method Detail
-
addWeakListener
public void addWeakListener(T listener)
Adds a listener. The listener will not prevent the object from being garbage collected.This should be used with care. It is better to add good cleanup code.
- Parameters:
listener- The listener.
-
addListener
public void addListener(T listener)
Adds a listener.- Parameters:
listener- The listener to add.
-
ensureNotInList
private boolean ensureNotInList(T listener)
-
containsListener
public boolean containsListener(T listener)
Determines if this listener list contains the given listener.- Parameters:
listener- listener to find- Returns:
trueis the listener is known- Since:
- 15649
-
removeListener
public void removeListener(T listener)
Removes a listener.- Parameters:
listener- The listener to remove.- Throws:
java.lang.IllegalArgumentException- if the listener was not registered before
-
failRemove
protected void failRemove(T listener)
-
hasListeners
public boolean hasListeners()
Check if any listeners are registered.- Returns:
trueif any are registered.
-
fireEvent
public void fireEvent(ListenerList.EventFirerer<T> eventFirerer)
Fires an event to every listener.- Parameters:
eventFirerer- The firerer to invoke the event method of the listener.
-
create
public static <T> ListenerList<T> create()
Create a new listener list- Type Parameters:
T- The listener type the list should hold.- Returns:
- A new list. A tracing list is created if trace is enabled.
-
createUnchecked
public static <T> ListenerList<T> createUnchecked()
Creates a new listener list that does not fail if listeners are added or removed twice.Use of this list is discouraged. You should always use
create()in new implementations and check your listeners.- Type Parameters:
T- The listener type- Returns:
- A new list.
- Since:
- 11224
-
-