Class IntStatistics
- All Implemented Interfaces:
IntConsumer
int 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 -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(int value) Updates the state of the statistics to reflect the addition ofvalue.static IntStatistics.BuilderReturns a new builder configured to create instances to compute the specifiedstatistics.combine(IntStatistics other) Combines the state of theotherstatistics into this one.getAsBigInteger(Statistic statistic) Gets the value of the specifiedstatisticas aBigInteger.doublegetAsDouble(Statistic statistic) Gets the value of the specifiedstatisticas adouble.intGets the value of the specifiedstatisticas anint.longGets the value of the specifiedstatisticas along.longgetCount()Return the count of values recorded.Gets a supplier for the value of the specifiedstatistic.booleanisSupported(Statistic statistic) Check if the specifiedstatisticis supported.static IntStatisticsReturns a new instance configured to compute the specifiedstatisticspopulated using the inputvalues.static IntStatisticsReturns a new instance configured to compute the specifiedstatistics.static IntStatisticsReturns a new instance configured to compute the specifiedstatisticspopulated using the specified range ofvalues.Sets the statistics configuration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.function.IntConsumer
andThen
-
Method Details
-
of
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:
IllegalArgumentException- if there are nostatisticsto compute.
-
of
Returns a new instance configured to compute the specifiedstatisticspopulated using the inputvalues.Use this method to create an instance populated with a (variable) array of
int[]data:IntStatistics stats = IntStatistics.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:
IllegalArgumentException- if there are nostatisticsto compute.
-
ofRange
Returns a new instance configured to compute the specifiedstatisticspopulated using the specified range ofvalues.Use this method to create an instance populated with part of an array of
int[]data, e.g. to use the first half of the data:int[] data = ... IntStatistics stats = IntStatistics.of( EnumSet.of(Statistic.MIN, Statistic.MAX), data, 0, data.length / 2);- Parameters:
statistics- Statistics to compute.values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
- the instance
- Throws:
IllegalArgumentException- if there are nostatisticsto compute.IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
-
builder
Returns a new builder configured to create instances to compute the specifiedstatistics.Use this method to create an instance populated with an array of
int[]data using theIntStatistics.Builder.build(int...)method:int[] data = ... IntStatistics stats = IntStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE) .build(data);The builder can be used to create multiple instances of
IntStatisticsto be used in parallel, or on separate arrays ofint[]data. These may becombined. For example:int[][] data = ... IntStatistics.Builder builder = IntStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); IntStatistics stats = Arrays.stream(data) .parallel() .map(builder::build) .reduce(IntStatistics::combine) .get();The builder can be used to create a
Collectorfor repeat use on multiple data:IntStatistics.Builder builder = IntStatistics.builder( Statistic.MIN, Statistic.MAX, Statistic.VARIANCE); Collector<int[], IntStatistics, IntStatistics> collector = Collector.of(builder::build, (s, d) -> s.combine(builder.build(d)), IntStatistics::combine); // Repeated int[][] data = ... IntStatistics stats = Arrays.stream(data).collect(collector);- Parameters:
statistics- Statistics to compute.- Returns:
- the builder
- Throws:
IllegalArgumentException- if there are nostatisticsto compute.
-
accept
Updates the state of the statistics to reflect the addition ofvalue.- Specified by:
acceptin interfaceIntConsumer- Parameters:
value- Value.
-
getCount
Return the count of values recorded.- Returns:
- the count of values
-
isSupported
Check if the specifiedstatisticis supported.Note: This method will not return
falseif the argument isnull.- Parameters:
statistic- Statistic.- Returns:
trueif supported- Throws:
NullPointerException- if thestatisticisnull- See Also:
-
getAsDouble
Gets the value of the specifiedstatisticas adouble.- Parameters:
statistic- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException- if thestatisticis not supported- See Also:
-
getAsInt
Gets the value of the specifiedstatisticas anint.Use this method to access the
intresult for exact integer statistics, for exampleStatistic.MIN.Note: This method may throw an
ArithmeticExceptionif the result overflows anint.- Parameters:
statistic- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException- if thestatisticis not supportedArithmeticException- if theresultoverflows anintor is not finite- See Also:
-
getAsLong
Gets the value of the specifiedstatisticas along.Use this method to access the
longresult for exact integer statistics, for exampleStatistic.SUMfor acountless than or equal to 232.Note: This method may throw an
ArithmeticExceptionif the result overflows anlong.- Parameters:
statistic- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException- if thestatisticis not supportedArithmeticException- if theresultoverflows anlongor is not finite- See Also:
-
getAsBigInteger
Gets the value of the specifiedstatisticas aBigInteger.Use this method to access the
BigIntegerresult for exact integer statistics, for exampleStatistic.SUM_OF_SQUARES.Note: This method may throw an
ArithmeticExceptionif the result is not finite.- Parameters:
statistic- Statistic.- Returns:
- the value
- Throws:
IllegalArgumentException- if thestatisticis not supportedArithmeticException- if theresultis not finite- See Also:
-
getResult
Gets a supplier for the value of the specifiedstatistic.The returned function will supply the correct result after calls to
acceptorcombinefurther values intothisinstance.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:
IllegalArgumentException- if thestatisticis not supported- See Also:
-
combine
Combines the state of theotherstatistics into this one. Onlythisinstance is modified by thecombineoperation.The
otherinstance must be compatible. This istrueif theotherinstance returnstrueforisSupported(Statistic)for all values of theStatisticenum which are supported bythisinstance.Note that this operation is not symmetric. It may be possible to perform
a.combine(b)but notb.combine(a). In the event that theotherinstance is not compatible then an exception is raised before any state is modified.- Parameters:
other- Another set of statistics to be combined.- Returns:
thisinstance after combiningother.- Throws:
IllegalArgumentException- if theotheris not compatible
-
setConfiguration
Sets the statistics configuration.These options only control the final computation of statistics. The configuration will not affect compatibility between instances during a
combineoperation.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:
thisinstance- Throws:
NullPointerException- if the value is null- See Also:
-