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     * Returns an instance populated using the specified range of {@code values}.
103     *
104     * <p>When the range is empty, the result is zero.
105     *
106     * @param values Values.
107     * @param from Inclusive start of the range.
108     * @param to Exclusive end of the range.
109     * @return {@code LongSum} instance.
110     * @throws IndexOutOfBoundsException if the sub-range is out of bounds
111     * @since 1.2
112     */
113    public static LongSum ofRange(long[] values, int from, int to) {
114        Statistics.checkFromToIndex(from, to, values.length);
115        return createFromRange(values, from, to);
116    }
117
118    /**
119     * Create an instance using the specified range of {@code values}.
120     *
121     * <p>Warning: No range checks are performed.
122     *
123     * @param values Values.
124     * @param from Inclusive start of the range.
125     * @param to Exclusive end of the range.
126     * @return {@code LongSum} instance.
127     */
128    static LongSum createFromRange(long[] values, int from, int to) {
129        final Int128 s = Int128.create();
130        for (int i = from; i < to; i++) {
131            s.add(values[i]);
132        }
133        return new LongSum(s);
134    }
135
136    /**
137     * Gets the sum.
138     *
139     * <p>This is package private for use in {@link LongStatistics}.
140     *
141     * @return the sum
142     */
143    Int128 getSum() {
144        return sum;
145    }
146
147    /**
148     * Updates the state of the statistic to reflect the addition of {@code value}.
149     *
150     * @param value Value.
151     */
152    @Override
153    public void accept(long value) {
154        sum.add(value);
155    }
156
157    /**
158     * Gets the sum of all input values.
159     *
160     * <p>When no values have been added, the result is zero.
161     *
162     * <p>Warning: This will raise an {@link ArithmeticException}
163     * if the result is not within the range {@code [-2^31, 2^31)}.
164     *
165     * @return sum of all values.
166     * @throws ArithmeticException if the {@code result} overflows an {@code int}
167     * @see #getAsBigInteger()
168     */
169    @Override
170    public int getAsInt() {
171        return sum.toIntExact();
172    }
173
174    /**
175     * Gets the sum of all input values.
176     *
177     * <p>When no values have been added, the result is zero.
178     *
179     * <p>Warning: This will raise an {@link ArithmeticException}
180     * if the result is not within the range {@code [-2^63, 2^63)}.
181     *
182     * @return sum of all values.
183     * @throws ArithmeticException if the {@code result} overflows a {@code long}
184     * @see #getAsBigInteger()
185     */
186    @Override
187    public long getAsLong() {
188        return sum.toLongExact();
189    }
190
191    /**
192     * Gets the sum of all input values.
193     *
194     * <p>When no values have been added, the result is zero.
195     *
196     * <p>Note that this conversion can lose information about the precision of the
197     * {@code BigInteger} value.
198     *
199     * @return sum of all values.
200     * @see #getAsBigInteger()
201     */
202    @Override
203    public double getAsDouble() {
204        return sum.toDouble();
205    }
206
207    /**
208     * Gets the sum of all input values.
209     *
210     * <p>When no values have been added, the result is zero.
211     *
212     * @return sum of all values.
213     */
214    @Override
215    public BigInteger getAsBigInteger() {
216        return sum.toBigInteger();
217    }
218
219    @Override
220    public LongSum combine(LongSum other) {
221        sum.add(other.sum);
222        return this;
223    }
224}