001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.statistics.descriptive;
018
019import java.math.BigInteger;
020
021/**
022 * Returns the sum of the available values.
023 *
024 * <ul>
025 *   <li>The result is zero if no values are added.
026 * </ul>
027 *
028 * <p>This class uses an exact integer sum. The exact sum is
029 * returned using {@link #getAsBigInteger()}. Methods that return {@code int} or
030 * {@code long} primitives will raise an exception if the result overflows.
031 *
032 * <p>Note that the implementation does not use {@code BigInteger} arithmetic; for
033 * performance the sum is computed using primitives to create a signed 128-bit integer.
034 * Support is provided for at least 2<sup>63</sup> observations.
035 *
036 * <p>This class is designed to work with (though does not require)
037 * {@linkplain java.util.stream streams}.
038 *
039 * <p><strong>This implementation is not thread safe.</strong>
040 * If multiple threads access an instance of this class concurrently,
041 * and at least one of the threads invokes the {@link java.util.function.LongConsumer#accept(long) accept} or
042 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
043 *
044 * <p>However, it is safe to use {@link java.util.function.LongConsumer#accept(long) accept}
045 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
046 * as {@code accumulator} and {@code combiner} functions of
047 * {@link java.util.stream.Collector Collector} on a parallel stream,
048 * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
049 * provides the necessary partitioning, isolation, and merging of results for
050 * safe and efficient parallel execution.
051 *
052 * @since 1.1
053 */
054public final class LongSum implements LongStatistic, StatisticAccumulator<LongSum> {
055    /** Sum of the values. */
056    private final Int128 sum;
057
058    /**
059     * Create an instance.
060     */
061    private LongSum() {
062        this(Int128.create());
063    }
064
065    /**
066     * Create an instance.
067     *
068     * @param sum Sum of the values.
069     */
070    private LongSum(Int128 sum) {
071        this.sum = sum;
072    }
073
074    /**
075     * Creates an instance.
076     *
077     * <p>The initial result is zero.
078     *
079     * @return {@code LongSum} instance.
080     */
081    public static LongSum create() {
082        return new LongSum();
083    }
084
085    /**
086     * Returns an instance populated using the input {@code values}.
087     *
088     * <p>When the input is an empty array, the result is zero.
089     *
090     * @param values Values.
091     * @return {@code LongSum} instance.
092     */
093    public static LongSum of(long... values) {
094        final Int128 s = Int128.create();
095        for (final long x : values) {
096            s.add(x);
097        }
098        return new LongSum(s);
099    }
100
101    /**
102     * Gets the sum.
103     *
104     * <p>This is package private for use in {@link LongStatistics}.
105     *
106     * @return the sum
107     */
108    Int128 getSum() {
109        return sum;
110    }
111
112    /**
113     * Updates the state of the statistic to reflect the addition of {@code value}.
114     *
115     * @param value Value.
116     */
117    @Override
118    public void accept(long value) {
119        sum.add(value);
120    }
121
122    /**
123     * Gets the sum of all input values.
124     *
125     * <p>When no values have been added, the result is zero.
126     *
127     * <p>Warning: This will raise an {@link ArithmeticException}
128     * if the result is not within the range {@code [-2^31, 2^31)}.
129     *
130     * @return sum of all values.
131     * @throws ArithmeticException if the {@code result} overflows an {@code int}
132     * @see #getAsBigInteger()
133     */
134    @Override
135    public int getAsInt() {
136        return sum.toIntExact();
137    }
138
139    /**
140     * Gets the sum of all input values.
141     *
142     * <p>When no values have been added, the result is zero.
143     *
144     * <p>Warning: This will raise an {@link ArithmeticException}
145     * if the result is not within the range {@code [-2^63, 2^63)}.
146     *
147     * @return sum of all values.
148     * @throws ArithmeticException if the {@code result} overflows a {@code long}
149     * @see #getAsBigInteger()
150     */
151    @Override
152    public long getAsLong() {
153        return sum.toLongExact();
154    }
155
156    /**
157     * Gets the sum of all input values.
158     *
159     * <p>When no values have been added, the result is zero.
160     *
161     * <p>Note that this conversion can lose information about the precision of the
162     * {@code BigInteger} value.
163     *
164     * @return sum of all values.
165     * @see #getAsBigInteger()
166     */
167    @Override
168    public double getAsDouble() {
169        return sum.toDouble();
170    }
171
172    /**
173     * Gets the sum of all input values.
174     *
175     * <p>When no values have been added, the result is zero.
176     *
177     * @return sum of all values.
178     */
179    @Override
180    public BigInteger getAsBigInteger() {
181        return sum.toBigInteger();
182    }
183
184    @Override
185    public LongSum combine(LongSum other) {
186        sum.add(other.sum);
187        return this;
188    }
189}