View Javadoc

1   package com.atlassian.johnson.event;
2   
3   import javax.annotation.Nonnull;
4   import javax.annotation.Nullable;
5   import javax.annotation.ParametersAreNonnullByDefault;
6   import java.util.Objects;
7   import java.util.function.Predicate;
8   
9   import static java.util.Arrays.asList;
10  
11  /**
12   * Predicates relating to Johnson events.
13   *
14   * @since 3.2
15   */
16  @ParametersAreNonnullByDefault
17  public final class EventPredicates {
18  
19      /**
20       * Checks whether the event has any of the given severity levels.
21       *
22       * @param levels the levels to compare against
23       * @return see above
24       * @since 3.2
25       */
26      @Nonnull
27      public static Predicate<Event> level(final EventLevel... levels) {
28          return event -> asList(levels).contains(event.getLevel());
29      }
30  
31      /**
32       * Checks whether the event has the given type.
33       *
34       * @param type the type to compare against
35       * @return see above
36       * @since 3.2
37       */
38      @Nonnull
39      public static Predicate<Event> type(final EventType type) {
40          return event -> type.equals(event.getKey());
41      }
42  
43      /**
44       * Checks whether the event has the given value of the given attribute. If the given value is <code>null</code>,
45       * the predicate returns <code>true</code> if and only if the event has a null value for that attribute (which
46       * includes not having any value for that attribute).
47       *
48       * @param name the name of the attribute to check
49       * @param value the value to check against
50       * @return see above
51       * @since 3.2
52       */
53      @Nonnull
54      public static Predicate<Event> attributeEquals(final String name, @Nullable final Object value) {
55          return event -> Objects.equals(value, event.getAttribute(name));
56      }
57  
58      private EventPredicates() {}
59  }