Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 3 | import com.google.common.base.MoreObjects; |
| 4 | import org.onlab.onos.event.AbstractEvent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 5 | |
| 6 | import java.util.Objects; |
| 7 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 8 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * A class to represent an intent related event. |
| 12 | */ |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 13 | public class IntentEvent extends AbstractEvent<IntentState, Intent> { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 14 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 15 | // TODO: determine a suitable parent class; if one does not exist, consider |
| 16 | // introducing one |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
| 18 | private final long time; |
| 19 | private final Intent intent; |
| 20 | private final IntentState state; |
| 21 | private final IntentState previous; |
| 22 | |
| 23 | /** |
| 24 | * Creates an event describing a state change of an intent. |
| 25 | * |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 26 | * @param intent subject intent |
| 27 | * @param state new intent state |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 28 | * @param previous previous intent state |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 29 | * @param time time the event created in milliseconds since start of epoch |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 30 | * @throws NullPointerException if the intent or state is null |
| 31 | */ |
| 32 | public IntentEvent(Intent intent, IntentState state, IntentState previous, long time) { |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 33 | super(state, intent); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | this.intent = checkNotNull(intent); |
| 35 | this.state = checkNotNull(state); |
| 36 | this.previous = previous; |
| 37 | this.time = time; |
| 38 | } |
| 39 | |
| 40 | /** |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 41 | * Returns the state of the intent which caused the event. |
| 42 | * |
| 43 | * @return the state of the intent |
| 44 | */ |
| 45 | public IntentState getState() { |
| 46 | return state; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the previous state of the intent which caused the event. |
| 51 | * |
| 52 | * @return the previous state of the intent |
| 53 | */ |
| 54 | public IntentState getPreviousState() { |
| 55 | return previous; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the intent associated with the event. |
| 60 | * |
| 61 | * @return the intent |
| 62 | */ |
| 63 | public Intent getIntent() { |
| 64 | return intent; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the time at which the event was created. |
| 69 | * |
| 70 | * @return the time in milliseconds since start of epoch |
| 71 | */ |
| 72 | public long getTime() { |
| 73 | return time; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean equals(Object o) { |
| 78 | if (this == o) { |
| 79 | return true; |
| 80 | } |
| 81 | if (o == null || getClass() != o.getClass()) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | IntentEvent that = (IntentEvent) o; |
| 86 | return Objects.equals(this.intent, that.intent) |
| 87 | && Objects.equals(this.state, that.state) |
| 88 | && Objects.equals(this.previous, that.previous) |
| 89 | && Objects.equals(this.time, that.time); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
| 94 | return Objects.hash(intent, state, previous, time); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return MoreObjects.toStringHelper(getClass()) |
| 100 | .add("intent", intent) |
| 101 | .add("state", state) |
| 102 | .add("previous", previous) |
| 103 | .add("time", time) |
| 104 | .toString(); |
| 105 | } |
| 106 | } |