Class ListenerList<E>

  • Type Parameters:
    E - the type of listeners in this list
    All Implemented Interfaces:
    Iterable<E>

    public class ListenerList<E>
    extends Object
    implements Iterable<E>
    This class is a thread safe list that is designed for storing lists of listeners. The implementation is optimized for minimal memory footprint, frequent reads and infrequent writes. Modification of the list is synchronized and relatively expensive, while accessing the listeners is very fast. For legacy code, readers are given access to the underlying array data structure for reading, with the trust that they will not modify the underlying array.

    A listener list handles the same listener being added multiple times, and tolerates removal of listeners that are the same as other listeners in the list. For this purpose, listeners can be compared with each other using either equality or identity, as specified in the list constructor.

    Use an enhanced 'for' loop to notify listeners. The recommended code sequence for notifying all registered listeners of say, FooListener#eventHappened(Event), is:

    ListenerList<FooListener> fooListeners = new ListenerList<>();
    //...
    for (FooListener listener : fooListeners) {
            listener.eventHappened(event);
    }
     

    Legacy code may still call getListeners() and then use a 'for' loop to iterate the Object[]. This might be insignificantly faster, but it lacks type-safety and risks inadvertent modifications to the array.

    This class can be used without OSGi running.

    Since:
    org.eclipse.equinox.common 3.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int EQUALITY
      Mode constant (value 0) indicating that listeners should be considered the same if they are equal.
      static int IDENTITY
      Mode constant (value 1) indicating that listeners should be considered the same if they are identical.
    • Constructor Summary

      Constructors 
      Constructor Description
      ListenerList()
      Creates a listener list in which listeners are compared using equality.
      ListenerList​(int mode)
      Creates a listener list using the provided comparison mode.
    • Field Detail

      • EQUALITY

        public static final int EQUALITY
        Mode constant (value 0) indicating that listeners should be considered the same if they are equal.
        See Also:
        Constant Field Values
      • IDENTITY

        public static final int IDENTITY
        Mode constant (value 1) indicating that listeners should be considered the same if they are identical.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ListenerList

        public ListenerList()
        Creates a listener list in which listeners are compared using equality.
      • ListenerList

        public ListenerList​(int mode)
        Creates a listener list using the provided comparison mode.
        Parameters:
        mode - The mode used to determine if listeners are the same.
    • Method Detail

      • add

        public void add​(E listener)
        Adds a listener to this list. This method has no effect if the same listener is already registered.
        Parameters:
        listener - the non-null listener to add
      • getListeners

        public Object[] getListeners()
        Returns an array containing all the registered listeners. The resulting array is unaffected by subsequent adds or removes. If there are no listeners registered, the result is an empty array. 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.

        Note: Callers of this method must not modify the returned array.

        Note: The recommended and type-safe way to iterate this list is to use an enhanced 'for' statement, see ListenerList. This method is deprecated for new code.

        Returns:
        the list of registered listeners
      • iterator

        public Iterator<E> iterator()
        Returns an iterator over all the registered listeners. The resulting iterator is unaffected by subsequent adds or removes. 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.
        Specified by:
        iterator in interface Iterable<E>
        Returns:
        an iterator
        Since:
        org.eclipse.equinox.common 3.8
      • 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 a listener from this list. Has no effect if the same listener was not already registered.
        Parameters:
        listener - the non-null listener to remove
      • size

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

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

        public Spliterator<E> spliterator()
        Returns a Spliterator covering the registered listeners.

        The spliterator reports Spliterator.SIZED, Spliterator.SUBSIZED, Spliterator.ORDERED, and Spliterator.IMMUTABLE

        Specified by:
        spliterator in interface Iterable<E>
        Returns:
        a spliterator for listeners
        Since:
        org.eclipse.equinox.common 3.9
      • stream

        public Stream<E> stream()
        Returns a sequential Stream over the registered listeners.
        Returns:
        a sequential Stream over the registered listeners.
        Since:
        org.eclipse.equinox.common 3.9
      • parallelStream

        public Stream<E> parallelStream()
        Returns a parallel Stream over the registered listeners.
        Returns:
        a parallel Stream over the registered listeners.
        Since:
        org.eclipse.equinox.common 3.9