Distance.ZERO as constant.

This commit is contained in:
Dennis Guse
2025-11-02 08:32:04 +01:00
parent fffeb40200
commit 6e4d46a14d
13 changed files with 30 additions and 28 deletions
@@ -125,6 +125,6 @@ public class SensorDataCyclingTest {
// TODO See #953 // TODO See #953
// assertEquals(2, current.getValue().getDistance().toM(), 0.01); // assertEquals(2, current.getValue().getDistance().toM(), 0.01);
// assertEquals(2, current.getValue().getSpeed().toMPS(), 0.01); // assertEquals(2, current.getValue().getSpeed().toMPS(), 0.01);
assertEquals(new AggregatorCyclingDistanceSpeed.Data(Distance.of(0), Distance.of(0), Speed.ZERO), current.getAggregatedValue(Instant.MIN)); assertEquals(new AggregatorCyclingDistanceSpeed.Data(Distance.ZERO, Distance.ZERO, Speed.ZERO), current.getAggregatedValue(Instant.MIN));
} }
} }
@@ -58,7 +58,7 @@ public class TrackRecordingServiceNotificationManagerTest {
null, null,
null, null,
null, null,
Distance.of(0), Distance.ZERO,
null, null,
null, null,
null, null,
@@ -110,7 +110,7 @@ public class TrackRecordingServiceRecordingTest {
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(startTime), Duration.ZERO, Duration.ZERO, Distance.of(0), Speed.ZERO, new AltitudeExtremities(0.0, 0.0), null, null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(startTime), Duration.ZERO, Duration.ZERO, Distance.ZERO, Speed.ZERO, new AltitudeExtremities(0.0, 0.0), null, null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
new TrackPointAssert().assertEquals(List.of( new TrackPointAssert().assertEquals(List.of(
@@ -124,7 +124,7 @@ public class TrackRecordingServiceRecordingTest {
service.endCurrentTrack(); service.endCurrentTrack();
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(stopTime), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.of(0), Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(stopTime), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.ZERO, Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
new TrackPointAssert().assertEquals(List.of( new TrackPointAssert().assertEquals(List.of(
@@ -199,7 +199,7 @@ public class TrackRecordingServiceRecordingTest {
service.endCurrentTrack(); service.endCurrentTrack();
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(pauseTime), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.of(0), Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(pauseTime), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.ZERO, Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
new TrackPointAssert().assertEquals(List.of( new TrackPointAssert().assertEquals(List.of(
@@ -214,7 +214,7 @@ public class TrackRecordingServiceRecordingTest {
service.resumeTrack(trackId); service.resumeTrack(trackId);
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(resumeTime), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.of(0), Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(resumeTime), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.ZERO, Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
new TrackPointAssert().assertEquals(List.of( new TrackPointAssert().assertEquals(List.of(
@@ -342,7 +342,7 @@ public class TrackRecordingServiceRecordingTest {
sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 1, 15); sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 1, 15);
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(gps1), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.of(0), Speed.of(15), new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(gps1), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.ZERO, Speed.of(15), new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
// when // when
@@ -416,7 +416,7 @@ public class TrackRecordingServiceRecordingTest {
sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 1, 15); sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 1, 15);
// then // then
Statistics gps1Statistics = new Statistics(Instant.parse(startTime), Instant.parse(gps1), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.of(0), Speed.of(15), new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null); Statistics gps1Statistics = new Statistics(Instant.parse(startTime), Instant.parse(gps1), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.ZERO, Speed.of(15), new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null);
assertEquals(gps1Statistics, contentProviderUtils.getTrack(trackId).getStatistics()); assertEquals(gps1Statistics, contentProviderUtils.getTrack(trackId).getStatistics());
// when // when
@@ -538,7 +538,7 @@ public class TrackRecordingServiceRecordingTest {
sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 100, 15); sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 100, 15);
// then // then
Statistics startStatistics = new Statistics(Instant.parse(startTime), Instant.parse(startTime), Duration.ofSeconds(0), Duration.ofSeconds(0), Distance.of(0), Speed.ZERO, new AltitudeExtremities(0.0, 0.0), null, null, null); Statistics startStatistics = new Statistics(Instant.parse(startTime), Instant.parse(startTime), Duration.ofSeconds(0), Duration.ofSeconds(0), Distance.ZERO, Speed.ZERO, new AltitudeExtremities(0.0, 0.0), null, null, null);
assertEquals(startStatistics, contentProviderUtils.getTrack(trackId).getStatistics()); assertEquals(startStatistics, contentProviderUtils.getTrack(trackId).getStatistics());
@@ -556,7 +556,7 @@ public class TrackRecordingServiceRecordingTest {
service.endCurrentTrack(); service.endCurrentTrack();
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(stopTime), Duration.ofSeconds(10), Duration.ofSeconds(10), Distance.of(0), Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(stopTime), Duration.ofSeconds(10), Duration.ofSeconds(10), Distance.ZERO, Speed.ZERO, new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
// then // then
@@ -582,7 +582,7 @@ public class TrackRecordingServiceRecordingTest {
sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 1, 15); sendGPSLocation(trackPointCreator, gps1, 45.0, 35.0, 1, 15);
// then // then
assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(gps1), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.of(0), Speed.of(15), new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null) assertEquals(new Statistics(Instant.parse(startTime), Instant.parse(gps1), Duration.ofSeconds(1), Duration.ofSeconds(1), Distance.ZERO, Speed.of(15), new AltitudeExtremities(0.0, 0.0), new AltitudeGainLoss(0, 0), null, null)
, contentProviderUtils.getTrack(trackId).getStatistics()); , contentProviderUtils.getTrack(trackId).getStatistics());
// when // when
@@ -647,7 +647,7 @@ public class TrackRecordingServiceRecordingTest {
// when // when
String sensor1 = "2020-02-02T02:02:03Z"; String sensor1 = "2020-02-02T02:02:03Z";
trackPointCreator.setClock(sensor1); trackPointCreator.setClock(sensor1);
sensorManager.onChanged(new Raw<>(sensor1, new BluetoothHandlerRunningSpeedAndCadence.Data(Speed.of(5), null, Distance.of(0)))); //Should be ignored sensorManager.onChanged(new Raw<>(sensor1, new BluetoothHandlerRunningSpeedAndCadence.Data(Speed.of(5), null, Distance.ZERO))); //Should be ignored
// when // when
String sensor2 = "2020-02-02T02:02:04Z"; String sensor2 = "2020-02-02T02:02:04Z";
@@ -704,7 +704,7 @@ public class TrackRecordingServiceRecordingTest {
null, null, null, null,
null, null,
Speed.of(5))) Speed.of(5)))
.setSensorDistance(Distance.of(0)), .setSensorDistance(Distance.ZERO),
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(sensor3)) new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(sensor3))
.setSpeed(Speed.of(5)) .setSpeed(Speed.of(5))
.setSensorDistance(Distance.of(10)), .setSensorDistance(Distance.of(10)),
@@ -723,7 +723,7 @@ public class TrackRecordingServiceRecordingTest {
null, null, null, null,
null, null,
Speed.of(5))) Speed.of(5)))
.setSensorDistance(Distance.of(0)), .setSensorDistance(Distance.ZERO),
new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL, new TrackPoint(TrackPoint.Type.SEGMENT_END_MANUAL,
new Position( new Position(
Instant.parse(stopTime), Instant.parse(stopTime),
@@ -731,7 +731,7 @@ public class TrackRecordingServiceRecordingTest {
null, null, null, null,
null, null,
Speed.ZERO)) //Sensor data is now outdated, but we do not fall back to GPS. Speed.ZERO)) //Sensor data is now outdated, but we do not fall back to GPS.
.setSensorDistance(Distance.of(0)) .setSensorDistance(Distance.ZERO)
), TestDataUtil.getTrackPoints(contentProviderUtils, trackId)); ), TestDataUtil.getTrackPoints(contentProviderUtils, trackId));
} }
@@ -288,7 +288,7 @@ public class TrackStatisticsUpdaterTest {
// when // when
subject.addTrackPoint( subject.addTrackPoint(
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.ofEpochSecond(40)) new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.ofEpochSecond(40))
.setSensorDistance(Distance.of(0))); .setSensorDistance(Distance.ZERO));
// then // then
assertTrue(subject.getSegmentStatisticUpdater().isIdle()); assertTrue(subject.getSegmentStatisticUpdater().isIdle());
assertEquals(Duration.ofSeconds(30), subject.getSegmentStatisticUpdater().getMovingTime()); assertEquals(Duration.ofSeconds(30), subject.getSegmentStatisticUpdater().getMovingTime());
@@ -128,7 +128,7 @@ public class IntervalStatisticsUpdaterTest {
intervalStatistics.addTrackPoints(trackPoints.iterator()); intervalStatistics.addTrackPoints(trackPoints.iterator());
List<Statistics> intervalList = intervalStatistics.getIntervalList(); List<Statistics> intervalList = intervalStatistics.getIntervalList();
Distance totalDistance = Distance.of(0); Distance totalDistance = Distance.ZERO;
float totalTime = 0L; float totalTime = 0L;
Float totalGain = null; Float totalGain = null;
Float totalLoss = null; Float totalLoss = null;
@@ -7,6 +7,8 @@ import de.dennisguse.opentracks.settings.UnitSystem;
public record Distance(double distance_m) { public record Distance(double distance_m) {
public static Distance ZERO = Distance.of(0);
public static Distance of(double distance_m) { public static Distance of(double distance_m) {
return new Distance(distance_m); return new Distance(distance_m);
} }
@@ -124,7 +124,7 @@ public class GPXTrackExporter implements TrackExporter {
private void writeTrackPoints(Track track) throws InterruptedException { private void writeTrackPoints(Track track) throws InterruptedException {
boolean wroteTrack = false; boolean wroteTrack = false;
boolean wroteSegment = false; boolean wroteSegment = false;
Distance trackDistance = Distance.of(0); Distance trackDistance = Distance.ZERO;
LinkedList<TrackPoint> sensorPoints = new LinkedList<>(); LinkedList<TrackPoint> sensorPoints = new LinkedList<>();
@@ -383,7 +383,7 @@ public class GPXTrackExporter implements TrackExporter {
if (cumulativeDistance != null) { if (cumulativeDistance != null) {
return cumulativeDistance; return cumulativeDistance;
} }
return Distance.of(0); return Distance.ZERO;
} }
private Double cumulateSensorData(TrackPoint trackPoint, List<TrackPoint> sensorPoints, Function<TrackPoint, Double> map) { private Double cumulateSensorData(TrackPoint trackPoint, List<TrackPoint> sensorPoints, Function<TrackPoint, Double> map) {
@@ -56,20 +56,20 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<BluetoothHandlerC
@Override @Override
protected void resetImmediate() { protected void resetImmediate() {
aggregatedValue = new Data(Distance.of(0), aggregatedValue.distanceOverall, Speed.ZERO); aggregatedValue = new Data(Distance.ZERO, aggregatedValue.distanceOverall, Speed.ZERO);
} }
@Override @Override
public void resetAggregated() { public void resetAggregated() {
if (aggregatedValue != null) { if (aggregatedValue != null) {
aggregatedValue = new Data(aggregatedValue.distance, Distance.of(0), aggregatedValue.speed); aggregatedValue = new Data(aggregatedValue.distance, Distance.ZERO, aggregatedValue.speed);
} }
} }
@NonNull @NonNull
@Override @Override
protected Data getNoneValue() { protected Data getNoneValue() {
return new Data(Distance.of(0), Distance.of(0), Speed.ZERO); return new Data(Distance.ZERO, Distance.ZERO, Speed.ZERO);
} }
public void setWheelCircumference(Distance wheelCircumference) { public void setWheelCircumference(Distance wheelCircumference) {
@@ -43,14 +43,14 @@ public final class AggregatorRunning extends Aggregator<BluetoothHandlerRunningS
@Override @Override
public void resetAggregated() { public void resetAggregated() {
if (aggregatedValue != null) { if (aggregatedValue != null) {
aggregatedValue = new Data(aggregatedValue.speed, aggregatedValue.cadence, Distance.of(0)); aggregatedValue = new Data(aggregatedValue.speed, aggregatedValue.cadence, Distance.ZERO);
} }
} }
@NonNull @NonNull
@Override @Override
protected Data getNoneValue() { protected Data getNoneValue() {
return new Data(Speed.ZERO, Cadence.of(0f), Distance.of(0)); return new Data(Speed.ZERO, Cadence.of(0f), Distance.ZERO);
} }
public record Data(Speed speed, Cadence cadence, @NonNull Distance distance) { public record Data(Speed speed, Cadence cadence, @NonNull Distance distance) {
@@ -187,7 +187,7 @@ public class SegmentStatisticUpdater {
totalTime = Duration.ofSeconds(0); totalTime = Duration.ofSeconds(0);
movingTime = Duration.ofSeconds(0); movingTime = Duration.ofSeconds(0);
totalDistance = Distance.of(0); totalDistance = Distance.ZERO;
maxSpeed = Speed.ZERO; maxSpeed = Speed.ZERO;
totalAltitudeGain_m = null; totalAltitudeGain_m = null;
totalAltitudeLoss_m = null; totalAltitudeLoss_m = null;
@@ -65,7 +65,7 @@ public class AggregatedStatistics {
this.countTracks = tracks.size(); this.countTracks = tracks.size();
totalMovingTime = tracks.stream().map(t -> t.getStatistics().movingTime()).reduce(Duration.ZERO, Duration::plus); totalMovingTime = tracks.stream().map(t -> t.getStatistics().movingTime()).reduce(Duration.ZERO, Duration::plus);
totalDistance = tracks.stream().map(t -> t.getStatistics().totalDistance()).reduce(Distance.of(0), Distance::plus); totalDistance = tracks.stream().map(t -> t.getStatistics().totalDistance()).reduce(Distance.ZERO, Distance::plus);
maxSpeed = tracks.stream().map(t -> t.getStatistics().maxSpeed()).max((o1, o2) -> (int) (o1.speed_mps() - o2.speed_mps())).orElse(Speed.ZERO); maxSpeed = tracks.stream().map(t -> t.getStatistics().maxSpeed()).max((o1, o2) -> (int) (o1.speed_mps() - o2.speed_mps())).orElse(Speed.ZERO);
avgMovingSpeed = Speed.of(totalDistance, totalMovingTime); avgMovingSpeed = Speed.of(totalDistance, totalMovingTime);
} }
@@ -45,7 +45,7 @@ public class SettingsCustomLayoutEditAdapter extends RecyclerView.Adapter<Recycl
Duration.ofMinutes(0), Duration.ofMinutes(0),
Duration.ofMinutes(0), Duration.ofMinutes(0),
Distance.of(0), Distance.ZERO,
Speed.ZERO, Speed.ZERO,
null, null,
@@ -103,7 +103,7 @@ public class IntervalStatisticsUpdater {
//TODO Could be replaced with Statistics? //TODO Could be replaced with Statistics?
@Deprecated @Deprecated
private static class Interval { private static class Interval {
private Distance distance = Distance.of(0); private Distance distance = Distance.ZERO;
private Duration time = Duration.ofSeconds(0); private Duration time = Duration.ofSeconds(0);
private Float gain_m; private Float gain_m;
private Float loss_m; private Float loss_m;