Class 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
    • 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:
        onEvent in interface EventListener
        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.
        1. If listener-a is null, it returns listener-b.
        2. If listener-b is null, it returns listener-a.
        3. 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.
        1. If listener-l equals the old listener OR listener-l is null, returns null.
        2. If listener-l is an instance of EventMulticaster, then it removes the old listener from it.
        3. Returns listener l.
        Parameters:
        l - the listener being removed from.
        old - the listener being removed.