LongStatistics.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.statistics.descriptive;

  18. import java.math.BigInteger;
  19. import java.util.Objects;
  20. import java.util.Set;
  21. import java.util.function.DoubleConsumer;
  22. import java.util.function.Function;
  23. import java.util.function.LongConsumer;

  24. /**
  25.  * Statistics for {@code long} values.
  26.  *
  27.  * <p>This class provides combinations of individual statistic implementations in the
  28.  * {@code org.apache.commons.statistics.descriptive} package.
  29.  *
  30.  * <p>Supports up to 2<sup>63</sup> (exclusive) observations.
  31.  * This implementation does not check for overflow of the count.
  32.  *
  33.  * @since 1.1
  34.  */
  35. public final class LongStatistics implements LongConsumer {
  36.     /** Error message for non configured statistics. */
  37.     private static final String NO_CONFIGURED_STATISTICS = "No configured statistics";
  38.     /** Error message for an unsupported statistic. */
  39.     private static final String UNSUPPORTED_STATISTIC = "Unsupported statistic: ";

  40.     /** Count of values recorded. */
  41.     private long count;
  42.     /** The consumer of values. */
  43.     private final LongConsumer consumer;
  44.     /** The {@link LongMin} implementation. */
  45.     private final LongMin min;
  46.     /** The {@link LongMax} implementation. */
  47.     private final LongMax max;
  48.     /** The moment implementation. May be any instance of {@link FirstMoment}.
  49.      * This implementation uses only the third and fourth moments. */
  50.     private final FirstMoment moment;
  51.     /** The {@link LongSum} implementation. */
  52.     private final LongSum sum;
  53.     /** The {@link Product} implementation. */
  54.     private final Product product;
  55.     /** The {@link LongSumOfSquares} implementation. */
  56.     private final LongSumOfSquares sumOfSquares;
  57.     /** The {@link SumOfLogs} implementation. */
  58.     private final SumOfLogs sumOfLogs;
  59.     /** Configuration options for computation of statistics. */
  60.     private StatisticsConfiguration config;

  61.     /**
  62.      * A builder for {@link LongStatistics}.
  63.      */
  64.     public static final class Builder {
  65.         /** An empty double array. */
  66.         private static final long[] NO_VALUES = {};

  67.         /** The {@link LongMin} constructor. */
  68.         private Function<long[], LongMin> min;
  69.         /** The {@link LongMax} constructor. */
  70.         private Function<long[], LongMax> max;
  71.         /** The moment constructor. May return any instance of {@link FirstMoment}. */
  72.         private Function<long[], FirstMoment> moment;
  73.         /** The {@link LongSum} constructor. */
  74.         private Function<long[], LongSum> sum;
  75.         /** The {@link Product} constructor. */
  76.         private Function<long[], Product> product;
  77.         /** The {@link LongSumOfSquares} constructor. */
  78.         private Function<long[], LongSumOfSquares> sumOfSquares;
  79.         /** The {@link SumOfLogs} constructor. */
  80.         private Function<long[], SumOfLogs> sumOfLogs;
  81.         /** The order of the moment. It corresponds to the power computed by the {@link FirstMoment}
  82.          * instance constructed by {@link #moment}. This should only be increased from the default
  83.          * of zero (corresponding to no moment computation). */
  84.         private int momentOrder;
  85.         /** Configuration options for computation of statistics. */
  86.         private StatisticsConfiguration config = StatisticsConfiguration.withDefaults();

  87.         /**
  88.          * Create an instance.
  89.          */
  90.         Builder() {
  91.             // Do nothing
  92.         }

  93.         /**
  94.          * Add the statistic to the statistics to compute.
  95.          *
  96.          * @param statistic Statistic to compute.
  97.          * @return {@code this} instance
  98.          */
  99.         Builder add(Statistic statistic) {
  100.             switch (statistic) {
  101.             case GEOMETRIC_MEAN:
  102.             case SUM_OF_LOGS:
  103.                 sumOfLogs = SumOfLogs::of;
  104.                 break;
  105.             case KURTOSIS:
  106.                 createMoment(4);
  107.                 break;
  108.             case MAX:
  109.                 max = LongMax::of;
  110.                 break;
  111.             case MIN:
  112.                 min = LongMin::of;
  113.                 break;
  114.             case PRODUCT:
  115.                 product = Product::of;
  116.                 break;
  117.             case SKEWNESS:
  118.                 createMoment(3);
  119.                 break;
  120.             case STANDARD_DEVIATION:
  121.             case VARIANCE:
  122.                 sum = LongSum::of;
  123.                 sumOfSquares = LongSumOfSquares::of;
  124.                 break;
  125.             case MEAN:
  126.             case SUM:
  127.                 sum = LongSum::of;
  128.                 break;
  129.             case SUM_OF_SQUARES:
  130.                 sumOfSquares = LongSumOfSquares::of;
  131.                 break;
  132.             default:
  133.                 throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic);
  134.             }
  135.             return this;
  136.         }

  137.         /**
  138.          * Creates the moment constructor for the specified {@code order},
  139.          * e.g. order=3 is sum of cubed deviations.
  140.          *
  141.          * @param order Order.
  142.          */
  143.         private void createMoment(int order) {
  144.             if (order > momentOrder) {
  145.                 momentOrder = order;
  146.                 if (order == 4) {
  147.                     moment = SumOfFourthDeviations::of;
  148.                 } else {
  149.                     // Assume order == 3
  150.                     moment = SumOfCubedDeviations::of;
  151.                 }
  152.             }
  153.         }

  154.         /**
  155.          * Sets the statistics configuration options for computation of statistics.
  156.          *
  157.          * @param v Value.
  158.          * @return the builder
  159.          * @throws NullPointerException if the value is null
  160.          */
  161.         public Builder setConfiguration(StatisticsConfiguration v) {
  162.             config = Objects.requireNonNull(v);
  163.             return this;
  164.         }

  165.         /**
  166.          * Builds a {@code LongStatistics} instance.
  167.          *
  168.          * @return {@code LongStatistics} instance.
  169.          */
  170.         public LongStatistics build() {
  171.             return build(NO_VALUES);
  172.         }

  173.         /**
  174.          * Builds a {@code LongStatistics} instance using the input {@code values}.
  175.          *
  176.          * <p>Note: {@code LongStatistics} computed using
  177.          * {@link LongStatistics#accept(long) accept} may be
  178.          * different from this instance.
  179.          *
  180.          * @param values Values.
  181.          * @return {@code LongStatistics} instance.
  182.          */
  183.         public LongStatistics build(long... values) {
  184.             Objects.requireNonNull(values, "values");
  185.             return new LongStatistics(
  186.                 values.length,
  187.                 create(min, values),
  188.                 create(max, values),
  189.                 create(moment, values),
  190.                 create(sum, values),
  191.                 create(product, values),
  192.                 create(sumOfSquares, values),
  193.                 create(sumOfLogs, values),
  194.                 config);
  195.         }

  196.         /**
  197.          * Creates the object from the {@code values}.
  198.          *
  199.          * @param <T> object type
  200.          * @param constructor Constructor.
  201.          * @param values Values
  202.          * @return the instance
  203.          */
  204.         private static <T> T create(Function<long[], T> constructor, long[] values) {
  205.             if (constructor != null) {
  206.                 return constructor.apply(values);
  207.             }
  208.             return null;
  209.         }
  210.     }

  211.     /**
  212.      * Create an instance.
  213.      *
  214.      * @param count Count of values.
  215.      * @param min LongMin implementation.
  216.      * @param max LongMax implementation.
  217.      * @param moment Moment implementation.
  218.      * @param sum LongSum implementation.
  219.      * @param product Product implementation.
  220.      * @param sumOfSquares Sum of squares implementation.
  221.      * @param sumOfLogs Sum of logs implementation.
  222.      * @param config Statistics configuration.
  223.      */
  224.     LongStatistics(long count, LongMin min, LongMax max, FirstMoment moment, LongSum sum,
  225.                   Product product, LongSumOfSquares sumOfSquares, SumOfLogs sumOfLogs,
  226.                   StatisticsConfiguration config) {
  227.         this.count = count;
  228.         this.min = min;
  229.         this.max = max;
  230.         this.moment = moment;
  231.         this.sum = sum;
  232.         this.product = product;
  233.         this.sumOfSquares = sumOfSquares;
  234.         this.sumOfLogs = sumOfLogs;
  235.         this.config = config;
  236.         // The final consumer should never be null as the builder is created
  237.         // with at least one statistic.
  238.         consumer = Statistics.compose(min, max, sum, sumOfSquares,
  239.                                       composeAsLong(moment, product, sumOfLogs));
  240.     }

  241.     /**
  242.      * Chain the {@code consumers} into a single composite {@code LongConsumer}.
  243.      * Ignore any {@code null} consumer.
  244.      *
  245.      * @param consumers Consumers.
  246.      * @return a composed consumer (or null)
  247.      */
  248.     private static LongConsumer composeAsLong(DoubleConsumer... consumers) {
  249.         final DoubleConsumer c = Statistics.compose(consumers);
  250.         if (c != null) {
  251.             return c::accept;
  252.         }
  253.         return null;
  254.     }

  255.     /**
  256.      * Returns a new instance configured to compute the specified {@code statistics}.
  257.      *
  258.      * <p>The statistics will be empty and so will return the default values for each
  259.      * computed statistic.
  260.      *
  261.      * @param statistics Statistics to compute.
  262.      * @return the instance
  263.      * @throws IllegalArgumentException if there are no {@code statistics} to compute.
  264.      */
  265.     public static LongStatistics of(Statistic... statistics) {
  266.         return builder(statistics).build();
  267.     }

  268.     /**
  269.      * Returns a new instance configured to compute the specified {@code statistics}
  270.      * populated using the input {@code values}.
  271.      *
  272.      * <p>Use this method to create an instance populated with a (variable) array of
  273.      * {@code long[]} data:
  274.      *
  275.      * <pre>
  276.      * LongStatistics stats = LongStatistics.of(
  277.      *     EnumSet.of(Statistic.MIN, Statistic.MAX),
  278.      *     1, 1, 2, 3, 5, 8, 13);
  279.      * </pre>
  280.      *
  281.      * @param statistics Statistics to compute.
  282.      * @param values Values.
  283.      * @return the instance
  284.      * @throws IllegalArgumentException if there are no {@code statistics} to compute.
  285.      */
  286.     public static LongStatistics of(Set<Statistic> statistics, long... values) {
  287.         if (statistics.isEmpty()) {
  288.             throw new IllegalArgumentException(NO_CONFIGURED_STATISTICS);
  289.         }
  290.         final Builder b = new Builder();
  291.         statistics.forEach(b::add);
  292.         return b.build(values);
  293.     }

  294.     /**
  295.      * Returns a new builder configured to create instances to compute the specified
  296.      * {@code statistics}.
  297.      *
  298.      * <p>Use this method to create an instance populated with an array of {@code long[]}
  299.      * data using the {@link Builder#build(long...)} method:
  300.      *
  301.      * <pre>
  302.      * long[] data = ...
  303.      * LongStatistics stats = LongStatistics.builder(
  304.      *     Statistic.MIN, Statistic.MAX, Statistic.VARIANCE)
  305.      *     .build(data);
  306.      * </pre>
  307.      *
  308.      * <p>The builder can be used to create multiple instances of {@link LongStatistics}
  309.      * to be used in parallel, or on separate arrays of {@code long[]} data. These may
  310.      * be {@link #combine(LongStatistics) combined}. For example:
  311.      *
  312.      * <pre>
  313.      * long[][] data = ...
  314.      * LongStatistics.Builder builder = LongStatistics.builder(
  315.      *     Statistic.MIN, Statistic.MAX, Statistic.VARIANCE);
  316.      * LongStatistics stats = Arrays.stream(data)
  317.      *     .parallel()
  318.      *     .map(builder::build)
  319.      *     .reduce(LongStatistics::combine)
  320.      *     .get();
  321.      * </pre>
  322.      *
  323.      * <p>The builder can be used to create a {@link java.util.stream.Collector} for repeat
  324.      * use on multiple data:
  325.      *
  326.      * <pre>{@code
  327.      * LongStatistics.Builder builder = LongStatistics.builder(
  328.      *     Statistic.MIN, Statistic.MAX, Statistic.VARIANCE);
  329.      * Collector<long[], LongStatistics, LongStatistics> collector =
  330.      *     Collector.of(builder::build,
  331.      *                  (s, d) -> s.combine(builder.build(d)),
  332.      *                  LongStatistics::combine);
  333.      *
  334.      * // Repeated
  335.      * long[][] data = ...
  336.      * LongStatistics stats = Arrays.stream(data).collect(collector);
  337.      * }</pre>
  338.      *
  339.      * @param statistics Statistics to compute.
  340.      * @return the builder
  341.      * @throws IllegalArgumentException if there are no {@code statistics} to compute.
  342.      */
  343.     public static Builder builder(Statistic... statistics) {
  344.         if (statistics.length == 0) {
  345.             throw new IllegalArgumentException(NO_CONFIGURED_STATISTICS);
  346.         }
  347.         final Builder b = new Builder();
  348.         for (final Statistic s : statistics) {
  349.             b.add(s);
  350.         }
  351.         return b;
  352.     }

  353.     /**
  354.      * Updates the state of the statistics to reflect the addition of {@code value}.
  355.      *
  356.      * @param value Value.
  357.      */
  358.     @Override
  359.     public void accept(long value) {
  360.         count++;
  361.         consumer.accept(value);
  362.     }

  363.     /**
  364.      * Return the count of values recorded.
  365.      *
  366.      * @return the count of values
  367.      */
  368.     public long getCount() {
  369.         return count;
  370.     }

  371.     /**
  372.      * Check if the specified {@code statistic} is supported.
  373.      *
  374.      * <p>Note: This method will not return {@code false} if the argument is {@code null}.
  375.      *
  376.      * @param statistic Statistic.
  377.      * @return {@code true} if supported
  378.      * @throws NullPointerException if the {@code statistic} is {@code null}
  379.      * @see #getResult(Statistic)
  380.      */
  381.     public boolean isSupported(Statistic statistic) {
  382.         // Check for the appropriate underlying implementation
  383.         switch (statistic) {
  384.         case GEOMETRIC_MEAN:
  385.         case SUM_OF_LOGS:
  386.             return sumOfLogs != null;
  387.         case KURTOSIS:
  388.             return moment instanceof SumOfFourthDeviations;
  389.         case MAX:
  390.             return max != null;
  391.         case MIN:
  392.             return min != null;
  393.         case PRODUCT:
  394.             return product != null;
  395.         case SKEWNESS:
  396.             return moment instanceof SumOfCubedDeviations;
  397.         case STANDARD_DEVIATION:
  398.         case VARIANCE:
  399.             return sum != null && sumOfSquares != null;
  400.         case MEAN:
  401.         case SUM:
  402.             return sum != null;
  403.         case SUM_OF_SQUARES:
  404.             return sumOfSquares != null;
  405.         default:
  406.             return false;
  407.         }
  408.     }

  409.     /**
  410.      * Gets the value of the specified {@code statistic} as a {@code double}.
  411.      *
  412.      * @param statistic Statistic.
  413.      * @return the value
  414.      * @throws IllegalArgumentException if the {@code statistic} is not supported
  415.      * @see #isSupported(Statistic)
  416.      * @see #getResult(Statistic)
  417.      */
  418.     public double getAsDouble(Statistic statistic) {
  419.         return getResult(statistic).getAsDouble();
  420.     }

  421.     /**
  422.      * Gets the value of the specified {@code statistic} as a {@code long}.
  423.      *
  424.      * <p>Use this method to access the {@code long} result for exact integer statistics,
  425.      * for example {@link Statistic#MIN}.
  426.      *
  427.      * <p>Note: This method may throw an {@link ArithmeticException} if the result
  428.      * overflows an {@code long}.
  429.      *
  430.      * @param statistic Statistic.
  431.      * @return the value
  432.      * @throws IllegalArgumentException if the {@code statistic} is not supported
  433.      * @throws ArithmeticException if the {@code result} overflows an {@code long} or is not
  434.      * finite
  435.      * @see #isSupported(Statistic)
  436.      * @see #getResult(Statistic)
  437.      */
  438.     public long getAsLong(Statistic statistic) {
  439.         return getResult(statistic).getAsLong();
  440.     }

  441.     /**
  442.      * Gets the value of the specified {@code statistic} as a {@code BigInteger}.
  443.      *
  444.      * <p>Use this method to access the {@code BigInteger} result for exact integer statistics,
  445.      * for example {@link Statistic#SUM_OF_SQUARES}.
  446.      *
  447.      * <p>Note: This method may throw an {@link ArithmeticException} if the result
  448.      * is not finite.
  449.      *
  450.      * @param statistic Statistic.
  451.      * @return the value
  452.      * @throws IllegalArgumentException if the {@code statistic} is not supported
  453.      * @throws ArithmeticException if the {@code result} is not finite
  454.      * @see #isSupported(Statistic)
  455.      * @see #getResult(Statistic)
  456.      */
  457.     public BigInteger getAsBigInteger(Statistic statistic) {
  458.         return getResult(statistic).getAsBigInteger();
  459.     }

  460.     /**
  461.      * Gets a supplier for the value of the specified {@code statistic}.
  462.      *
  463.      * <p>The returned function will supply the correct result after
  464.      * calls to {@link #accept(long) accept} or
  465.      * {@link #combine(LongStatistics) combine} further values into
  466.      * {@code this} instance.
  467.      *
  468.      * <p>This method can be used to perform a one-time look-up of the statistic
  469.      * function to compute statistics as values are dynamically added.
  470.      *
  471.      * @param statistic Statistic.
  472.      * @return the supplier
  473.      * @throws IllegalArgumentException if the {@code statistic} is not supported
  474.      * @see #isSupported(Statistic)
  475.      * @see #getAsDouble(Statistic)
  476.      */
  477.     public StatisticResult getResult(Statistic statistic) {
  478.         // Locate the implementation.
  479.         // Statistics that wrap an underlying implementation are created in methods.
  480.         // The return argument should be an interface reference and not an instance
  481.         // of LongStatistic. This ensures the statistic implementation cannot
  482.         // be updated with new values by casting the result and calling accept(long).
  483.         StatisticResult stat = null;
  484.         switch (statistic) {
  485.         case GEOMETRIC_MEAN:
  486.             stat = getGeometricMean();
  487.             break;
  488.         case KURTOSIS:
  489.             stat = getKurtosis();
  490.             break;
  491.         case MAX:
  492.             stat = Statistics.getResultAsLongOrNull(max);
  493.             break;
  494.         case MEAN:
  495.             stat = getMean();
  496.             break;
  497.         case MIN:
  498.             stat = Statistics.getResultAsLongOrNull(min);
  499.             break;
  500.         case PRODUCT:
  501.             stat = Statistics.getResultAsDoubleOrNull(product);
  502.             break;
  503.         case SKEWNESS:
  504.             stat = getSkewness();
  505.             break;
  506.         case STANDARD_DEVIATION:
  507.             stat = getStandardDeviation();
  508.             break;
  509.         case SUM:
  510.             stat = Statistics.getResultAsBigIntegerOrNull(sum);
  511.             break;
  512.         case SUM_OF_LOGS:
  513.             stat = Statistics.getResultAsDoubleOrNull(sumOfLogs);
  514.             break;
  515.         case SUM_OF_SQUARES:
  516.             stat = Statistics.getResultAsBigIntegerOrNull(sumOfSquares);
  517.             break;
  518.         case VARIANCE:
  519.             stat = getVariance();
  520.             break;
  521.         default:
  522.             break;
  523.         }
  524.         if (stat != null) {
  525.             return stat;
  526.         }
  527.         throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic);
  528.     }

  529.     /**
  530.      * Gets the geometric mean.
  531.      *
  532.      * @return a geometric mean supplier (or null if unsupported)
  533.      */
  534.     private StatisticResult getGeometricMean() {
  535.         if (sumOfLogs != null) {
  536.             // Return a function that has access to the count and sumOfLogs
  537.             return () -> GeometricMean.computeGeometricMean(count, sumOfLogs);
  538.         }
  539.         return null;
  540.     }

  541.     /**
  542.      * Gets the kurtosis.
  543.      *
  544.      * @return a kurtosis supplier (or null if unsupported)
  545.      */
  546.     private StatisticResult getKurtosis() {
  547.         if (moment instanceof SumOfFourthDeviations) {
  548.             return new Kurtosis((SumOfFourthDeviations) moment)
  549.                 .setBiased(config.isBiased())::getAsDouble;
  550.         }
  551.         return null;
  552.     }

  553.     /**
  554.      * Gets the mean.
  555.      *
  556.      * @return a mean supplier (or null if unsupported)
  557.      */
  558.     private StatisticResult getMean() {
  559.         if (sum != null) {
  560.             // Return a function that has access to the count and sum
  561.             final Int128 s = sum.getSum();
  562.             return () -> LongMean.computeMean(s, count);
  563.         }
  564.         return null;
  565.     }

  566.     /**
  567.      * Gets the skewness.
  568.      *
  569.      * @return a skewness supplier (or null if unsupported)
  570.      */
  571.     private StatisticResult getSkewness() {
  572.         if (moment instanceof SumOfCubedDeviations) {
  573.             return new Skewness((SumOfCubedDeviations) moment)
  574.                 .setBiased(config.isBiased())::getAsDouble;
  575.         }
  576.         return null;
  577.     }

  578.     /**
  579.      * Gets the standard deviation.
  580.      *
  581.      * @return a standard deviation supplier (or null if unsupported)
  582.      */
  583.     private StatisticResult getStandardDeviation() {
  584.         return getVarianceOrStd(true);
  585.     }

  586.     /**
  587.      * Gets the variance.
  588.      *
  589.      * @return a variance supplier (or null if unsupported)
  590.      */
  591.     private StatisticResult getVariance() {
  592.         return getVarianceOrStd(false);
  593.     }

  594.     /**
  595.      * Gets the variance or standard deviation.
  596.      *
  597.      * @param std Flag to control if the statistic is the standard deviation.
  598.      * @return a variance/standard deviation supplier (or null if unsupported)
  599.      */
  600.     private StatisticResult getVarianceOrStd(boolean std) {
  601.         if (sum != null && sumOfSquares != null) {
  602.             // Return a function that has access to the count, sum and sum of squares
  603.             final Int128 s = sum.getSum();
  604.             final UInt192 ss = sumOfSquares.getSumOfSquares();
  605.             final boolean biased = config.isBiased();
  606.             return () -> LongVariance.computeVarianceOrStd(ss, s, count, biased, std);
  607.         }
  608.         return null;
  609.     }

  610.     /**
  611.      * Combines the state of the {@code other} statistics into this one.
  612.      * Only {@code this} instance is modified by the {@code combine} operation.
  613.      *
  614.      * <p>The {@code other} instance must be <em>compatible</em>. This is {@code true} if the
  615.      * {@code other} instance returns {@code true} for {@link #isSupported(Statistic)} for
  616.      * all values of the {@link Statistic} enum which are supported by {@code this}
  617.      * instance.
  618.      *
  619.      * <p>Note that this operation is <em>not symmetric</em>. It may be possible to perform
  620.      * {@code a.combine(b)} but not {@code b.combine(a)}. In the event that the {@code other}
  621.      * instance is not compatible then an exception is raised before any state is modified.
  622.      *
  623.      * @param other Another set of statistics to be combined.
  624.      * @return {@code this} instance after combining {@code other}.
  625.      * @throws IllegalArgumentException if the {@code other} is not compatible
  626.      */
  627.     public LongStatistics combine(LongStatistics other) {
  628.         // Check compatibility
  629.         Statistics.checkCombineCompatible(min, other.min);
  630.         Statistics.checkCombineCompatible(max, other.max);
  631.         Statistics.checkCombineCompatible(sum, other.sum);
  632.         Statistics.checkCombineCompatible(product, other.product);
  633.         Statistics.checkCombineCompatible(sumOfSquares, other.sumOfSquares);
  634.         Statistics.checkCombineCompatible(sumOfLogs, other.sumOfLogs);
  635.         Statistics.checkCombineAssignable(moment, other.moment);
  636.         // Combine
  637.         count += other.count;
  638.         Statistics.combine(min, other.min);
  639.         Statistics.combine(max, other.max);
  640.         Statistics.combine(sum, other.sum);
  641.         Statistics.combine(product, other.product);
  642.         Statistics.combine(sumOfSquares, other.sumOfSquares);
  643.         Statistics.combine(sumOfLogs, other.sumOfLogs);
  644.         Statistics.combineMoment(moment, other.moment);
  645.         return this;
  646.     }

  647.     /**
  648.      * Sets the statistics configuration.
  649.      *
  650.      * <p>These options only control the final computation of statistics. The configuration
  651.      * will not affect compatibility between instances during a
  652.      * {@link #combine(LongStatistics) combine} operation.
  653.      *
  654.      * <p>Note: These options will affect any future computation of statistics. Supplier functions
  655.      * that have been previously created will not be updated with the new configuration.
  656.      *
  657.      * @param v Value.
  658.      * @return {@code this} instance
  659.      * @throws NullPointerException if the value is null
  660.      * @see #getResult(Statistic)
  661.      */
  662.     public LongStatistics setConfiguration(StatisticsConfiguration v) {
  663.         config = Objects.requireNonNull(v);
  664.         return this;
  665.     }
  666. }