Traversal implements Serializable { public double getSpeedAtTime(double time) { if (DoubleMath.fuzzyEquals(time, totalDuration, totalDuration * ROUNDING_ERROR_FRACTION)) { TraversalSection lastSection = Iterables.getLast(sections); return lastSection.getSpeedAtTime(lastSection.getDuration()); } if (time > totalDuration) { return 0; } if (time < 0) { throw new TraversalCalculationException("Negative time provided"); } return SegmentGraphUtilities.getValueAt( sections, TraversalSection::getDuration, TraversalSection::getSpeedAtTime, time); } Traversal(ImmutableList<TraversalSection> sections); ImmutableList<TraversalSection> getSections(); double getTotalDuration(); double getTotalDistance(); double getDistanceAtTime(double time); double getTimeAtDistance(double distance); double getSpeedAtTime(double time); double getSpeedAtDistance(double distance); double getAccelerationAtTime(double time); double getAccelerationAtDistance(double distance); double getDurationAccelerating(); double getDurationAtConstantSpeed(); double getDurationDecelerating(); static final Traversal EMPTY_TRAVERSAL; }
@Test void getSpeedAtTime_returnsExpectedValue() { assertThat(constantAccelerationTraversal.getSpeedAtTime(1.2)).isCloseTo(2.4, within(0.000001)); assertThat(constantAccelerationTraversal.getSpeedAtTime(2.4)).isCloseTo(4, within(0.000001)); }
@Test void getSpeedAtTime_whenInputNegative_thenThrowsException() { assertThatThrownBy(() -> constantAccelerationTraversal.getSpeedAtTime(-1)).isInstanceOf(TraversalCalculationException.class); }
@Test void getSpeedAtTime_whenInputBeyondMax_thenReturnsZero() { assertThat(constantAccelerationTraversal.getSpeedAtTime(1000)).isCloseTo(0, within(1e-9)); }
@Test void getSpeedAtTime_realExample() { assertThat(realTraversal.getSpeedAtTime(4968.894977362377)).isCloseTo(8.000102181418545E-5, within(1e-9)); }