From 8b78902c1ea766b90304497efa1bed86aac68cf3 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sun, 13 Oct 2024 20:37:58 +0200 Subject: [PATCH] RunningAverage: basic cleanup of MedianMeanRunningAverage This fixes lack of synchronization in the copy constructor, removes all the empty javadoc and generally improves code style. --- .../math/MedianMeanRunningAverage.java | 134 ++++++++---------- 1 file changed, 58 insertions(+), 76 deletions(-) diff --git a/src/freenet/support/math/MedianMeanRunningAverage.java b/src/freenet/support/math/MedianMeanRunningAverage.java index 7de1ee50a60..7b38553762f 100644 --- a/src/freenet/support/math/MedianMeanRunningAverage.java +++ b/src/freenet/support/math/MedianMeanRunningAverage.java @@ -1,98 +1,80 @@ package freenet.support.math; import java.util.ArrayList; +import java.util.Collections; /** * A RunningAverage that tracks both the median and mean of a series of values. * WARNING: Uses memory and proportional to the number of reports! Only for debugging! * (Also uses CPU time O(N log N) with the number of reports in currentValue()). + * * @author Matthew Toseland (0xE43DA450) */ public final class MedianMeanRunningAverage implements RunningAverage, Cloneable { - private static final long serialVersionUID = 1L; - final ArrayList reports; - final TrivialRunningAverage mean; + private final ArrayList reports = new ArrayList<>(); + private final TrivialRunningAverage mean; - /** - * - */ - public MedianMeanRunningAverage() { - reports = new ArrayList(); - mean = new TrivialRunningAverage(); - } + public MedianMeanRunningAverage() { + mean = new TrivialRunningAverage(); + } - /** - * - * @param average - */ - public MedianMeanRunningAverage(MedianMeanRunningAverage average) { - this.mean = new TrivialRunningAverage(average.mean); - this.reports = new ArrayList(); - reports.addAll(average.reports); - } + public MedianMeanRunningAverage(MedianMeanRunningAverage other) { + synchronized (other.reports) { + this.mean = new TrivialRunningAverage(other.mean); + this.reports.addAll(other.reports); + } + } - @Override - public MedianMeanRunningAverage clone() { - // Override clone() for synchronization. - // Implement Cloneable to shut up findbugs. - synchronized (this) { - return new MedianMeanRunningAverage(this); - } - } + @Override + public MedianMeanRunningAverage clone() { + return new MedianMeanRunningAverage(this); + } - @Override - public synchronized long countReports() { - return reports.size(); - } + @Override + public long countReports() { + synchronized (reports) { + return reports.size(); + } + } - /** - * - * @return - */ - @Override - public synchronized double currentValue() { - int size = reports.size(); - int middle = size / 2; - java.util.Collections.sort(reports); - return reports.get(middle); - } + @Override + public double currentValue() { + synchronized (reports) { + int size = reports.size(); + int middle = size / 2; + Collections.sort(reports); + return reports.get(middle); + } + } - /** - * - * @param d - */ - @Override - public synchronized void report(double d) { - mean.report(d); - reports.add(d); - } + @Override + public void report(double d) { + synchronized (reports) { + mean.report(d); + reports.add(d); + } + } - /** - * - * @param d - */ - @Override - public void report(long d) { - report((double)d); - } + @Override + public void report(long d) { + report((double) d); + } - @Override - public double valueIfReported(double r) { - throw new UnsupportedOperationException(); - } - - @Override - public synchronized String toString() { - return "Median "+currentValue()+" mean "+mean.currentValue(); - } - - /** - * - * @return - */ - public synchronized double meanValue() { - return mean.currentValue(); - } + @Override + public double valueIfReported(double r) { + throw new UnsupportedOperationException(); + } + + @Override + public String toString() { + synchronized (reports) { + return "Median " + currentValue() + " mean " + meanValue(); + } + } + + public double meanValue() { + return mean.currentValue(); + } }