♻️ refactor: Remove the maxSpeed and minSpeed properties to optimize the calculation of maximum and minimum speeds.
This commit is contained in:
parent
913ee64b5c
commit
b58e1774a1
1 changed files with 9 additions and 14 deletions
|
|
@ -11,10 +11,6 @@ public class AvgSpeed {
|
|||
|
||||
private final TreeMap<Long, SpeedPoint> speedPoints;
|
||||
|
||||
private long maxSpeed;
|
||||
|
||||
private long minSpeed;
|
||||
|
||||
private final int smoothingWindowSize;
|
||||
|
||||
/**
|
||||
|
|
@ -34,8 +30,6 @@ public class AvgSpeed {
|
|||
public AvgSpeed(int interval, int smoothingWindowSize) {
|
||||
this.interval = interval;
|
||||
this.speedPoints = new TreeMap<>();
|
||||
this.maxSpeed = 0;
|
||||
this.minSpeed = Long.MAX_VALUE;
|
||||
this.smoothingWindowSize = smoothingWindowSize;
|
||||
}
|
||||
|
||||
|
|
@ -54,12 +48,6 @@ public class AvgSpeed {
|
|||
speed = smoothSpeed(speed);
|
||||
}
|
||||
|
||||
// Update max/min speeds only for non-zero speeds
|
||||
if (speed > 0) {
|
||||
maxSpeed = Math.max(maxSpeed, speed);
|
||||
minSpeed = Math.min(minSpeed, speed);
|
||||
}
|
||||
|
||||
// Add new speed point
|
||||
speedPoints.put(timestamp, new SpeedPoint(downloadedSize, speed));
|
||||
|
||||
|
|
@ -162,14 +150,21 @@ public class AvgSpeed {
|
|||
* Get maximum recorded speed
|
||||
*/
|
||||
public long getMaxSpeed() {
|
||||
return maxSpeed;
|
||||
return speedPoints.values().stream()
|
||||
.map(point -> point.speed)
|
||||
.max(Long::compare)
|
||||
.orElse(0L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get minimum recorded speed
|
||||
*/
|
||||
public long getMinSpeed() {
|
||||
return minSpeed == Long.MAX_VALUE ? 0 : minSpeed;
|
||||
return speedPoints.values().stream()
|
||||
.map(point -> point.speed)
|
||||
.filter(speed -> speed > 0)
|
||||
.min(Long::compare)
|
||||
.orElse(0L);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue