Package org.djutils.event
Class EventProducerImpl
- java.lang.Object
-
- org.djutils.event.EventProducerImpl
-
- All Implemented Interfaces:
Serializable
,EventProducerInterface
public class EventProducerImpl extends Object implements EventProducerInterface, Serializable
The EventProducer forms the reference implementation of the EventProducerInterface. 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.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
-
-
Field Summary
-
Fields inherited from interface org.djutils.event.EventProducerInterface
FIRST_POSITION, LAST_POSITION
-
-
Constructor Summary
Constructors Constructor Description EventProducerImpl(EventProducerInterface embeddingEventProducer)
Construct the helper class to execute the work for registering listeners and firing events.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addListener(EventListenerInterface listener, EventTypeInterface eventType)
Add a listener as strong reference to the BEGINNING of a queue of listeners.boolean
addListener(EventListenerInterface listener, EventTypeInterface eventType, int position)
Add a listener as strong reference to the specified position of a queue of listeners.boolean
addListener(EventListenerInterface listener, EventTypeInterface eventType, int position, ReferenceType referenceType)
Add a listener to the specified position of a queue of listeners.boolean
addListener(EventListenerInterface listener, EventTypeInterface eventType, ReferenceType referenceType)
Add a listener to the BEGINNING of a queue of listeners.void
fireEvent(EventInterface event)
Transmit an event to all interested listeners.void
fireEvent(EventInterface event, boolean verifyMetaData)
Transmit a regular event to all interested listeners.void
fireEvent(EventListenerInterface listener, EventInterface event)
Transmit an event to a listener.void
fireEvent(EventTypeInterface eventType, boolean verifyMetaData)
Transmit an event with no payload object to all interested listeners.boolean
fireEvent(EventTypeInterface eventType, boolean value, boolean verifyMetaData)
Transmit an event with a boolean payload to all interested listeners.byte
fireEvent(EventTypeInterface eventType, byte value, boolean verifyMetaData)
Transmit an event with a one byte payload to all interested listeners.char
fireEvent(EventTypeInterface eventType, char value, boolean verifyMetaData)
Transmit an event with a one char payload to all interested listeners.double
fireEvent(EventTypeInterface eventType, double value, boolean verifyMetaData)
Transmit an event with a double value payload to all interested listeners.float
fireEvent(EventTypeInterface eventType, float value, boolean verifyMetaData)
Transmit an event with a float value payload to all interested listeners.int
fireEvent(EventTypeInterface eventType, int value, boolean verifyMetaData)
Transmit an event with an integer payload to all interested listeners.long
fireEvent(EventTypeInterface eventType, long value, boolean verifyMetaData)
Transmit an event with a long payload to all interested listeners.short
fireEvent(EventTypeInterface eventType, short value, boolean verifyMetaData)
Transmit an event with a short payload to all interested listeners.Serializable
fireEvent(EventTypeInterface eventType, Serializable value, boolean verifyMetaData)
Transmit an event with a serializable object as payload to all interested listeners.<C extends Comparable<C> & Serializable>
voidfireTimedEvent(TimedEventInterface<C> event, boolean verifyMetaData)
Transmit a time-stamped event to all interested listeners.<C extends Comparable<C> & Serializable>
booleanfireTimedEvent(TimedEventTypeInterface eventType, boolean value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a boolean payload to all interested listeners.<C extends Comparable<C> & Serializable>
bytefireTimedEvent(TimedEventTypeInterface eventType, byte value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a one byte payload to all interested listeners.<C extends Comparable<C> & Serializable>
charfireTimedEvent(TimedEventTypeInterface eventType, char value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a one char payload to all interested listeners.<C extends Comparable<C> & Serializable>
doublefireTimedEvent(TimedEventTypeInterface eventType, double value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a double value payload to interested listeners.<C extends Comparable<C> & Serializable>
floatfireTimedEvent(TimedEventTypeInterface eventType, float value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a float value payload to interested listeners.<C extends Comparable<C> & Serializable>
intfireTimedEvent(TimedEventTypeInterface eventType, int value, C time, boolean verifyMetaData)
Transmit a time-stamped event with an integer payload to all interested listeners.<C extends Comparable<C> & Serializable>
longfireTimedEvent(TimedEventTypeInterface eventType, long value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a long payload to all interested listeners.<C extends Comparable<C> & Serializable>
shortfireTimedEvent(TimedEventTypeInterface eventType, short value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a short payload to all interested listeners.<C extends Comparable<C> & Serializable>
voidfireTimedEvent(TimedEventTypeInterface eventType, C time, boolean verifyMetaData)
Transmit a time-stamped event with a no payload object to all interested listeners.<C extends Comparable<C> & Serializable>
SerializablefireTimedEvent(TimedEventTypeInterface eventType, Serializable value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a Serializable object (payload) to all interested listeners.Set<EventTypeInterface>
getEventTypesWithListeners()
Return the EventTypeInterfaces for which the EventProducer has listeners.List<Reference<EventListenerInterface>>
getListenerReferences(EventTypeInterface 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.Serializable
getSourceId()
Provide the sourceId that will be transmitted with the fired Event.boolean
hasListeners()
Return whether the EventProducer has listeners.int
numberOfListeners(EventTypeInterface eventType)
Return the number of listeners for the provided EventTypeInterface.int
removeAllListeners()
Remove all the listeners from this event producer.int
removeAllListeners(Class<?> ofClass)
Removes all the listeners of a class from this event producer.boolean
removeListener(EventListenerInterface listener, EventTypeInterface eventType)
Remove the subscription of a listener for a specific event.
-
-
-
Constructor Detail
-
EventProducerImpl
public EventProducerImpl(EventProducerInterface embeddingEventProducer)
Construct the helper class to execute the work for registering listeners and firing events.- Parameters:
embeddingEventProducer
- EventProducerInterface; the embedding event producer class
-
-
Method Detail
-
getSourceId
public Serializable 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
public final boolean addListener(EventListenerInterface listener, EventTypeInterface eventType)
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
- EventTypeInterface; 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, EventTypeInterface 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
- EventTypeInterface; 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, EventTypeInterface 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
- EventTypeInterface; 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, EventTypeInterface 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
- EventTypeInterface; 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
public void fireEvent(EventListenerInterface listener, EventInterface event) throws RemoteException
Transmit an event to a listener. This method is a hook method. The default implementation simply invokes the notify on the listener. In specific cases (filtering, storing, queuing, this method can be overwritten.- Parameters:
listener
- EventListenerInterface; the listener for this eventevent
- EventInterface; the event to fire- Throws:
RemoteException
- on network failure
-
fireEvent
public void fireEvent(EventInterface event)
Transmit an event to all interested listeners.- Parameters:
event
- EventInterface; the event
-
fireEvent
public void fireEvent(EventInterface event, boolean verifyMetaData)
Transmit a regular event to all interested listeners.- Parameters:
event
- EventInterface; the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> void fireTimedEvent(TimedEventInterface<C> event, boolean verifyMetaData)
Transmit a time-stamped event to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
event
- TimedEventInterface<C>; the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not
-
fireEvent
public void fireEvent(EventTypeInterface eventType, boolean verifyMetaData)
Transmit an event with no payload object to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> void fireTimedEvent(TimedEventTypeInterface eventType, C time, boolean verifyMetaData)
Transmit a time-stamped event with a no payload object to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- TimedEventTypeInterface; the eventType of the event.time
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not
-
fireEvent
public Serializable fireEvent(EventTypeInterface eventType, Serializable value, boolean verifyMetaData)
Transmit an event with a serializable object as payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- Serializable; the object sent with the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- Serializable; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> Serializable fireTimedEvent(TimedEventTypeInterface eventType, Serializable value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the event.value
- Serializable; the payload sent with the eventtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- Serializable; the payload
-
fireEvent
public byte fireEvent(EventTypeInterface eventType, byte value, boolean verifyMetaData)
Transmit an event with a one byte payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- byte; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- byte; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> byte fireTimedEvent(TimedEventTypeInterface eventType, byte value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the eventvalue
- byte; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- byte; the payload
-
fireEvent
public char fireEvent(EventTypeInterface eventType, char value, boolean verifyMetaData)
Transmit an event with a one char payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- char; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- char; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> char fireTimedEvent(TimedEventTypeInterface eventType, char value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a one char payload to all interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- TimedEventTypeInterface; the eventType of the eventvalue
- char; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- char; the payload
-
fireEvent
public boolean fireEvent(EventTypeInterface eventType, boolean value, boolean verifyMetaData)
Transmit an event with a boolean payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- boolean; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- boolean; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> boolean fireTimedEvent(TimedEventTypeInterface eventType, boolean value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the eventvalue
- boolean; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- boolean; the payload
-
fireEvent
public double fireEvent(EventTypeInterface eventType, double value, boolean verifyMetaData)
Transmit an event with a double value payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- double; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- double; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> double fireTimedEvent(TimedEventTypeInterface eventType, double value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the eventvalue
- double; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- double; the payload
-
fireEvent
public int fireEvent(EventTypeInterface eventType, int value, boolean verifyMetaData)
Transmit an event with an integer payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- int; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- int; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> int fireTimedEvent(TimedEventTypeInterface eventType, int value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the eventvalue
- int; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- int; the payload
-
fireEvent
public long fireEvent(EventTypeInterface eventType, long value, boolean verifyMetaData)
Transmit an event with a long payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- long; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- long; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> long fireTimedEvent(TimedEventTypeInterface eventType, long value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the eventvalue
- long; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- long; the payload
-
fireEvent
public short fireEvent(EventTypeInterface eventType, short value, boolean verifyMetaData)
Transmit an event with a short payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- short; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- short; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> short fireTimedEvent(TimedEventTypeInterface eventType, short value, C time, boolean verifyMetaData)
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
- TimedEventTypeInterface; the eventType of the eventvalue
- short; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- short; the payload
-
fireEvent
public float fireEvent(EventTypeInterface eventType, float value, boolean verifyMetaData)
Transmit an event with a float value payload to all interested listeners.- Parameters:
eventType
- EventTypeInterface; the eventType of the eventvalue
- float; the payloadverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- float; the payload
-
fireTimedEvent
public <C extends Comparable<C> & Serializable> float fireTimedEvent(TimedEventTypeInterface eventType, float value, C time, boolean verifyMetaData)
Transmit a time-stamped event with a float value payload to interested listeners.- Type Parameters:
C
- the comparable type to indicate the time when the event is fired- Parameters:
eventType
- TimedEventTypeInterface; the eventType of the eventvalue
- float; the payloadtime
- C; a time stamp for the eventverifyMetaData
- boolean; whether to verify the compliance with metadata or not- Returns:
- float; the payload
-
removeAllListeners
public int removeAllListeners()
Remove all the listeners from this event producer.- Returns:
- int; the number of removed event types
-
removeAllListeners
public int removeAllListeners(Class<?> ofClass)
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
public final boolean removeListener(EventListenerInterface listener, EventTypeInterface eventType)
Remove the subscription of a listener for a specific event.- Specified by:
removeListener
in interfaceEventProducerInterface
- Parameters:
listener
- EventListenerInterface; which is no longer interestedeventType
- EventTypeInterface; 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
public int numberOfListeners(EventTypeInterface eventType)
Return the number of listeners for the provided EventTypeInterface.- Specified by:
numberOfListeners
in interfaceEventProducerInterface
- Parameters:
eventType
- EventTypeInterface; the event type to return the number of listeners for- Returns:
- boolean; whether the EventProducer has listeners or not
-
getListenerReferences
public List<Reference<EventListenerInterface>> getListenerReferences(EventTypeInterface 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. 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
- EventTypeInterface; 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
-
getEventTypesWithListeners
public Set<EventTypeInterface> getEventTypesWithListeners()
Return the EventTypeInterfaces for which the EventProducer has listeners.- Specified by:
getEventTypesWithListeners
in interfaceEventProducerInterface
- Returns:
- Set<EventTypeInterface>; the EventTypeInterfaces for which the EventProducer has registered listeners
-
-