aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/event/Events.java
blob: b7166b0e79bfc881f0595509612c59c710b5c4f5 (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
package fr.umlv.java.wallj.event;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Events filtering utilities.
 *
 * @author Pacien TRAN-GIRARD
 */
public final class Events {
  /**
   * @param eventList  list of events to filter
   * @param eventClass event class to keep
   * @param <T>        type of events to keep
   * @return a filtered stream of events
   */
  public static <T extends Event> Stream<T> filter(List<Event> eventList, Class<T> eventClass) {
    return eventList.stream()
           .filter(e -> e.getClass().isAssignableFrom(eventClass))
           .map(eventClass::cast);
  }

  /**
   * @param eventList  list of events to filter
   * @param eventClass event class to keep
   * @param <T>        type of events to keep
   * @return any matching event
   */
  public static <T extends Event> Optional<T> findFirst(List<Event> eventList, Class<T> eventClass) {
    return filter(eventList, eventClass).findFirst();
  }

  private Events() {
    // static class
  }
}