Package com.day.util

Class ListenerList


  • public class ListenerList
    extends Object
    The ListenerList class provides a utility to maintain lists of registered listeners. It is fairly lightweight and should not impose to much memory overhead.

    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);
     }
     
    Since:
    iguana
    • Constructor Detail

      • ListenerList

        public ListenerList()
        Creates an instance of this listener list class.
    • Method Detail

      • addListener

        public boolean addListener​(Object listener)
        Adds a listener to the list of listeners if it is not yet registered in the list.
        Parameters:
        listener - The listener to add to the list. If null nothing is done.
        Returns:
        true if the listener has been added to the list
      • removeListener

        public boolean removeListener​(Object listener)
        Removes a listener from the list of listeners if it is contained.
        Parameters:
        listener - The listener to remove from the list. If null nothing is done.
        Returns:
        true if the listener has been added to the list
      • clear

        public void clear()
        Clears the list of registered listeners.
      • size

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

        public Object[] getListeners()
        Returns the list of registered listeners. Note that this method returns the same array for two subsequent calls if no listeners have been added or removed in the mean time. This also means, that callers of this method MUST NOT modify the array returned.
        Returns:
        the listeners
      • getListeners

        public Object[] getListeners​(Class requestedType)
        Returns the list of registered listeners in an array of the reqeusted runtime type. Note that this method returns the same array for two subsequent calls if no listeners have been added or removed in the mean time and if the element type of is assignment compatible. This also means, that callers of this method MUST NOT modify the array returned.
        Parameters:
        requestedType - The runtime type of the components of the array to return.
        Returns:
        the listeners
        Throws:
        ArrayStoreException - if the runtime type is not a supertype of the runtime type of every element in this list.