Package com.google.android.exoplayer2.util

## Class ListenerSet<T>

- java.lang.Object
- - com.google.android.exoplayer2.util.ListenerSet<T>

- Type Parameters:
    `T` - The listener type.

      public final class ListenerSet<T>
      extends Object

  A set of listeners.

  Events are guaranteed to arrive in the order in which they happened even if a new event is triggered recursively from another listener.

  Events are also guaranteed to be only sent to the listeners registered at the time the event was enqueued and haven't been removed since.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static interface` | `ListenerSet.Event<T>` | An event sent to a listener.
    `static interface` | `ListenerSet.IterationFinishedEvent<T>` | An event sent to a listener when all other events sent during one `Looper` message queue iteration were handled by the listener.

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `ListenerSet​(Looper looper, Clock clock, ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent)` | Creates a new listener set.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `add​(T listener)` | Adds a listener to the set.
    `ListenerSet<T>` | `copy​(Looper looper, ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent)` | Copies the listener set.
    `void` | `flushEvents()` | Notifies listeners of events previously enqueued with `queueEvent(int, Event)`.
    `void` | `queueEvent​(int eventFlag, ListenerSet.Event<T> event)` | Adds an event that is sent to the listeners when `flushEvents()` is called.
    `void` | `release()` | Releases the set of listeners immediately.
    `void` | `remove​(T listener)` | Removes a listener from the set.
    `void` | `sendEvent​(int eventFlag, ListenerSet.Event<T> event)` | `Queues` a single event and immediately `flushes` the event queue to notify all listeners.
    `int` | `size()` | Returns the number of added listeners.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ListenerSet

          public ListenerSet​(Looper looper,
                             Clock clock,
                             ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent)

      Creates a new listener set.
      Parameters:
        `looper` - A `Looper` used to call listeners on. The same `Looper` must be used to call all other methods of this class.
        `clock` - A `Clock`.
        `iterationFinishedEvent` - An `ListenerSet.IterationFinishedEvent` sent when all other events sent during one `Looper` message queue iteration were handled by the listeners.

  - ### Method Detail

    - #### copy

          @CheckResult
          public ListenerSet<T> copy​(Looper looper,
                                     ListenerSet.IterationFinishedEvent<T> iterationFinishedEvent)

      Copies the listener set.
      Parameters:
        `looper` - The new `Looper` for the copied listener set.
        `iterationFinishedEvent` - The new `ListenerSet.IterationFinishedEvent` sent when all other events sent during one `Looper` message queue iteration were handled by the listeners.
      Returns:
        The copied listener set.

    - #### add

          public void add​(T listener)

      Adds a listener to the set.

      If a listener is already present, it will not be added again.

      Parameters:
        `listener` - The listener to be added.

    - #### remove

          public void remove​(T listener)

      Removes a listener from the set.

      If the listener is not present, nothing happens.

      Parameters:
        `listener` - The listener to be removed.

    - #### size

          public int size()

      Returns the number of added listeners.

    - #### queueEvent

          public void queueEvent​(int eventFlag,
                                 ListenerSet.Event<T> event)

      Adds an event that is sent to the listeners when `flushEvents()` is called.
      Parameters:
        `eventFlag` - An integer indicating the type of the event, or `C.INDEX_UNSET` to report this event without flag.
        `event` - The event.

    - #### flushEvents

          public void flushEvents()

      Notifies listeners of events previously enqueued with `queueEvent(int, Event)`.

    - #### sendEvent

          public void sendEvent​(int eventFlag,
                                ListenerSet.Event<T> event)

      `Queues` a single event and immediately `flushes` the event queue to notify all listeners.
      Parameters:
        `eventFlag` - An integer flag indicating the type of the event, or `C.INDEX_UNSET` to report this event without flag.
        `event` - The event.

    - #### release

          public void release()

      Releases the set of listeners immediately.

      This will ensure no events are sent to any listener after this method has been called.