Replace long time_ms with Instant and Duration, respectively.

Fixes #561.
This commit is contained in:
Dennis Guse
2021-01-09 13:27:14 +01:00
parent 3bbcbfe202
commit e990b413cd
47 changed files with 442 additions and 434 deletions
@@ -19,6 +19,9 @@ package de.dennisguse.opentracks.stats;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.time.Duration;
import java.time.Instant;
/**
* Statistical data about a {@link de.dennisguse.opentracks.content.data.Track}.
* The data in this class should be filled out by {@link TrackStatisticsUpdater}.
@@ -33,15 +36,15 @@ public class TrackStatistics {
private final ExtremityMonitor elevationExtremities = new ExtremityMonitor();
// The track start time.
private long startTime_ms;
private Instant startTime;
// The track stop time.
private long stopTime_ms;
private Instant stopTime;
private double totalDistance_m;
// Updated when new points are received, may be stale.
private long totalTime_ms;
private Duration totalTime;
// Based on when we believe the user is traveling.
private long movingTime_ms;
private Duration movingTime;
// The maximum speed (meters/second) that we believe is valid.
private double maxSpeed_mps;
// The total elevation gained (meters).
@@ -60,11 +63,11 @@ public class TrackStatistics {
* @param other another statistics data object to copy from
*/
public TrackStatistics(TrackStatistics other) {
startTime_ms = other.startTime_ms;
stopTime_ms = other.stopTime_ms;
startTime = other.startTime;
stopTime = other.stopTime;
totalDistance_m = other.totalDistance_m;
totalTime_ms = other.totalTime_ms;
movingTime_ms = other.movingTime_ms;
totalTime = other.totalTime;
movingTime = other.movingTime;
maxSpeed_mps = other.maxSpeed_mps;
elevationExtremities.set(other.elevationExtremities.getMin(), other.elevationExtremities.getMax());
totalElevationGain_m = other.totalElevationGain_m;
@@ -78,11 +81,11 @@ public class TrackStatistics {
* @param other another statistics data object
*/
public void merge(TrackStatistics other) {
startTime_ms = Math.min(startTime_ms, other.startTime_ms);
stopTime_ms = Math.max(stopTime_ms, other.stopTime_ms);
startTime = startTime.isBefore(other.startTime) ? startTime : other.startTime;
stopTime = stopTime.isAfter(other.stopTime) ? stopTime : other.stopTime;
totalDistance_m += other.totalDistance_m;
totalTime_ms += other.totalTime_ms;
movingTime_ms += other.movingTime_ms;
totalTime = totalTime.plus(other.totalTime);
movingTime = movingTime.plus(other.movingTime);
maxSpeed_mps = Math.max(maxSpeed_mps, other.maxSpeed_mps);
if (other.elevationExtremities.hasData()) {
elevationExtremities.update(other.elevationExtremities.getMin());
@@ -109,44 +112,41 @@ public class TrackStatistics {
}
public void reset() {
setStartTime_ms(0);
setStartTime(Instant.ofEpochMilli(0)); //TODO should probably initialized with null
setTotalDistance(0);
setTotalTime(0);
setMovingTime(0);
setTotalTime(Duration.ofSeconds(0));
setMovingTime(Duration.ofSeconds(0));
setMaxSpeed(0);
setTotalElevationGain(null);
}
public void reset(long startTime_ms) {
public void reset(Instant startTime) {
reset();
setStartTime_ms(startTime_ms);
setStartTime(startTime);
}
/**
* Gets the track start time. The number of milliseconds since epoch.
*/
public long getStartTime_ms() {
return startTime_ms;
public Instant getStartTime() {
return startTime;
}
/**
* Should only be called on start.
*/
public void setStartTime_ms(long startTime_ms) {
this.startTime_ms = startTime_ms;
setStopTime_ms(startTime_ms);
public void setStartTime(Instant startTime) {
this.startTime = startTime;
setStopTime(startTime);
}
public long getStopTime_ms() {
return stopTime_ms;
public Instant getStopTime() {
return stopTime;
}
public void setStopTime_ms(long stopTime_ms) {
if (stopTime_ms < startTime_ms) {
throw new RuntimeException("stopTime cannot be less than startTime: " + startTime_ms + " " + stopTime_ms);
public void setStopTime(Instant stopTime) {
if (stopTime.isBefore(startTime)) {
throw new RuntimeException("stopTime cannot be less than startTime: " + startTime + " " + stopTime);
}
this.stopTime_ms = stopTime_ms;
this.stopTime = stopTime;
}
public double getTotalDistance() {
@@ -164,26 +164,26 @@ public class TrackStatistics {
/**
* Gets the total time in milliseconds that this track has been active.
* This statistic is only updated when a new point is added to the statistics, so it may be off.
* If you need to calculate the proper total time, use {@link #getStartTime_ms} with the current time.
* If you need to calculate the proper total time, use {@link #getStartTime} with the current time.
*/
public long getTotalTime() {
return totalTime_ms;
public Duration getTotalTime() {
return totalTime;
}
public void setTotalTime(long totalTime_ms) {
this.totalTime_ms = totalTime_ms;
public void setTotalTime(Duration totalTime) {
this.totalTime = totalTime;
}
public long getMovingTime() {
return movingTime_ms;
public Duration getMovingTime() {
return movingTime;
}
public void setMovingTime(long movingTime_ms) {
this.movingTime_ms = movingTime_ms;
public void setMovingTime(Duration movingTime) {
this.movingTime = movingTime;
}
public void addMovingTime(long time_ms) {
movingTime_ms += time_ms;
public void addMovingTime(Duration time) {
movingTime = movingTime.plus(time);
}
/**
@@ -191,20 +191,20 @@ public class TrackStatistics {
* This calculation only takes into account the displacement until the last point that was accounted for in statistics.
*/
public double getAverageSpeed() {
if (totalTime_ms == 0L) {
if (totalTime.isZero()) {
return 0.0;
}
return totalDistance_m / ((double) totalTime_ms / 1000.0);
return totalDistance_m / (double) totalTime.getSeconds();
}
/**
* Gets the average moving speed in meters/second.
*/
public double getAverageMovingSpeed() {
if (movingTime_ms == 0L) {
if (movingTime.isZero()) {
return 0.0;
}
return totalDistance_m / ((double) movingTime_ms / 1000.0);
return totalDistance_m / (double) movingTime.getSeconds();
}
/**
@@ -317,7 +317,7 @@ public class TrackStatistics {
@NonNull
@Override
public String toString() {
return "TrackStatistics { Start Time: " + getStartTime_ms() + "; Stop Time: " + getStopTime_ms()
return "TrackStatistics { Start Time: " + getStartTime() + "; Stop Time: " + getStopTime()
+ "; Total Distance: " + getTotalDistance() + "; Total Time: " + getTotalTime()
+ "; Moving Time: " + getMovingTime() + "; Max Speed: " + getMaxSpeed()
+ "; Min Elevation: " + getMinElevation() + "; Max Elevation: " + getMaxElevation()