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