LongSum.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. /**
  20.  * Returns the sum of the available values.
  21.  *
  22.  * <ul>
  23.  *   <li>The result is zero if no values are added.
  24.  * </ul>
  25.  *
  26.  * <p>This class uses an exact integer sum. The exact sum is
  27.  * returned using {@link #getAsBigInteger()}. Methods that return {@code int} or
  28.  * {@code long} primitives will raise an exception if the result overflows.
  29.  *
  30.  * <p>Note that the implementation does not use {@code BigInteger} arithmetic; for
  31.  * performance the sum is computed using primitives to create a signed 128-bit integer.
  32.  * Support is provided for at least 2<sup>63</sup> observations.
  33.  *
  34.  * <p>This class is designed to work with (though does not require)
  35.  * {@linkplain java.util.stream streams}.
  36.  *
  37.  * <p><strong>This implementation is not thread safe.</strong>
  38.  * If multiple threads access an instance of this class concurrently,
  39.  * and at least one of the threads invokes the {@link java.util.function.LongConsumer#accept(long) accept} or
  40.  * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
  41.  *
  42.  * <p>However, it is safe to use {@link java.util.function.LongConsumer#accept(long) accept}
  43.  * and {@link StatisticAccumulator#combine(StatisticResult) combine}
  44.  * as {@code accumulator} and {@code combiner} functions of
  45.  * {@link java.util.stream.Collector Collector} on a parallel stream,
  46.  * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  47.  * provides the necessary partitioning, isolation, and merging of results for
  48.  * safe and efficient parallel execution.
  49.  *
  50.  * @since 1.1
  51.  */
  52. public final class LongSum implements LongStatistic, StatisticAccumulator<LongSum> {
  53.     /** Sum of the values. */
  54.     private final Int128 sum;

  55.     /**
  56.      * Create an instance.
  57.      */
  58.     private LongSum() {
  59.         this(Int128.create());
  60.     }

  61.     /**
  62.      * Create an instance.
  63.      *
  64.      * @param sum Sum of the values.
  65.      */
  66.     private LongSum(Int128 sum) {
  67.         this.sum = sum;
  68.     }

  69.     /**
  70.      * Creates an instance.
  71.      *
  72.      * <p>The initial result is zero.
  73.      *
  74.      * @return {@code LongSum} instance.
  75.      */
  76.     public static LongSum create() {
  77.         return new LongSum();
  78.     }

  79.     /**
  80.      * Returns an instance populated using the input {@code values}.
  81.      *
  82.      * <p>When the input is an empty array, the result is zero.
  83.      *
  84.      * @param values Values.
  85.      * @return {@code LongSum} instance.
  86.      */
  87.     public static LongSum of(long... values) {
  88.         final Int128 s = Int128.create();
  89.         for (final long x : values) {
  90.             s.add(x);
  91.         }
  92.         return new LongSum(s);
  93.     }

  94.     /**
  95.      * Gets the sum.
  96.      *
  97.      * <p>This is package private for use in {@link LongStatistics}.
  98.      *
  99.      * @return the sum
  100.      */
  101.     Int128 getSum() {
  102.         return sum;
  103.     }

  104.     /**
  105.      * Updates the state of the statistic to reflect the addition of {@code value}.
  106.      *
  107.      * @param value Value.
  108.      */
  109.     @Override
  110.     public void accept(long value) {
  111.         sum.add(value);
  112.     }

  113.     /**
  114.      * Gets the sum of all input values.
  115.      *
  116.      * <p>When no values have been added, the result is zero.
  117.      *
  118.      * <p>Warning: This will raise an {@link ArithmeticException}
  119.      * if the result is not within the range {@code [-2^31, 2^31)}.
  120.      *
  121.      * @return sum of all values.
  122.      * @throws ArithmeticException if the {@code result} overflows an {@code int}
  123.      * @see #getAsBigInteger()
  124.      */
  125.     @Override
  126.     public int getAsInt() {
  127.         return sum.toIntExact();
  128.     }

  129.     /**
  130.      * Gets the sum of all input values.
  131.      *
  132.      * <p>When no values have been added, the result is zero.
  133.      *
  134.      * <p>Warning: This will raise an {@link ArithmeticException}
  135.      * if the result is not within the range {@code [-2^63, 2^63)}.
  136.      *
  137.      * @return sum of all values.
  138.      * @throws ArithmeticException if the {@code result} overflows a {@code long}
  139.      * @see #getAsBigInteger()
  140.      */
  141.     @Override
  142.     public long getAsLong() {
  143.         return sum.toLongExact();
  144.     }

  145.     /**
  146.      * Gets the sum of all input values.
  147.      *
  148.      * <p>When no values have been added, the result is zero.
  149.      *
  150.      * <p>Note that this conversion can lose information about the precision of the
  151.      * {@code BigInteger} value.
  152.      *
  153.      * @return sum of all values.
  154.      * @see #getAsBigInteger()
  155.      */
  156.     @Override
  157.     public double getAsDouble() {
  158.         return sum.toDouble();
  159.     }

  160.     /**
  161.      * Gets the sum of all input values.
  162.      *
  163.      * <p>When no values have been added, the result is zero.
  164.      *
  165.      * @return sum of all values.
  166.      */
  167.     @Override
  168.     public BigInteger getAsBigInteger() {
  169.         return sum.toBigInteger();
  170.     }

  171.     @Override
  172.     public LongSum combine(LongSum other) {
  173.         sum.add(other.sum);
  174.         return this;
  175.     }
  176. }