summaryrefslogtreecommitdiffstats
path: root/mobile/android/exoplayer2/src/main/java/org/mozilla/thirdparty/com/google/android/exoplayer2/util/EventDispatcher.java
blob: 6e9a3798bf23a5309e837c8259ac22fa2ffca43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mozilla.thirdparty.com.google.android.exoplayer2.util;

import android.os.Handler;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Event dispatcher which allows listener registration.
 *
 * @param <T> The type of listener.
 */
public final class EventDispatcher<T> {

  /** Functional interface to send an event. */
  public interface Event<T> {

    /**
     * Sends the event to a listener.
     *
     * @param listener The listener to send the event to.
     */
    void sendTo(T listener);
  }

  /** The list of listeners and handlers. */
  private final CopyOnWriteArrayList<HandlerAndListener<T>> listeners;

  /** Creates an event dispatcher. */
  public EventDispatcher() {
    listeners = new CopyOnWriteArrayList<>();
  }

  /** Adds a listener to the event dispatcher. */
  public void addListener(Handler handler, T eventListener) {
    Assertions.checkArgument(handler != null && eventListener != null);
    removeListener(eventListener);
    listeners.add(new HandlerAndListener<>(handler, eventListener));
  }

  /** Removes a listener from the event dispatcher. */
  public void removeListener(T eventListener) {
    for (HandlerAndListener<T> handlerAndListener : listeners) {
      if (handlerAndListener.listener == eventListener) {
        handlerAndListener.release();
        listeners.remove(handlerAndListener);
      }
    }
  }

  /**
   * Dispatches an event to all registered listeners.
   *
   * @param event The {@link Event}.
   */
  public void dispatch(Event<T> event) {
    for (HandlerAndListener<T> handlerAndListener : listeners) {
      handlerAndListener.dispatch(event);
    }
  }

  private static final class HandlerAndListener<T> {

    private final Handler handler;
    private final T listener;

    private boolean released;

    public HandlerAndListener(Handler handler, T eventListener) {
      this.handler = handler;
      this.listener = eventListener;
    }

    public void release() {
      released = true;
    }

    public void dispatch(Event<T> event) {
      handler.post(
          () -> {
            if (!released) {
              event.sendTo(listener);
            }
          });
    }
  }
}