public class BasicEventHandler extends Object implements IEventHandler
Basic implementation of a synchronous event manager.
We have to take care of cases of calls to register/unregister initiated synchronously (in same thread) by listeners while an event is being dispatched. This is done by using temporary lists and committing changes that occured during the iteration only at the end of the call to publish().
Likewise recursive calls to publish have to be handled as well. This is done by accumulating events in a queue and dispatching each one in their own loop, thus avoiding recursive calls to publish while iterating.
We use weak references in the main list of listeners to prevent memory leaks in cases where listeners forget to unregister themselves.
Constructor and Description |
---|
BasicEventHandler() |
Modifier and Type | Method and Description |
---|---|
void |
clearAllListeners()
Clear all listeners.
|
int |
getNumListeners() |
void |
publishEvent(Event<?> e)
Dispatch event to all registered listeners
|
void |
registerListener(IEventListener listener)
Registers a listener with this event producer.
|
void |
unregisterListener(IEventListener listener)
Unregisters a listener from this event producer.
|
public void registerListener(IEventListener listener)
IEventProducer
registerListener
in interface IEventProducer
public void unregisterListener(IEventListener listener)
IEventProducer
unregisterListener
in interface IEventProducer
public void publishEvent(Event<?> e)
IEventHandler
publishEvent
in interface IEventHandler
e
- event to dispatchpublic int getNumListeners()
getNumListeners
in interface IEventHandler
public void clearAllListeners()
IEventHandler
clearAllListeners
in interface IEventHandler
Copyright © 2016 Sensia Software LLC. All rights reserved.