Class LongStatistics

  • All Implemented Interfaces:
    LongConsumer

    public final class LongStatistics
    extends Object
    implements LongConsumer
    Statistics for long 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 Detail

      • of

        public static LongStatistics 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 LongStatistics of​(Set<Statistic> statistics,
                                        long... 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 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:
        IllegalArgumentException - if there are no statistics to compute.
      • builder

        public static LongStatistics.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 long[] data using the LongStatistics.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 of long[] data. These may be combined. 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:
        IllegalArgumentException - if there are no statistics to compute.
      • accept

        public void accept​(long value)
        Updates the state of the statistics to reflect the addition of value.
        Specified by:
        accept in interface 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 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:
        getResult(Statistic)
      • combine

        public LongStatistics combine​(LongStatistics other)
        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

        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:
        NullPointerException - if the value is null
        See Also:
        getResult(Statistic)