@InterfaceAudience.Public @InterfaceStability.Evolving public final class MeanStatistic extends Object implements Serializable, Cloneable
It can be used to accrue values so as to dynamically update the mean. If so, know that there is no synchronization on the methods.
If a statistic has 0 samples then it is considered to be empty.
All 'empty' statistics are equivalent, independent of the sum value.
For non-empty statistics, sum and sample values must match for equality.
It is serializable and annotated for correct serializations with jackson2.
Thread safety. The operations to add/copy sample data, are thread safe.
add(MeanStatistic)
addSample(long)
clear()
setSamplesAndSum(long, long)
set(MeanStatistic)
setSamples(long)
and setSum(long)
So is the mean()
method. This ensures that when
used to aggregated statistics, the aggregate value and sample
count are set and evaluated consistently.
Other methods marked as synchronized because Findbugs overreacts to the idea that some operations to update sum and sample count are synchronized, but that things like equals are not.
Constructor and Description |
---|
MeanStatistic()
Create an empty statistic.
|
MeanStatistic(long samples,
long sum)
Constructor, with some resilience against invalid sample counts.
|
MeanStatistic(MeanStatistic that)
Create from another statistic.
|
Modifier and Type | Method and Description |
---|---|
MeanStatistic |
add(MeanStatistic other)
Add another MeanStatistic.
|
void |
addSample(long value)
Add a sample.
|
void |
clear()
Set the values to 0.
|
MeanStatistic |
clone() |
MeanStatistic |
copy()
Create a copy of this instance.
|
boolean |
equals(Object o) |
long |
getSamples()
Get the sample count.
|
long |
getSum()
Get the sum of samples.
|
int |
hashCode()
The hash code is derived from the mean
and sample count: if either is changed
the statistic cannot be used as a key
for hash tables/maps.
|
boolean |
isEmpty()
Is a statistic empty?
|
double |
mean()
Get the arithmetic mean value.
|
void |
set(MeanStatistic other)
Set the statistic to the values of another.
|
void |
setSamples(long samples)
Set the sample count.
|
void |
setSamplesAndSum(long sampleCount,
long newSum)
Set the sum and samples.
|
void |
setSum(long sum)
Set the sum.
|
String |
toString() |
public MeanStatistic(long samples, long sum)
samples
- sample count.sum
- sum valuepublic MeanStatistic(MeanStatistic that)
that
- sourcepublic MeanStatistic()
public long getSum()
public long getSamples()
public boolean isEmpty()
public void clear()
public void setSamplesAndSum(long sampleCount, long newSum)
sampleCount
- new sample count.newSum
- new sumpublic void set(MeanStatistic other)
other
- the source.public void setSum(long sum)
sum
- new sumpublic void setSamples(long samples)
samples
- sample count.public double mean()
public MeanStatistic add(MeanStatistic other)
other
- other valuepublic void addSample(long value)
value
- value to add to the sumpublic int hashCode()
public MeanStatistic clone()
public MeanStatistic copy()
Copyright © 2022 Apache Software Foundation. All rights reserved.