Package com.iizix.event
Class EventMulticaster
java.lang.Object
com.iizix.event.EventMulticaster
- All Implemented Interfaces:
EventListener
A class which implements efficient and thread-safe multicast event dispatching for generic events.
This class will manage an immutable structure consisting of a chain of event listeners and will dispatch events to those listeners. Because the structure is immutable, it is safe to use this API to add/remove listeners during the process of an event dispatch operation.
An example of how this class could be used to implement a new component which fires "action" events:
public MyComponent extends Component
{
EventListener listener;
public void addPropertyValueListener(PropertyListener l)
{
listener=EventMulticaster.add(listener,l);
}
public void removePropertyListener(PropertyListener l)
{
listener=EventMulticaster.remove(listener,l);
}
public void someMethod()
{
// When event occurs which causes "onEvent".
EventListener listener=this.listener;
if ( listener!=null )
listener.onEvent(new SomeEvent(...));
}
}
- Author:
- Christopher Mindus
Field Summary
Modifier and TypeFieldDescriptionprotected final EventListener
The two event listeners.protected final EventListener
The two event listeners.Constructor Summary
ModifierConstructorDescriptionprotected
Creates an event multicaster instance which chains listener-a with listener-b.Method Summary
Modifier and TypeMethodDescriptionstatic EventListener
add
(EventListener a, EventListener b) Adds listener-a with listener-b and returns the resulting multicast listener.protected static EventListener
Returns the resulting multicast listener from adding listener-a and listener-b together.void
Called when the event is fired.void
populate
(ArrayList<EventListener> listeners) Populates the array with listeners.protected EventListener
remove
(EventListener old) Removes a listener from this multicaster and returns the resulting multicast listener.static EventListener
remove
(EventListener l, EventListener old) Removes the old listener from listener-l and returns the resulting multicast listener.protected static EventListener
removeInternal
(EventListener l, EventListener old) Returns the resulting multicast listener after removing the old listener from listener-l.
Field Details
a
The two event listeners.b
The two event listeners.
Constructor Details
EventMulticaster
Creates an event multicaster instance which chains listener-a with listener-b.- Parameters:
a
- listener-a.b
- listener-b.
Method Details
remove
Removes a listener from this multicaster and returns the resulting multicast listener.- Parameters:
old
- the listener to be removed.
populate
Populates the array with listeners.onEvent
Called when the event is fired.- Specified by:
onEvent
in interfaceEventListener
- Parameters:
event
- The event.
add
Adds listener-a with listener-b and returns the resulting multicast listener.- Parameters:
a
- listener-a.b
- listener-b.
remove
Removes the old listener from listener-l and returns the resulting multicast listener.- Parameters:
l
- listener-l.old
- the listener being removed.
addInternal
Returns the resulting multicast listener from adding listener-a and listener-b together.- If listener-a is null, it returns listener-b.
- If listener-b is null, it returns listener-a.
- If neither are null, then it creates and returns a new EventMulticaster instance which chains a with b.
- Parameters:
a
- event-listener-a.b
- event-listener-b.
removeInternal
Returns the resulting multicast listener after removing the old listener from listener-l.- If listener-l equals the old listener OR listener-l is null, returns null.
- If listener-l is an instance of EventMulticaster, then it removes the old listener from it.
- Returns listener l.
- Parameters:
l
- the listener being removed from.old
- the listener being removed.