Class ListenerList

java.lang.Object
org.nuxeo.common.collections.ListenerList

public class ListenerList extends Object
Internal class to maintain a list of listeners. This class is a thread-safe list that is optimized for frequent reads and infrequent writes. Copy on write is used to ensure readers can access the list without synchronization overhead. Readers are given access to the underlying array data structure for reading, with the trust that they will not modify the underlying array.

Contains code from Eclipse org.eclipse.core.runtime.ListenerList class

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Mode constant (value 0) indicating that listeners should be compared using equality.
    static final int
    Mode constant (value 1) indicating that listeners should be compared using identity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a listener list.
    ListenerList(int mode, Comparator<?> comparator)
    Creates a listener list using the provided comparison mode.
    ListenerList(Comparator<?> comparator)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Object listener)
    Adds the given listener to this list.
    Returns an array containing all the registered listeners.
     
    boolean
    Returns whether this listener list is empty.
    void
    remove(Object listener)
    Removes the given listener from this list.
    int
    Returns the number of registered listeners.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • EQUALITY

      public static final int EQUALITY
      Mode constant (value 0) indicating that listeners should be compared using equality.
      See Also:
    • IDENTITY

      public static final int IDENTITY
      Mode constant (value 1) indicating that listeners should be compared using identity.
      See Also:
  • Constructor Details

    • ListenerList

      public ListenerList()
      Creates a listener list.
    • ListenerList

      public ListenerList(Comparator<?> comparator)
    • ListenerList

      public ListenerList(int mode, Comparator<?> comparator)
      Creates a listener list using the provided comparison mode.
  • Method Details

    • add

      public void add(Object listener)
      Adds the given listener to this list. Has no effect if an equal listener is already registered.

      This method is synchronized to protect against multiple threads adding or removing listeners concurrently. This does not block concurrent readers.

      Parameters:
      listener - the 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 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.

      Note: callers must not modify the returned array.

      Returns:
      the list of registered listeners
    • getListenersCopy

      public Object[] getListenersCopy()
    • 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.

      This method is synchronized to protect against multiple threads adding or removing listeners concurrently. This does not block concurrent readers.

      Parameters:
      listener - the listener
    • size

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