Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitable collection class for event listeners in Java

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):

  1. Maintains insertion order. The earlier listeners may cancel the event, preventing it from being delivered to listeners added later. This will break if using a class such as HashSet whose iterator may return elements in the wrong order.
  2. Uses WeakReferences so that the listener list does not prevent the listeners from being garbage-collected.
  3. The collection is a Set, so duplicates are automatically removed.
  4. The 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?

like image 579
finnw Avatar asked Jan 15 '10 15:01

finnw


People also ask

What is the use of event listeners class in Java?

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.

What is event listener and its classes?

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.

What are the 4 collection classes in Java?

Collections are used in every programming language and when Java arrived, it also came with few Collection classes – Vector, Stack, Hashtable, Array.

Which interface is used to listener to events?

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.


1 Answers

You could use WeakListeners (see http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/WeakListeners.html) and CopyOnWriteArraySet.

  1. Implement a remove(ListenerType listener) method in your event source.
  2. 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.

like image 129
Oleg Ryaboy Avatar answered Nov 15 '22 17:11

Oleg Ryaboy