Renaming root project name.
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / analysis / UserAction.java
1 package edu.uci.iotproject.analysis;
2
3 import java.time.Instant;
4 import java.time.ZoneId;
5 import java.time.format.DateTimeFormatter;
6
7 /**
8  * Models a user's action, such as toggling the smart plug on/off at a given time.
9  *
10  * @author Janus Varmarken
11  */
12 public class UserAction {
13
14     private static volatile DateTimeFormatter TIMESTAMP_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME.
15             withZone(ZoneId.of("America/Los_Angeles"));
16
17     /**
18      * Sets the {@link DateTimeFormatter} used when outputting a user action as a string and parsing a user action from
19      * a string.
20      * @param formatter The formatter to use for outputting and parsing.
21      */
22     public static void setTimestampFormatter(DateTimeFormatter formatter) {
23         TIMESTAMP_FORMATTER = formatter;
24     }
25
26     /**
27      * Instantiates a {@code UserAction} from a string that obeys the format used in {@link UserAction#toString()}.
28      * @param string The string that represents a {@code UserAction}
29      * @return A {@code UserAction} resulting from deserializing the string.
30      */
31     public static UserAction fromString(String string) {
32         String[] parts = string.split("@");
33         if (parts.length != 2) {
34             throw new IllegalArgumentException("Invalid string format");
35         }
36         // If any of these two parses fail, an exception is thrown -- no need to check return values.
37         UserAction.Type actionType = UserAction.Type.valueOf(parts[0].trim());
38         Instant timestamp = TIMESTAMP_FORMATTER.parse(parts[1].trim(), Instant::from);
39         return new UserAction(actionType, timestamp);
40     }
41
42
43     /**
44      * The specific type of action the user performed.
45      */
46     private final Type mType;
47
48     /**
49      * The time the action took place.
50      */
51     private final Instant mTimestamp;
52
53     public UserAction(Type typeOfAction, Instant timeOfAction) {
54         mType = typeOfAction;
55         mTimestamp = timeOfAction;
56     }
57
58     /**
59      * Get the specific type of action performed by the user.
60      * @return the specific type of action performed by the user.
61      */
62     public Type getType() {
63         return mType;
64     }
65
66     /**
67      * Get the time at which the user performed this action.
68      * @return the time at which the user performed this action.
69      */
70     public Instant getTimestamp() {
71         return mTimestamp;
72     }
73
74     /**
75      * Enum for indicating what type of action the user performed.
76      */
77     public enum Type {
78         TOGGLE_ON, TOGGLE_OFF
79     }
80
81
82     @Override
83     public boolean equals(Object obj) {
84         if (this == obj) {
85             return true;
86         }
87         if (obj instanceof UserAction) {
88             UserAction that = (UserAction) obj;
89             return this.mType == that.mType && this.mTimestamp.equals(that.mTimestamp);
90         } else {
91             return false;
92         }
93     }
94
95     @Override
96     public int hashCode() {
97         final int prime = 31;
98         int hashCode = 17;
99         hashCode = prime * hashCode + mType.hashCode();
100         hashCode = prime * hashCode + mTimestamp.hashCode();
101         return hashCode;
102     }
103
104     @Override
105     public String toString() {
106        return String.format("%s @ %s", mType.name(), TIMESTAMP_FORMATTER.format(mTimestamp));
107     }
108 }