1 package com.atlassian.johnson.event;
2
3 import com.atlassian.johnson.Johnson;
4 import org.apache.commons.lang.ObjectUtils;
5
6 import javax.annotation.Nonnull;
7 import javax.annotation.Nullable;
8 import javax.annotation.ParametersAreNonnullByDefault;
9
10 /**
11 * The severity of an {@link Event}.
12 *
13 * Applications using Johnson are free to define their own levels with their own
14 * semantics; they can use the constants below or not. However be aware that some
15 * of the pre-defined levels below have special meaning to the Johnson framework.
16 * For more details, search the source code for their usage.
17 */
18 @ParametersAreNonnullByDefault
19 public class EventLevel {
20
21 /**
22 * This level might indicate a non-fatal error, e.g. one that allows
23 * the application to start but with reduced functionality.
24 */
25 public static final String ERROR = "error";
26
27 /**
28 * This level typically means that the application is unable to start.
29 */
30 public static final String FATAL = "fatal";
31
32 /**
33 * This level typically indicates a condition that requires attention
34 * but does not materially affect the operation of the application.
35 */
36 public static final String WARNING = "warning";
37
38 /**
39 * Returns the configured {@link EventLevel} with the given name.
40 *
41 * @param level the name of the level to find
42 * @return <code>null</code> if no such level is defined by the application
43 * @throws IllegalStateException if Johnson has not been initialised
44 */
45 @Nullable
46 public static EventLevel get(final String level) {
47 return Johnson.getConfig().getEventLevel(level);
48 }
49
50 private final String description;
51 private final String level;
52
53 /**
54 * Constructor.
55 *
56 * @param level the internal name of the level in the Johnson configuration, e.g. {@link #FATAL}
57 * @param description a human-readable description of this level
58 */
59 public EventLevel(final String level, final String description) {
60 this.description = description;
61 this.level = level;
62 }
63
64 @Nonnull
65 public String getDescription() {
66 return description;
67 }
68
69 @Nonnull
70 public String getLevel() {
71 return level;
72 }
73
74 public boolean equals(final Object o) {
75 if (this == o) {
76 return true;
77 }
78 if (!(o instanceof EventLevel)) {
79 return false;
80 }
81
82 EventLevel e = (EventLevel) o;
83 return ObjectUtils.equals(getDescription(), e.getDescription()) &&
84 ObjectUtils.equals(getLevel(), e.getLevel());
85 }
86
87 public int hashCode() {
88 int result = 7;
89 result = 31 * result + ObjectUtils.hashCode(getLevel());
90 result = 31 * result + ObjectUtils.hashCode(getDescription());
91 return result;
92 }
93
94 public String toString() {
95 return "(EventLevel: " + level + ")";
96 }
97 }