diff options
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java index 514876d..5cf5bb5 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/RollingAverage.java @@ -37,18 +37,46 @@ public class RollingAverage { } public void add(BigDecimal num) { - this.total = this.total.add(num); - this.samples.add(num); - if (this.samples.size() > this.size) { - this.total = this.total.subtract(this.samples.remove()); + synchronized (this) { + this.total = this.total.add(num); + this.samples.add(num); + if (this.samples.size() > this.size) { + this.total = this.total.subtract(this.samples.remove()); + } } } public double getAverage() { - if (this.samples.isEmpty()) { - return 0; + synchronized (this) { + if (this.samples.isEmpty()) { + return 0; + } + return this.total.divide(BigDecimal.valueOf(this.samples.size()), 30, RoundingMode.HALF_UP).doubleValue(); + } + } + + public double getMax() { + synchronized (this) { + BigDecimal max = BigDecimal.ZERO; + for (BigDecimal sample : this.samples) { + if (sample.compareTo(max) > 0) { + max = sample; + } + } + return max.doubleValue(); + } + } + + public double getMin() { + synchronized (this) { + BigDecimal min = BigDecimal.ZERO; + for (BigDecimal sample : this.samples) { + if (min == BigDecimal.ZERO || sample.compareTo(min) < 0) { + min = sample; + } + } + return min.doubleValue(); } - return this.total.divide(BigDecimal.valueOf(this.samples.size()), 30, RoundingMode.HALF_UP).doubleValue(); } } |