Related: Does java have a "LinkedConcurrentHashMap" data structure?
I am looking for a collection class to hold references to event listeners.
Ideally I would like the collection to have the following properties (in order of priority):
HashSet
whose iterator may return elements in the wrong order.WeakReference
s so that the listener list does not prevent the listeners from being garbage-collected.Set
, so duplicates are automatically removed.Iterator
is a thread-safe snapshot of the collection, unaffected by the addition of new listeners. Also allows events to be delivered on multiple threads. (This is not essential - I could iterate over a clone of the set instead.)I am aware of some classes that satisfy some but not all of these criteria. Examples:
java.util.LinkedHashSet
(#1 and #3)java.util.WeakHashMap
, wrapped by Collections.newSetFromMap
(#2 and #3)javax.swing.event.EventListenerList
(needs some extra synchronization) (#1 and #4)java.util.concurrent.CopyOnWriteArraySet
(#1, #3 and #4)But nothing with both #1 and #2. Does class like this exist in a library somewhere?
An event listener in Java is designed to process some kind of event — it "listens" for an event, such as a user's mouse click or a key press, and then it responds accordingly. An event listener must be connected to an event object that defines the event.
The Event listener represent the interfaces responsible to handle events. Java provides us various Event listener classes but we will discuss those which are more frequently used. Every method of an event listener method has a single argument as an object which is subclass of EventObject class.
Collections are used in every programming language and when Java arrived, it also came with few Collection classes – Vector, Stack, Hashtable, Array.
The EventListener interface is the primary method for handling events. Users implement the EventListener interface and register their listener on an EventTarget using the AddEventListener method.
You could use WeakListeners (see http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/WeakListeners.html) and CopyOnWriteArraySet.
remove(ListenerType listener)
method in your event source.In your register(SomeListener listener)
method, add a WeakListener to the collection instead:
listenerCollection.put((ListenerType)WeakListeners.create (
ListenerType.class, listener, this));
When the real listener is removed from memory, the weak listener will be notified, and it will unregister itself. (This is why it needs the reference to the source (this
) for the registration.) The unregistration is done using reflection by calling the method remove of the source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With