mirror of
https://github.com/google/nomulus.git
synced 2025-05-07 23:38:21 +02:00
Allow EventSample.record to accept numSamples=0
There's really no reason not to. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=171037754
This commit is contained in:
parent
6b113603db
commit
0b5b16e97c
2 changed files with 9 additions and 2 deletions
|
@ -41,7 +41,7 @@ public final class MutableDistribution implements Distribution {
|
|||
private final DistributionFitter distributionFitter;
|
||||
private double sumOfSquaredDeviation = 0.0;
|
||||
private double mean = 0.0;
|
||||
private int count = 0;
|
||||
private long count = 0;
|
||||
|
||||
/** Constructs an empty Distribution with the specified {@link DistributionFitter}. */
|
||||
public MutableDistribution(DistributionFitter distributionFitter) {
|
||||
|
@ -70,9 +70,15 @@ public final class MutableDistribution implements Distribution {
|
|||
}
|
||||
|
||||
public void add(double value, long numSamples) {
|
||||
checkArgument(numSamples > 0, "numSamples must be greater than 0");
|
||||
checkArgument(numSamples >= 0, "numSamples must be non-negative");
|
||||
checkDouble(value);
|
||||
|
||||
// having numSamples = 0 works as expected (does nothing) even if we let it continue, but we
|
||||
// can short-circuit it by returning early.
|
||||
if (numSamples == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map.Entry<Range<Double>, Long> entry = intervalCounts.getEntry(value);
|
||||
intervalCounts.put(entry.getKey(), entry.getValue() + numSamples);
|
||||
this.count += numSamples;
|
||||
|
|
|
@ -109,6 +109,7 @@ public class MutableDistributionTest {
|
|||
distribution.add(2.0);
|
||||
distribution.add(16.0);
|
||||
distribution.add(128.0, 5);
|
||||
distribution.add(1024.0, 0);
|
||||
|
||||
assertThat(distribution.count()).isEqualTo(7);
|
||||
assertThat(distribution.mean()).isWithin(0.0).of(94.0);
|
||||
|
|
Loading…
Add table
Reference in a new issue