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
019/**
020 * Returns the sum of the available values.
021 *
022 * <ul>
023 *   <li>The result is zero if no values are added.
024 *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
025 *   <li>The result is {@code NaN} if the values contain positive and negative infinity.
026 *   <li>The result is non-finite if the values contain infinities of the same sign.
027 * </ul>
028 *
029 * <p>Note: In the event of infinite values of the same sign the result will be non-finite.
030 * In this case the returned results may differ (either the same infinity or {@code NaN}) based on
031 * input order if the sum of the finite-only values can overflow to an opposite signed infinity.
032 *
033 * <p>This class is designed to work with (though does not require)
034 * {@linkplain java.util.stream streams}.
035 *
036 * <p><strong>This instance is not thread safe.</strong>
037 * If multiple threads access an instance of this class concurrently,
038 * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
039 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
040 *
041 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
042 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
043 * as {@code accumulator} and {@code combiner} functions of
044 * {@link java.util.stream.Collector Collector} on a parallel stream,
045 * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
046 * provides the necessary partitioning, isolation, and merging of results for
047 * safe and efficient parallel execution.
048 *
049 * @since 1.1
050 * @see org.apache.commons.numbers.core.Sum
051 */
052public final class Sum implements DoubleStatistic, StatisticAccumulator<Sum> {
053
054    /** {@link org.apache.commons.numbers.core.Sum Sum} used to compute the sum. */
055    private final org.apache.commons.numbers.core.Sum delegate;
056
057    /**
058     * Create an instance.
059     */
060    private Sum() {
061        this(org.apache.commons.numbers.core.Sum.create());
062    }
063
064    /**
065     * Create an instance using the specified {@code sum}.
066     *
067     * @param sum Sum.
068     */
069    Sum(org.apache.commons.numbers.core.Sum sum) {
070        delegate = sum;
071    }
072
073    /**
074     * Creates an instance.
075     *
076     * <p>The initial result is zero.
077     *
078     * @return {@code Sum} instance.
079     */
080    public static Sum create() {
081        return new Sum();
082    }
083
084    /**
085     * Returns an instance populated using the input {@code values}.
086     *
087     * <p>The result is {@code NaN} if any of the values is {@code NaN}
088     * or the sum at any point is a {@code NaN}.
089     *
090     * <p>When the input is an empty array, the result is zero.
091     *
092     * @param values Values.
093     * @return {@code Sum} instance.
094     */
095    public static Sum of(double... values) {
096        return new Sum(org.apache.commons.numbers.core.Sum.of(values));
097    }
098
099    /**
100     * Returns an instance populated using the specified range of {@code values}.
101     *
102     * <p>The result is {@code NaN} if any of the values is {@code NaN}
103     * or the sum at any point is a {@code NaN}.
104     *
105     * <p>When the range is empty, the result is zero.
106     *
107     * @param values Values.
108     * @param from Inclusive start of the range.
109     * @param to Exclusive end of the range.
110     * @return {@code Sum} instance.
111     * @throws IndexOutOfBoundsException if the sub-range is out of bounds
112     * @since 1.2
113     */
114    public static Sum ofRange(double[] values, int from, int to) {
115        Statistics.checkFromToIndex(from, to, values.length);
116        return new Sum(Statistics.sum(values, from, to));
117    }
118
119    /**
120     * Updates the state of the statistic to reflect the addition of {@code value}.
121     *
122     * @param value Value.
123     */
124    @Override
125    public void accept(double value) {
126        delegate.accept(value);
127    }
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     * @return sum of all values.
135     */
136    @Override
137    public double getAsDouble() {
138        return delegate.getAsDouble();
139    }
140
141    @Override
142    public Sum combine(Sum other) {
143        delegate.add(other.delegate);
144        return this;
145    }
146}