1 package edu.uci.iotproject.analysis;
3 import java.time.Instant;
4 import java.time.ZoneId;
5 import java.time.format.DateTimeFormatter;
8 * Models a user's action, such as toggling the smart plug on/off at a given time.
10 * @author Janus Varmarken
12 public class UserAction {
14 private static volatile DateTimeFormatter TIMESTAMP_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME.
15 withZone(ZoneId.of("America/Los_Angeles"));
18 * Sets the {@link DateTimeFormatter} used when outputting a user action as a string and parsing a user action from
20 * @param formatter The formatter to use for outputting and parsing.
22 public static void setTimestampFormatter(DateTimeFormatter formatter) {
23 TIMESTAMP_FORMATTER = formatter;
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.
31 public static UserAction fromString(String string) {
32 String[] parts = string.split("@");
33 if (parts.length != 2) {
34 throw new IllegalArgumentException("Invalid string format");
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);
44 * The specific type of action the user performed.
46 private final Type mType;
49 * The time the action took place.
51 private final Instant mTimestamp;
53 public UserAction(Type typeOfAction, Instant timeOfAction) {
55 mTimestamp = timeOfAction;
59 * Get the specific type of action performed by the user.
60 * @return the specific type of action performed by the user.
62 public Type getType() {
67 * Get the time at which the user performed this action.
68 * @return the time at which the user performed this action.
70 public Instant getTimestamp() {
75 * Enum for indicating what type of action the user performed.
83 public boolean equals(Object obj) {
87 if (obj instanceof UserAction) {
88 UserAction that = (UserAction) obj;
89 return this.mType == that.mType && this.mTimestamp.equals(that.mTimestamp);
96 public int hashCode() {
99 hashCode = prime * hashCode + mType.hashCode();
100 hashCode = prime * hashCode + mTimestamp.hashCode();
105 public String toString() {
106 return String.format("%s @ %s", mType.name(), TIMESTAMP_FORMATTER.format(mTimestamp));