Package org.djutils.event
Class EventProducer
java.lang.Object
org.djutils.event.EventProducer
- All Implemented Interfaces:
Serializable
,EventProducerInterface
- Direct Known Subclasses:
EventProducingCollection
,EventProducingIterator
,EventProducingList
,EventProducingMap
,EventProducingSet
public abstract class EventProducer extends Object implements EventProducerInterface, Serializable
The EventProducer forms the reference implementation of the EventProducerInterface. Objects extending this class are provided
all the functionalities for registration and event firing. The storage of the listeners is done in a Map with the EventType
as the key, and a List of References (weak or strong) to the Listeners. The class uses a helper class EventProducerImpl to do
the real work, and avoid code duplication.
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
-
Field Summary
Fields Modifier and Type Field Description protected EventProducerImpl
eventProducerImpl
The EventProducer helper class with the actual implementation to avoid code duplication.Fields inherited from interface org.djutils.event.EventProducerInterface
FIRST_POSITION, LAST_POSITION
-
Constructor Summary
Constructors Constructor Description EventProducer()
Constructs a new EventProducer and checks for duplicate values in event types. -
Method Summary
Modifier and Type Method Description boolean
addListener(EventListenerInterface listener, EventType eventType)
Add a listener as strong reference to the BEGINNING of a queue of listeners.boolean
addListener(EventListenerInterface listener, EventType eventType, int position)
Add a listener as strong reference to the specified position of a queue of listeners.boolean
addListener(EventListenerInterface listener, EventType eventType, int position, ReferenceType referenceType)
Add a listener to the specified position of a queue of listeners.boolean
addListener(EventListenerInterface listener, EventType eventType, ReferenceType referenceType)
Add a listener to the BEGINNING of a queue of listeners.protected EventInterface
fireEvent(EventInterface event)
Transmit an event to all interested listeners.protected void
fireEvent(EventType eventType)
Transmit an event with no payload object to all interested listeners.protected boolean
fireEvent(EventType eventType, boolean value)
Transmit an event with a boolean payload to all interested listeners.protected byte
fireEvent(EventType eventType, byte value)
Transmit an event with a one byte payload to all interested listeners.protected double
fireEvent(EventType eventType, double value)
Transmit an event with a double value payload to all interested listeners.protected int
fireEvent(EventType eventType, int value)
Transmit an event with an integer payload to all interested listeners.protected long
fireEvent(EventType eventType, long value)
Transmit an event with a long payload to all interested listeners.protected short
fireEvent(EventType eventType, short value)
Transmit an event with a short payload to all interested listeners.protected Serializable
fireEvent(EventType eventType, Serializable value)
Transmit an event with a serializable object as payload to all interested listeners.protected <C extends Comparable<C> & Serializable>
booleanfireTimedEvent(EventType eventType, boolean value, C time)
Transmit a time-stamped event with a boolean payload to all interested listeners.protected <C extends Comparable<C> & Serializable>
bytefireTimedEvent(EventType eventType, byte value, C time)
Transmit a time-stamped event with a one byte payload to all interested listeners.protected <C extends Comparable<C> & Serializable>
doublefireTimedEvent(EventType eventType, double value, C time)
Transmit a time-stamped event with a double value payload to interested listeners.protected <C extends Comparable<C> & Serializable>
intfireTimedEvent(EventType eventType, int value, C time)
Transmit a time-stamped event with an integer payload to all interested listeners.protected <C extends Comparable<C> & Serializable>
longfireTimedEvent(EventType eventType, long value, C time)
Transmit a time-stamped event with a long payload to all interested listeners.protected <C extends Comparable<C> & Serializable>
shortfireTimedEvent(EventType eventType, short value, C time)
Transmit a time-stamped event with a short payload to all interested listeners.protected <C extends Comparable<C> & Serializable>
SerializablefireTimedEvent(EventType eventType, Serializable value, C time)
Transmit a time-stamped event with a Serializable object (payload) to all interested listeners.Set<EventType>
getEventTypesWithListeners()
Return the EventTypes for which the EventProducer has listeners.protected List<Reference<EventListenerInterface>>
getListenerReferences(EventType eventType)
Return a safe copy of the list of (strong or weak) references to the registered listeners for the provided event type, or an empty list when nothing is registered for this event type.abstract Serializable
getSourceId()
Provide the sourceId that will be transmitted with the fired Event.boolean
hasListeners()
Return whether the EventProducer has listeners.int
numberOfListeners(EventType eventType)
Return the number of listeners for the provided EventType.protected int
removeAllListeners()
Remove all the listeners from this event producer.protected int
removeAllListeners(Class<?> ofClass)
Removes all the listeners of a class from this event producer.boolean
removeListener(EventListenerInterface listener, EventType eventType)
Remove the subscription of a listener for a specific event.
-
Field Details
-
eventProducerImpl
The EventProducer helper class with the actual implementation to avoid code duplication.
-
-
Constructor Details
-
EventProducer
public EventProducer()Constructs a new EventProducer and checks for duplicate values in event types.
-
-
Method Details
-
getSourceId
Provide the sourceId that will be transmitted with the fired Event.- Specified by:
getSourceId
in interfaceEventProducerInterface
- Returns:
- Serializable; the sourceId that will be transmitted with the fired Event
-
addListener
Add a listener as strong reference to the BEGINNING of a queue of listeners.- Specified by:
addListener
in interfaceEventProducerInterface
- Parameters:
listener
- EventListenerInterface; the listener which is interested at events of eventTypeeventType
- EventType; the events of interest- Returns:
- the success of adding the listener. If a listener was already added false is returned
-
addListener
public final boolean addListener(EventListenerInterface listener, EventType eventType, ReferenceType referenceType)Add a listener to the BEGINNING of a queue of listeners.- Specified by:
addListener
in interfaceEventProducerInterface
- Parameters:
listener
- EventListenerInterface; the listener which is interested at events of eventTypeeventType
- EventType; the events of interestreferenceType
- ReferenceType; whether the listener is added as a strong or as a weak reference- Returns:
- the success of adding the listener. If a listener was already added false is returned
- See Also:
WeakReference
-
addListener
public final boolean addListener(EventListenerInterface listener, EventType eventType, int position)Add a listener as strong reference to the specified position of a queue of listeners.- Specified by:
addListener
in interfaceEventProducerInterface
- Parameters:
listener
- EventListenerInterface; the listener which is interested at events of eventTypeeventType
- EventType; the events of interestposition
- int; the position of the listener in the queue- Returns:
- the success of adding the listener. If a listener was already added, or an illegal position is provided false is returned
-
addListener
public final boolean addListener(EventListenerInterface listener, EventType eventType, int position, ReferenceType referenceType)Add a listener to the specified position of a queue of listeners.- Specified by:
addListener
in interfaceEventProducerInterface
- Parameters:
listener
- EventListenerInterface; which is interested at certain eventseventType
- EventType; the events of interestposition
- int; the position of the listener in the queuereferenceType
- ReferenceType; whether the listener is added as a strong or as a weak reference- Returns:
- the success of adding the listener. If a listener was already added or an illegal position is provided false is returned
- See Also:
WeakReference
-
fireEvent
Transmit an event to all interested listeners.- Parameters:
event
- EventInterface; the event- Returns:
- EventInterface; the event (for method chaining)
-
fireEvent
Transmit an event with a serializable object as payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- Serializable; the object sent with the event- Returns:
- Serializable; the payload
-
fireEvent
Transmit an event with no payload object to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the event
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> Serializable fireTimedEvent(EventType eventType, Serializable value, C time)Transmit a time-stamped event with a Serializable object (payload) to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the event.value
- Serializable; the payload sent with the eventtime
- C; a time stamp for the event- Returns:
- Serializable; the payload
-
fireEvent
Transmit an event with a one byte payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- byte; the payload- Returns:
- byte; the payload
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> byte fireTimedEvent(EventType eventType, byte value, C time)Transmit a time-stamped event with a one byte payload to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the eventvalue
- byte; the payloadtime
- C; a time stamp for the event- Returns:
- byte; the payload
-
fireEvent
Transmit an event with a boolean payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- boolean; the payload- Returns:
- boolean; the payload
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> boolean fireTimedEvent(EventType eventType, boolean value, C time)Transmit a time-stamped event with a boolean payload to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the eventvalue
- boolean; the payloadtime
- C; a time stamp for the event- Returns:
- boolean; the payload
-
fireEvent
Transmit an event with a double value payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- double; the payload- Returns:
- double; the payload
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> double fireTimedEvent(EventType eventType, double value, C time)Transmit a time-stamped event with a double value payload to interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the eventvalue
- double; the payloadtime
- C; a time stamp for the event- Returns:
- double; the payload
-
fireEvent
Transmit an event with an integer payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- int; the payload- Returns:
- int; the payload
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> int fireTimedEvent(EventType eventType, int value, C time)Transmit a time-stamped event with an integer payload to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the eventvalue
- int; the payloadtime
- C; a time stamp for the event- Returns:
- int; the payload
-
fireEvent
Transmit an event with a long payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- long; the payload- Returns:
- long; the payload
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> long fireTimedEvent(EventType eventType, long value, C time)Transmit a time-stamped event with a long payload to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the eventvalue
- long; the payloadtime
- C; a time stamp for the event- Returns:
- long; the payload
-
fireEvent
Transmit an event with a short payload to all interested listeners.- Parameters:
eventType
- EventType; the eventType of the eventvalue
- short; the payload- Returns:
- short; the payload
-
fireTimedEvent
protected <C extends Comparable<C> & Serializable> short fireTimedEvent(EventType eventType, short value, C time)Transmit a time-stamped event with a short payload to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- EventType; the eventType of the eventvalue
- short; the payloadtime
- C; a time stamp for the event- Returns:
- short; the payload
-
removeAllListeners
protected int removeAllListeners()Remove all the listeners from this event producer.- Returns:
- int; the number of removed event types
-
removeAllListeners
Removes all the listeners of a class from this event producer.- Parameters:
ofClass
- Class<?>; the class or superclass- Returns:
- int; the number of removed listeners
-
removeListener
Remove the subscription of a listener for a specific event.- Specified by:
removeListener
in interfaceEventProducerInterface
- Parameters:
listener
- EventListenerInterface; which is no longer interestedeventType
- EventType; the event which is of no interest any more- Returns:
- the success of removing the listener. If a listener was not subscribed false is returned
-
hasListeners
public boolean hasListeners()Return whether the EventProducer has listeners.- Specified by:
hasListeners
in interfaceEventProducerInterface
- Returns:
- boolean; whether the EventProducer has listeners or not
-
numberOfListeners
Return the number of listeners for the provided EventType.- Specified by:
numberOfListeners
in interfaceEventProducerInterface
- Parameters:
eventType
- EventType; the event type to return the number of listeners for- Returns:
- boolean; whether the EventProducer has listeners or not
-
getEventTypesWithListeners
Return the EventTypes for which the EventProducer has listeners.- Specified by:
getEventTypesWithListeners
in interfaceEventProducerInterface
- Returns:
- Set<EventType>; the EventTypes for which the EventProducer has registered listeners
-
getListenerReferences
Return a safe copy of the list of (strong or weak) references to the registered listeners for the provided event type, or an empty list when nothing is registered for this event type. The method never returns a null pointer, so it is safe to use the result directly in an iterator. The references to the listeners are the original references, so not safe copies.- Parameters:
eventType
- EventType; the event type to look up the listeners for- Returns:
- List<Reference<EventListenerInterface>>; the list of references to the listeners for this event type, or an empty list when the event type is not registered
-