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-2020 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
Modifier and Type Method Description void
clear()
Clears the EventListenerMap.boolean
containsKey(EventType 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<EventType,List<Reference<EventListenerInterface>>>>
entrySet()
Returns the Set of Entry types holding pairs of a key (EventType) and a value (List of references to EventListeners for that EventType).List<Reference<EventListenerInterface>>
get(EventType key)
Returns the original List of references to EventListeners for the given EventType.boolean
isEmpty()
Return whether the EventListenerMap is empty.Set<EventType>
keySet()
Returns a safe copy of the Set of EventTypes for which listeners are registered.List<Reference<EventListenerInterface>>
put(EventType key, List<Reference<EventListenerInterface>> value)
Add the List of references to EventListeners for the given EventType to the underlying Map.void
putAll(EventListenerMap m)
Add all entries of the map to the EventListenerMap.List<Reference<EventListenerInterface>>
remove(EventType key)
Remove the List of references to EventListeners for the given EventType.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.
-
Constructor Details
-
EventListenerMap
public EventListenerMap()
-
-
Method Details
-
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
Return whether the EventListenerMap contains the EventType as a key.- Parameters:
eventType
- EventType; the EventType key to search for- Returns:
- boolean; whether the EventListenerMap contains the EventType as a key
-
containsValue
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
Returns whether the EventListenerMap contains the reference to the eventListener as one of the subscribers.- Parameters:
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
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
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
Returns the Set of Entry types holding pairs of a key (EventType) and a value (List of references to EventListeners for that EventType). 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<EventType, List<Reference<EventListenerInterface>>>>;the Set of Entry types holding pairs of a key (EventType) and a value (List of references to EventListeners for that EventType). Note: this is not a safe copy!
-
keySet
Returns a safe copy of the Set of EventTypes for which listeners are registered.- Returns:
- Set<EventType>; a safe copy of the Set of EventType keys for which listeners are registered
-
get
Returns the original List of references to EventListeners for the given EventType. 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 EventType is not found.- Parameters:
key
- EventType; the eventType to look up the listeners for- Returns:
- List<Reference<EventListenerInterface>; the List of references to EventListeners for the given EventType, or null when the EventType is not found. Note: this is not a safe copy.
-
remove
Remove the List of references to EventListeners for the given EventType.- Parameters:
key
- EventType; the eventType to remove the listeners for- Returns:
- List<Reference<EventListenerInterface>>; the removed List of references to EventListeners for the given EventType
-
put
public List<Reference<EventListenerInterface>> put(EventType key, List<Reference<EventListenerInterface>> value)Add the List of references to EventListeners for the given EventType 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
- EventType; the eventType to store the listeners forvalue
- List<Reference<EventListenerInterface>>; the references to EventListeners to store for the given EventType- Returns:
- List<Reference<EventListenerInterface>; the previous List of references to EventListeners for the given EventType, or null when there was no previous mapping
-