Class MeanStatistic
- All Implemented Interfaces:
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)andsetSum(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.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate an empty statistic.MeanStatistic(long samples, long sum) Constructor, with some resilience against invalid sample counts.MeanStatistic(MeanStatistic that) Create from another statistic. -
Method Summary
Modifier and TypeMethodDescriptionadd(MeanStatistic other) Add another MeanStatistic.voidaddSample(long value) Add a sample.voidclear()Set the values to 0.clone()copy()Create a copy of this instance.booleanlongGet the sample count.longgetSum()Get the sum of samples.inthashCode()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.booleanisEmpty()Is a statistic empty?doublemean()Get the arithmetic mean value.voidset(MeanStatistic other) Set the statistic to the values of another.voidsetSamples(long samples) Set the sample count.voidsetSamplesAndSum(long sampleCount, long newSum) Set the sum and samples.voidsetSum(long sum) Set the sum.toString()
-
Constructor Details
-
MeanStatistic
public MeanStatistic(long samples, long sum) Constructor, with some resilience against invalid sample counts. If the sample count is 0 or less, the sum is set to 0 and the sample count to 0.- Parameters:
samples- sample count.sum- sum value
-
MeanStatistic
Create from another statistic.- Parameters:
that- source
-
MeanStatistic
public MeanStatistic()Create an empty statistic.
-
-
Method Details
-
getSum
public long getSum()Get the sum of samples.- Returns:
- the sum
-
getSamples
public long getSamples()Get the sample count.- Returns:
- the sample count; 0 means empty
-
isEmpty
public boolean isEmpty()Is a statistic empty?- Returns:
- true if the sample count is 0
-
clear
public void clear()Set the values to 0. -
setSamplesAndSum
public void setSamplesAndSum(long sampleCount, long newSum) Set the sum and samples. Synchronized.- Parameters:
sampleCount- new sample count.newSum- new sum
-
set
Set the statistic to the values of another. Synchronized.- Parameters:
other- the source.
-
setSum
public void setSum(long sum) Set the sum.- Parameters:
sum- new sum
-
setSamples
public void setSamples(long samples) Set the sample count. If this is less than zero, it is set to zero. This stops an ill-formed JSON entry from breaking deserialization, or get an invalid sample count into an entry.- Parameters:
samples- sample count.
-
mean
public double mean()Get the arithmetic mean value.- Returns:
- the mean
-
add
Add another MeanStatistic.- Parameters:
other- other value- Returns:
- mean statistic.
-
addSample
public void addSample(long value) Add a sample. Thread safe.- Parameters:
value- value to add to the sum
-
hashCode
public 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. -
equals
-
clone
-
copy
Create a copy of this instance.- Returns:
- copy.
-
toString
-