Class ListenerList


  • public class ListenerList
    extends Object
    This class is used to maintain a list of listeners, and is used in the implementations of several classes within JFace which allow you to register listeners of various kinds. It is a fairly lightweight object, occupying minimal space when no listeners are registered.

    Note that the add method checks for and eliminates duplicates based on identity (not equality). Likewise, the remove method compares based on identity.

    Use the getListeners method when notifying listeners. Note that no garbage is created if no listeners are registered. The recommended code sequence for notifying all registered listeners of say, FooListener.eventHappened, is:

     Object[] listeners = myListenerList.getListeners();
     for (int i = 0; i < listeners.length; ++i) {
        ((FooListener) listeners[i]).eventHappened(event);
     }
     
    • Constructor Detail

      • ListenerList

        public ListenerList()
        Creates a listener list with an initial capacity of 1.
      • ListenerList

        public ListenerList​(int capacity)
        Creates a listener list with the given initial capacity.
        Parameters:
        capacity - the number of listeners which this list can initially accept without growing its internal representation; must be at least 1
    • Method Detail

      • add

        public void add​(Object listener)
        Adds the given listener to this list. Has no effect if an identical listener is already registered.
        Parameters:
        listener - the listener
      • clear

        public void clear()
        Removes all listeners from this list.
      • getListeners

        public Object[] getListeners()
        Returns an array containing all the registered listeners, in the order in which they were added.

        The resulting array is unaffected by subsequent adds or removes. If there are no listeners registered, the result is an empty array singleton instance (no garbage is created). Use this method when notifying listeners, so that any modifications to the listener list during the notification will have no effect on the notification itself.

        Returns:
        the list of registered listeners
      • isEmpty

        public boolean isEmpty()
        Returns whether this listener list is empty.
        Returns:
        true if there are no registered listeners, and false otherwise
      • remove

        public void remove​(Object listener)
        Removes the given listener from this list. Has no effect if an identical listener was not already registered.
        Parameters:
        listener - the listener
      • size

        public int size()
        Returns the number of registered listeners.
        Returns:
        the number of registered listeners