Class LongStatistics
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.LongStatistics
-
- All Implemented Interfaces:
java.util.function.LongConsumer
public final class LongStatistics extends java.lang.Object implements java.util.function.LongConsumer
Statistics forlong
values.This class provides combinations of individual statistic implementations in the
org.apache.commons.statistics.descriptive
package.Supports up to 263 (exclusive) observations. This implementation does not check for overflow of the count.
- Since:
- 1.1
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LongStatistics.Builder
A builder forLongStatistics
.
-
Field Summary
Fields Modifier and Type Field Description private StatisticsConfiguration
config
Configuration options for computation of statistics.private java.util.function.LongConsumer
consumer
The consumer of values.private long
count
Count of values recorded.private LongMax
max
TheLongMax
implementation.private LongMin
min
TheLongMin
implementation.private FirstMoment
moment
The moment implementation.private static java.lang.String
NO_CONFIGURED_STATISTICS
Error message for non configured statistics.private Product
product
TheProduct
implementation.private LongSum
sum
TheLongSum
implementation.private SumOfLogs
sumOfLogs
TheSumOfLogs
implementation.private LongSumOfSquares
sumOfSquares
TheLongSumOfSquares
implementation.private static java.lang.String
UNSUPPORTED_STATISTIC
Error message for an unsupported statistic.
-
Constructor Summary
Constructors Constructor Description LongStatistics(long count, LongMin min, LongMax max, FirstMoment moment, LongSum sum, Product product, LongSumOfSquares sumOfSquares, SumOfLogs sumOfLogs, StatisticsConfiguration config)
Create an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(long value)
Updates the state of the statistics to reflect the addition ofvalue
.static LongStatistics.Builder
builder(Statistic... statistics)
Returns a new builder configured to create instances to compute the specifiedstatistics
.LongStatistics
combine(LongStatistics other)
Combines the state of theother
statistics into this one.private static java.util.function.LongConsumer
composeAsLong(java.util.function.DoubleConsumer... consumers)
Chain theconsumers
into a single compositeLongConsumer
.java.math.BigInteger
getAsBigInteger(Statistic statistic)
Gets the value of the specifiedstatistic
as aBigInteger
.double
getAsDouble(Statistic statistic)
Gets the value of the specifiedstatistic
as adouble
.long
getAsLong(Statistic statistic)
Gets the value of the specifiedstatistic
as along
.long
getCount()
Return the count of values recorded.private StatisticResult
getGeometricMean()
Gets the geometric mean.private StatisticResult
getKurtosis()
Gets the kurtosis.private StatisticResult
getMean()
Gets the mean.StatisticResult
getResult(Statistic statistic)
Gets a supplier for the value of the specifiedstatistic
.private StatisticResult
getSkewness()
Gets the skewness.private StatisticResult
getStandardDeviation()
Gets the standard deviation.private StatisticResult
getVariance()
Gets the variance.private StatisticResult
getVarianceOrStd(boolean std)
Gets the variance or standard deviation.boolean
isSupported(Statistic statistic)
Check if the specifiedstatistic
is supported.static LongStatistics
of(java.util.Set<Statistic> statistics, long... values)
Returns a new instance configured to compute the specifiedstatistics
populated using the inputvalues
.static LongStatistics
of(Statistic... statistics)
Returns a new instance configured to compute the specifiedstatistics
.LongStatistics
setConfiguration(StatisticsConfiguration v)
Sets the statistics configuration.
-
-
-
Field Detail
-
NO_CONFIGURED_STATISTICS
private static final java.lang.String NO_CONFIGURED_STATISTICS
Error message for non configured statistics.- See Also:
- Constant Field Values
-
UNSUPPORTED_STATISTIC
private static final java.lang.String UNSUPPORTED_STATISTIC
Error message for an unsupported statistic.- See Also:
- Constant Field Values
-
count
private long count
Count of values recorded.
-
consumer
private final java.util.function.LongConsumer consumer
The consumer of values.
-
moment
private final FirstMoment moment
The moment implementation. May be any instance ofFirstMoment
. This implementation uses only the third and fourth moments.
-
sumOfSquares
private final LongSumOfSquares sumOfSquares
TheLongSumOfSquares
implementation.
-
config
private StatisticsConfiguration config
Configuration options for computation of statistics.
-
-
Constructor Detail
-
LongStatistics
LongStatistics(long count, LongMin min, LongMax max, FirstMoment moment, LongSum sum, Product product, LongSumOfSquares sumOfSquares, SumOfLogs sumOfLogs, StatisticsConfiguration config)
Create an instance.- Parameters:
count
- Count of values.min
- LongMin implementation.max
- LongMax implementation.moment
- Moment implementation.sum
- LongSum implementation.product
- Product implementation.sumOfSquares
- Sum of squares implementation.sumOfLogs
- Sum of logs implementation.config
- Statistics configuration.
-
-
Method Detail
-
composeAsLong
private static java.util.function.LongConsumer composeAsLong(java.util.function.DoubleConsumer... consumers)
Chain theconsumers
into a single compositeLongConsumer
. Ignore anynull
consumer.- Parameters:
consumers
- Consumers.- Returns:
- a composed consumer (or null)
-
of
public static LongStatistics of(Statistic... statistics)
Returns a new instance configured to compute the specifiedstatistics
.The statistics will be empty and so will return the default values for each computed statistic.
- Parameters:
statistics
- Statistics to compute.- Returns:
- the instance
- Throws:
java.lang.IllegalArgumentException
- if there are nostatistics
to compute.
-
of
public static LongStatistics of(java.util.Set<Statistic> statistics, long... values)
Returns a new instance configured to compute the specifiedstatistics
populated using the inputvalues
.Use this method to create an instance populated with a (variable) array of
long[]
data:LongStatistics stats = LongStatistics.of( EnumSet.of(Statistic.MIN, Statistic.MAX), 1, 1, 2, 3, 5, 8, 13);
- Parameters:
statistics
- Statistics to compute.values
- Values.- Returns:
- the instance
- Throws:
java.lang.IllegalArgumentException
- if there are nostatistics
to compute.
-
builder
public static LongStatistics.Builder builder(Statistic... statistics)
Returns a new builder configured to create instances to compute the specifiedstatistics
.Use this method to create an instance populated with an array of
long[]
data using theLongStatistics.Builder.build(long...)
method:long[] data = ... LongStatistics stats = LongStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE) .build(data);
The builder can be used to create multiple instances of
LongStatistics
to be used in parallel, or on separate arrays oflong[]
data. These may becombined
. For example:long[][] data = ... LongStatistics.Builder builder = LongStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); LongStatistics stats = Arrays.stream(data) .parallel() .map(builder::build) .reduce(LongStatistics::combine) .get();
The builder can be used to create a
Collector
for repeat use on multiple data:LongStatistics.Builder builder = LongStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); Collector<long[], LongStatistics, LongStatistics> collector = Collector.of(builder::build, (s, d) -> s.combine(builder.build(d)), LongStatistics::combine); // Repeated long[][] data = ... LongStatistics stats = Arrays.stream(data).collect(collector);
- Parameters:
statistics
- Statistics to compute.- Returns:
- the builder
- Throws:
java.lang.IllegalArgumentException
- if there are nostatistics
to compute.
-
accept
public void accept(long value)
Updates the state of the statistics to reflect the addition ofvalue
.- Specified by:
accept
in interfacejava.util.function.LongConsumer
- Parameters:
value
- Value.
-
getCount
public long getCount()
Return the count of values recorded.- Returns:
- the count of values
-
isSupported
public boolean isSupported(Statistic statistic)
Check if the specifiedstatistic
is supported.Note: This method will not return
false
if the argument isnull
.- Parameters:
statistic
- Statistic.- Returns:
true
if supported- Throws:
java.lang.NullPointerException
- if thestatistic
isnull
- See Also:
getResult(Statistic)
-
getAsDouble
public double getAsDouble(Statistic statistic)
Gets the value of the specifiedstatistic
as adouble
.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
java.lang.IllegalArgumentException
- if thestatistic
is not supported- See Also:
isSupported(Statistic)
,getResult(Statistic)
-
getAsLong
public long getAsLong(Statistic statistic)
Gets the value of the specifiedstatistic
as along
.Use this method to access the
long
result for exact integer statistics, for exampleStatistic.MIN
.Note: This method may throw an
ArithmeticException
if the result overflows anlong
.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
java.lang.IllegalArgumentException
- if thestatistic
is not supportedjava.lang.ArithmeticException
- if theresult
overflows anlong
or is not finite- See Also:
isSupported(Statistic)
,getResult(Statistic)
-
getAsBigInteger
public java.math.BigInteger getAsBigInteger(Statistic statistic)
Gets the value of the specifiedstatistic
as aBigInteger
.Use this method to access the
BigInteger
result for exact integer statistics, for exampleStatistic.SUM_OF_SQUARES
.Note: This method may throw an
ArithmeticException
if the result is not finite.- Parameters:
statistic
- Statistic.- Returns:
- the value
- Throws:
java.lang.IllegalArgumentException
- if thestatistic
is not supportedjava.lang.ArithmeticException
- if theresult
is not finite- See Also:
isSupported(Statistic)
,getResult(Statistic)
-
getResult
public StatisticResult getResult(Statistic statistic)
Gets a supplier for the value of the specifiedstatistic
.The returned function will supply the correct result after calls to
accept
orcombine
further values intothis
instance.This method can be used to perform a one-time look-up of the statistic function to compute statistics as values are dynamically added.
- Parameters:
statistic
- Statistic.- Returns:
- the supplier
- Throws:
java.lang.IllegalArgumentException
- if thestatistic
is not supported- See Also:
isSupported(Statistic)
,getAsDouble(Statistic)
-
getGeometricMean
private StatisticResult getGeometricMean()
Gets the geometric mean.- Returns:
- a geometric mean supplier (or null if unsupported)
-
getKurtosis
private StatisticResult getKurtosis()
Gets the kurtosis.- Returns:
- a kurtosis supplier (or null if unsupported)
-
getMean
private StatisticResult getMean()
Gets the mean.- Returns:
- a mean supplier (or null if unsupported)
-
getSkewness
private StatisticResult getSkewness()
Gets the skewness.- Returns:
- a skewness supplier (or null if unsupported)
-
getStandardDeviation
private StatisticResult getStandardDeviation()
Gets the standard deviation.- Returns:
- a standard deviation supplier (or null if unsupported)
-
getVariance
private StatisticResult getVariance()
Gets the variance.- Returns:
- a variance supplier (or null if unsupported)
-
getVarianceOrStd
private StatisticResult getVarianceOrStd(boolean std)
Gets the variance or standard deviation.- Parameters:
std
- Flag to control if the statistic is the standard deviation.- Returns:
- a variance/standard deviation supplier (or null if unsupported)
-
combine
public LongStatistics combine(LongStatistics other)
Combines the state of theother
statistics into this one. Onlythis
instance is modified by thecombine
operation.The
other
instance must be compatible. This istrue
if theother
instance returnstrue
forisSupported(Statistic)
for all values of theStatistic
enum which are supported bythis
instance.Note that this operation is not symmetric. It may be possible to perform
a.combine(b)
but notb.combine(a)
. In the event that theother
instance is not compatible then an exception is raised before any state is modified.- Parameters:
other
- Another set of statistics to be combined.- Returns:
this
instance after combiningother
.- Throws:
java.lang.IllegalArgumentException
- if theother
is not compatible
-
setConfiguration
public LongStatistics setConfiguration(StatisticsConfiguration v)
Sets the statistics configuration.These options only control the final computation of statistics. The configuration will not affect compatibility between instances during a
combine
operation.Note: These options will affect any future computation of statistics. Supplier functions that have been previously created will not be updated with the new configuration.
- Parameters:
v
- Value.- Returns:
this
instance- Throws:
java.lang.NullPointerException
- if the value is null- See Also:
getResult(Statistic)
-
-