Class IntStatistics

java.lang.Object
org.apache.commons.statistics.descriptive.IntStatistics
All Implemented Interfaces:
IntConsumer

public final class IntStatistics extends Object implements IntConsumer
Statistics for 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
  • Method Details

    • of

      public static IntStatistics of(Statistic... statistics)
      Returns a new instance configured to compute the specified statistics.

      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 no statistics to compute.
    • of

      public static IntStatistics of(Set<Statistic> statistics, int... values)
      Returns a new instance configured to compute the specified statistics populated using the input values.

      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 no statistics to compute.
    • ofRange

      public static IntStatistics ofRange(Set<Statistic> statistics, int[] values, int from, int to)
      Returns a new instance configured to compute the specified statistics populated using the specified range of values.

      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 no statistics to compute.
      IndexOutOfBoundsException - if the sub-range is out of bounds
      Since:
      1.2
    • builder

      public static IntStatistics.Builder builder(Statistic... statistics)
      Returns a new builder configured to create instances to compute the specified statistics.

      Use this method to create an instance populated with an array of int[] data using the IntStatistics.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 IntStatistics to be used in parallel, or on separate arrays of int[] data. These may be combined. 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 Collector for 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 no statistics to compute.
    • accept

      public void accept(int value)
      Updates the state of the statistics to reflect the addition of value.
      Specified by:
      accept in interface IntConsumer
      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 specified statistic is supported.

      Note: This method will not return false if the argument is null.

      Parameters:
      statistic - Statistic.
      Returns:
      true if supported
      Throws:
      NullPointerException - if the statistic is null
      See Also:
    • getAsDouble

      public double getAsDouble(Statistic statistic)
      Gets the value of the specified statistic as a double.
      Parameters:
      statistic - Statistic.
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the statistic is not supported
      See Also:
    • getAsInt

      public int getAsInt(Statistic statistic)
      Gets the value of the specified statistic as an int.

      Use this method to access the int result for exact integer statistics, for example Statistic.MIN.

      Note: This method may throw an ArithmeticException if the result overflows an int.

      Parameters:
      statistic - Statistic.
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the statistic is not supported
      ArithmeticException - if the result overflows an int or is not finite
      See Also:
    • getAsLong

      public long getAsLong(Statistic statistic)
      Gets the value of the specified statistic as a long.

      Use this method to access the long result for exact integer statistics, for example Statistic.SUM for a count less than or equal to 232.

      Note: This method may throw an ArithmeticException if the result overflows an long.

      Parameters:
      statistic - Statistic.
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the statistic is not supported
      ArithmeticException - if the result overflows an long or is not finite
      See Also:
    • getAsBigInteger

      public BigInteger getAsBigInteger(Statistic statistic)
      Gets the value of the specified statistic as a BigInteger.

      Use this method to access the BigInteger result for exact integer statistics, for example Statistic.SUM_OF_SQUARES.

      Note: This method may throw an ArithmeticException if the result is not finite.

      Parameters:
      statistic - Statistic.
      Returns:
      the value
      Throws:
      IllegalArgumentException - if the statistic is not supported
      ArithmeticException - if the result is not finite
      See Also:
    • getResult

      public StatisticResult getResult(Statistic statistic)
      Gets a supplier for the value of the specified statistic.

      The returned function will supply the correct result after calls to accept or combine further values into this 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:
      IllegalArgumentException - if the statistic is not supported
      See Also:
    • combine

      Combines the state of the other statistics into this one. Only this instance is modified by the combine operation.

      The other instance must be compatible. This is true if the other instance returns true for isSupported(Statistic) for all values of the Statistic enum which are supported by this instance.

      Note that this operation is not symmetric. It may be possible to perform a.combine(b) but not b.combine(a). In the event that the other 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 combining other.
      Throws:
      IllegalArgumentException - if the other is 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 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:
      NullPointerException - if the value is null
      See Also: