001// Copyright (c) Choreo contributors
002
003package choreo.trajectory;
004
005import com.google.gson.JsonDeserializationContext;
006import com.google.gson.JsonDeserializer;
007import com.google.gson.JsonElement;
008import com.google.gson.JsonParseException;
009import java.lang.reflect.Type;
010
011// /** A marker for an event in a trajectory. */
012// public record EventMarker(double timestamp, String event) {
013//   /**
014//    * Returns a new EventMarker with the timestamp offset by the specified amount.
015//    *
016//    * @param timestampOffset The amount to offset the timestamp by.
017//    * @return A new EventMarker with the timestamp offset by the specified amount.
018//    */
019//   public EventMarker offsetBy(double timestampOffset) {
020//     return new EventMarker(timestamp + timestampOffset, event);
021//   }
022// }
023
024/** A marker for an event in a trajectory. */
025public class EventMarker {
026  /** GSON deserializer for choreolib event markers */
027  public static class Deserializer implements JsonDeserializer<EventMarker> {
028    /** Default constructor. */
029    public Deserializer() {}
030
031    public EventMarker deserialize(
032        JsonElement json, Type typeOfT, JsonDeserializationContext context)
033        throws JsonParseException {
034      try {
035        var targetTimestamp =
036            json.getAsJsonObject()
037                .get("from")
038                .getAsJsonObject()
039                .get("targetTimestamp")
040                .getAsDouble();
041        var offset =
042            json.getAsJsonObject()
043                .get("from")
044                .getAsJsonObject()
045                .get("offset")
046                .getAsJsonObject()
047                .get("val")
048                .getAsDouble();
049        var event = json.getAsJsonObject().get("name").getAsString();
050
051        return new EventMarker(targetTimestamp + offset, event);
052      } catch (IllegalStateException
053          | UnsupportedOperationException
054          | NullPointerException
055          | NumberFormatException e) {
056        return new EventMarker(-1, "");
057      }
058    }
059  }
060
061  /** The timestamp of the event. */
062  public final double timestamp;
063
064  /** The event. */
065  public final String event;
066
067  /**
068   * Constructs an EventMarker with the specified parameters.
069   *
070   * @param timestamp The timestamp of the event.
071   * @param event The event.
072   */
073  public EventMarker(double timestamp, String event) {
074    this.timestamp = timestamp;
075    this.event = event;
076  }
077
078  /**
079   * Returns a new EventMarker with the timestamp offset by the specified amount.
080   *
081   * @param timestampOffset The amount to offset the timestamp by.
082   * @return A new EventMarker with the timestamp offset by the specified amount.
083   */
084  public EventMarker offsetBy(double timestampOffset) {
085    return new EventMarker(timestamp + timestampOffset, event);
086  }
087
088  @Override
089  public boolean equals(Object obj) {
090    if (!(obj instanceof EventMarker)) {
091      return false;
092    }
093
094    var other = (EventMarker) obj;
095    return this.timestamp == other.timestamp && this.event.equals(other.event);
096  }
097}