Speed.ZERO as constant.

This commit is contained in:
Dennis Guse
2025-11-02 08:30:15 +01:00
parent a5e5d0ca51
commit fffeb40200
15 changed files with 42 additions and 45 deletions
@@ -6,9 +6,11 @@ import de.dennisguse.opentracks.settings.UnitSystem;
public record Speed(double speed_mps) {
public static Speed ZERO = Speed.of(0);
public static Speed of(Distance distance, Duration duration) {
if (duration.isZero()) {
return zero();
return ZERO;
}
return new Speed(distance.toM() / (duration.toMillis() / (double) Duration.ofSeconds(1).toMillis()));
@@ -26,11 +28,6 @@ public record Speed(double speed_mps) {
return of(Distance.ofKilometer(speed_kmh), Duration.ofHours(1));
}
//TODO Like Duration.ZERO
public static Speed zero() {
return of(0.0);
}
public static Speed max(Speed speed1, Speed speed2) {
if (speed1.greaterThan(speed2)) {
return speed1;
@@ -40,7 +37,7 @@ public record Speed(double speed_mps) {
}
public boolean isZero() {
return speed_mps == 0;
return this.equals(ZERO);
}
public boolean isInvalid() {
@@ -50,7 +50,7 @@ public class SpeedFormatter {
String unitString = resources.getString(unitId);
if (speed == null) {
speed = Speed.zero();
speed = Speed.ZERO;
}
if (reportSpeedOrPace) {
@@ -36,7 +36,7 @@ public record Statistics(
public Speed getAverageSpeed() {
if (totalTime.isZero()) {
return Speed.of(0);
return Speed.ZERO;
}
return Speed.of(totalDistance, totalTime);
}
@@ -56,7 +56,7 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<BluetoothHandlerC
@Override
protected void resetImmediate() {
aggregatedValue = new Data(Distance.of(0), aggregatedValue.distanceOverall, Speed.zero());
aggregatedValue = new Data(Distance.of(0), aggregatedValue.distanceOverall, Speed.ZERO);
}
@Override
@@ -69,7 +69,7 @@ public class AggregatorCyclingDistanceSpeed extends Aggregator<BluetoothHandlerC
@NonNull
@Override
protected Data getNoneValue() {
return new Data(Distance.of(0), Distance.of(0), Speed.zero());
return new Data(Distance.of(0), Distance.of(0), Speed.ZERO);
}
public void setWheelCircumference(Distance wheelCircumference) {
@@ -37,7 +37,7 @@ public final class AggregatorRunning extends Aggregator<BluetoothHandlerRunningS
@Override
protected void resetImmediate() {
aggregatedValue = new Data(Speed.zero(), Cadence.of(0f), aggregatedValue.distance);
aggregatedValue = new Data(Speed.ZERO, Cadence.of(0f), aggregatedValue.distance);
}
@Override
@@ -50,7 +50,7 @@ public final class AggregatorRunning extends Aggregator<BluetoothHandlerRunningS
@NonNull
@Override
protected Data getNoneValue() {
return new Data(Speed.zero(), Cadence.of(0f), Distance.of(0));
return new Data(Speed.ZERO, Cadence.of(0f), Distance.of(0));
}
public record Data(Speed speed, Cadence cadence, @NonNull Distance distance) {
@@ -188,7 +188,7 @@ public class SegmentStatisticUpdater {
totalTime = Duration.ofSeconds(0);
movingTime = Duration.ofSeconds(0);
totalDistance = Distance.of(0);
maxSpeed = Speed.zero();
maxSpeed = Speed.ZERO;
totalAltitudeGain_m = null;
totalAltitudeLoss_m = null;
@@ -305,7 +305,7 @@ public class SegmentStatisticUpdater {
*/
public Speed getAverageSpeed() {
if (totalTime.isZero()) {
return Speed.of(0);
return Speed.ZERO;
}
return Speed.of(totalDistance.toM() / totalTime.getSeconds());
}
@@ -66,7 +66,7 @@ public class AggregatedStatistics {
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);
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);
}
@@ -46,7 +46,7 @@ public class SettingsCustomLayoutEditAdapter extends RecyclerView.Adapter<Recycl
Duration.ofMinutes(0),
Distance.of(0),
Speed.of(0),
Speed.ZERO,
null,
new AltitudeGainLoss(0f, 0f),
@@ -64,7 +64,7 @@ public class SettingsCustomLayoutEditAdapter extends RecyclerView.Adapter<Recycl
Altitude.EGM2008.of(0),
null,
null,
Speed.zero()
Speed.ZERO
);
demoData = new RecordingData(track, new TrackPoint(TrackPoint.Type.TRACKPOINT, position), null, statistics);