Related: http://stackoverflow.com/questions/1391918/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):
- 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. - Uses
WeakReference
s so that the listener list does not prevent the listeners from being garbage-collected. - The collection is a
Set
, so duplicates are automatically removed. - 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 byCollections.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?