Package org.djutils.event
Class EventListenerMap
- java.lang.Object
-
- org.djutils.event.EventListenerMap
-
- All Implemented Interfaces:
Serializable
public final class EventListenerMap extends Object implements Serializable
The EventListenerMap maps EventTypes on lists of References to EventListeners. The References can be Weak or Strong. The Map can be serialized. When serializing, the References to RemoteEventListeners are not written as they are fully dependent on a volatile network state that will almost certainly not be the same when the serialized map is read back.Copyright (c) 2002-2021 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html. This class was originally part of the DSOL project, see https://simulation.tudelft.nl/dsol/manual.
- Author:
- Peter Jacobs , Alexander Verbraeck
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description EventListenerMap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the EventListenerMap.boolean
containsKey(EventTypeInterface eventType)
Return whether the EventListenerMap contains the EventType as a key.boolean
containsValue(EventListenerInterface eventListener)
Return whether the EventListenerMap contains the eventListener as one of the subscribers.boolean
containsValue(Reference<EventListenerInterface> reference)
Returns whether the EventListenerMap contains the reference to the eventListener as one of the subscribers.Set<Map.Entry<EventTypeInterface,List<Reference<EventListenerInterface>>>>
entrySet()
Returns the Set of Entry types holding pairs of a key (EventTypeInterface) and a value (List of references to EventListeners for that EventTypeInterface).List<Reference<EventListenerInterface>>
get(EventTypeInterface key)
Returns the original List of references to EventListeners for the given EventTypeInterface.boolean
isEmpty()
Return whether the EventListenerMap is empty.Set<EventTypeInterface>
keySet()
Returns a safe copy of the Set of EventTypeInterfaces for which listeners are registered.List<Reference<EventListenerInterface>>
put(EventTypeInterface key, List<Reference<EventListenerInterface>> value)
Add the List of references to EventListeners for the given EventTypeInterface to the underlying Map.void
putAll(EventListenerMap m)
Add all entries of the map to the EventListenerMap.List<Reference<EventListenerInterface>>
remove(EventTypeInterface key)
Remove the List of references to EventListeners for the given EventTypeInterface.int
size()
Return the size of the EventListenerMap, i.e.Collection<List<Reference<EventListenerInterface>>>
values()
Returns a safe copy of the collection of lists of references to EventListeners, i.e.
-
-
-
Method Detail
-
size
public int size()
Return the size of the EventListenerMap, i.e. the number of EventTypes that are registered.- Returns:
- int; the size of the EventListenerMap, i.e. the number of EventTypes that are registered
-
clear
public void clear()
Clears the EventListenerMap.
-
isEmpty
public boolean isEmpty()
Return whether the EventListenerMap is empty.- Returns:
- boolean; whether the EventListenerMap is empty
-
containsKey
public boolean containsKey(EventTypeInterface eventType)
Return whether the EventListenerMap contains the EventType as a key.- Parameters:
eventType
- EventTypeInterface; the EventType key to search for- Returns:
- boolean; whether the EventListenerMap contains the EventType as a key
-
containsValue
public boolean containsValue(EventListenerInterface eventListener)
Return whether the EventListenerMap contains the eventListener as one of the subscribers.- Parameters:
eventListener
- EventListenerInterface; the EventListener value to search for- Returns:
- boolean; true if the EventListenerMap contains the eventListener as one of the subscribers; false otherwise
-
containsValue
public boolean containsValue(Reference<EventListenerInterface> reference)
Returns whether the EventListenerMap contains the reference to the eventListener as one of the subscribers.- Parameters:
reference
- Reference<EventListenerInterface>; the reference pointer an EventListener to search for- Returns:
- boolean; true if the EventListenerMap contains the reference to the eventListener as one of the subscribers; false otherwise
-
values
public Collection<List<Reference<EventListenerInterface>>> values()
Returns a safe copy of the collection of lists of references to EventListeners, i.e. all the listeners registered in the map- Returns:
- Collection<List<Reference<EventListenerInterface>>>; a safe copy of the collection of lists of references to EventListeners, i.e. all the listeners registered in the map
-
putAll
public void putAll(EventListenerMap m)
Add all entries of the map to the EventListenerMap. The lists of listeners are added as a safe copy, so the list will not be changed when the entries from copied map will be changed.- Parameters:
m
- EventListenerMap; the map with references to event listeners to add to the current EventListenerMap
-
entrySet
public Set<Map.Entry<EventTypeInterface,List<Reference<EventListenerInterface>>>> entrySet()
Returns the Set of Entry types holding pairs of a key (EventTypeInterface) and a value (List of references to EventListeners for that EventTypeInterface). Note: this is a map with the real values, so not a safe copy. This entrySet can be used to change the underlying map.- Returns:
- Set<Map.Entry<EventTypeInterface, List<Reference<EventListenerInterface>>>>;the Set of Entry types holding pairs of a key (EventTypeInterface) and a value (List of references to EventListeners for that EventTypeInterface). Note: this is not a safe copy!
-
keySet
public Set<EventTypeInterface> keySet()
Returns a safe copy of the Set of EventTypeInterfaces for which listeners are registered.- Returns:
- Set<EventTypeInterface>; a safe copy of the Set of EventTypeInterface keys for which listeners are registered
-
get
public List<Reference<EventListenerInterface>> get(EventTypeInterface key)
Returns the original List of references to EventListeners for the given EventTypeInterface. Note: this is not a safe copy, so the list is backed by the original data structure and will change when listeners are added or removed. The method will return null when the EventTypeInterface is not found.- Parameters:
key
- EventTypeInterface; the eventType to look up the listeners for- Returns:
- List<Reference<EventListenerInterface>; the List of references to EventListeners for the given EventTypeInterface, or null when the EventTypeInterface is not found. Note: this is not a safe copy.
-
remove
public List<Reference<EventListenerInterface>> remove(EventTypeInterface key)
Remove the List of references to EventListeners for the given EventTypeInterface.- Parameters:
key
- EventTypeInterface; the eventType to remove the listeners for- Returns:
- List<Reference<EventListenerInterface>>; the removed List of references to EventListeners for the given EventTypeInterface
-
put
public List<Reference<EventListenerInterface>> put(EventTypeInterface key, List<Reference<EventListenerInterface>> value)
Add the List of references to EventListeners for the given EventTypeInterface to the underlying Map. A safe copy will be added, so the original list will not be affected when listeners are removed or added, nor will the underlying map be affected when the provided list is changed.- Parameters:
key
- EventTypeInterface; the eventType to store the listeners forvalue
- List<Reference<EventListenerInterface>>; the references to EventListeners to store for the given EventTypeInterface- Returns:
- List<Reference<EventListenerInterface>; the previous List of references to EventListeners for the given EventTypeInterface, or null when there was no previous mapping
-
-