001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.math.trajectory.proto;
006
007import edu.wpi.first.math.proto.Trajectory.ProtobufTrajectory;
008import edu.wpi.first.math.proto.Trajectory.ProtobufTrajectoryState;
009import edu.wpi.first.math.trajectory.Trajectory;
010import edu.wpi.first.util.protobuf.Protobuf;
011import java.util.ArrayList;
012import us.hebi.quickbuf.Descriptors.Descriptor;
013
014public class TrajectoryProto implements Protobuf<Trajectory, ProtobufTrajectory> {
015  @Override
016  public Class<Trajectory> getTypeClass() {
017    return Trajectory.class;
018  }
019
020  @Override
021  public Descriptor getDescriptor() {
022    return ProtobufTrajectory.getDescriptor();
023  }
024
025  @Override
026  public ProtobufTrajectory createMessage() {
027    return ProtobufTrajectory.newInstance();
028  }
029
030  @Override
031  public Trajectory unpack(ProtobufTrajectory msg) {
032    ArrayList<Trajectory.State> states = new ArrayList<>(msg.getStates().length());
033    for (ProtobufTrajectoryState protoState : msg.getStates()) {
034      states.add(Trajectory.State.proto.unpack(protoState));
035    }
036
037    return new Trajectory(states);
038  }
039
040  @Override
041  public void pack(ProtobufTrajectory msg, Trajectory value) {
042    var states = msg.getMutableStates().reserve(value.getStates().size());
043    for (var item : value.getStates()) {
044      Trajectory.State.proto.pack(states.next(), item);
045    }
046  }
047}