Package com.iizix.event
Class EventMulticaster
- java.lang.Object
- com.iizix.event.EventMulticaster
- All Implemented Interfaces:
EventListener
public class EventMulticaster extends java.lang.Object implements 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
Fields Modifier and Type Field Description protected EventListeneraThe two event listeners.protected EventListenerbThe two event listeners.
Constructor Summary
Constructors Modifier Constructor Description protectedEventMulticaster(EventListener a, EventListener b)Creates an event multicaster instance which chains listener-a with listener-b.
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static EventListeneradd(EventListener a, EventListener b)Adds listener-a with listener-b and returns the resulting multicast listener.protected static EventListeneraddInternal(EventListener a, EventListener b)Returns the resulting multicast listener from adding listener-a and listener-b together.voidonEvent(GEvent event)Called when the event is fired.voidpopulate(java.util.ArrayList<EventListener> listeners)Populates the array with listeners.protected EventListenerremove(EventListener old)Removes a listener from this multicaster and returns the resulting multicast listener.static EventListenerremove(EventListener l, EventListener old)Removes the old listener from listener-l and returns the resulting multicast listener.protected static EventListenerremoveInternal(EventListener l, EventListener old)Returns the resulting multicast listener after removing the old listener from listener-l.
Field Detail
a
protected final EventListener a
The two event listeners.
b
protected final EventListener b
The two event listeners.
Constructor Detail
EventMulticaster
protected EventMulticaster(EventListener a, EventListener b)
Creates an event multicaster instance which chains listener-a with listener-b.- Parameters:
a- listener-a.b- listener-b.
Method Detail
remove
protected EventListener remove(EventListener old)
Removes a listener from this multicaster and returns the resulting multicast listener.- Parameters:
old- the listener to be removed.
populate
public void populate(java.util.ArrayList<EventListener> listeners)
Populates the array with listeners.
onEvent
public void onEvent(GEvent event)
Called when the event is fired.- Specified by:
onEventin interfaceEventListener- Parameters:
event- The event.
add
public static EventListener add(EventListener a, EventListener b)
Adds listener-a with listener-b and returns the resulting multicast listener.- Parameters:
a- listener-a.b- listener-b.
remove
public static EventListener remove(EventListener l, EventListener old)
Removes the old listener from listener-l and returns the resulting multicast listener.- Parameters:
l- listener-l.old- the listener being removed.
addInternal
protected static EventListener addInternal(EventListener a, EventListener b)
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
protected static EventListener removeInternal(EventListener l, EventListener old)
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.