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 19 /** 20 * Returns the sum of the available values. 21 * 22 * <ul> 23 * <li>The result is zero if no values are added. 24 * <li>The result is {@code NaN} if any of the values is {@code NaN}. 25 * <li>The result is {@code NaN} if the values contain positive and negative infinity. 26 * <li>The result is non-finite if the values contain infinities of the same sign. 27 * </ul> 28 * 29 * <p>Note: In the event of infinite values of the same sign the result will be non-finite. 30 * In this case the returned results may differ (either the same infinity or {@code NaN}) based on 31 * input order if the sum of the finite-only values can overflow to an opposite signed infinity. 32 * 33 * <p>This class is designed to work with (though does not require) 34 * {@linkplain java.util.stream streams}. 35 * 36 * <p><strong>This instance is not thread safe.</strong> 37 * If multiple threads access an instance of this class concurrently, 38 * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or 39 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally. 40 * 41 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept} 42 * and {@link StatisticAccumulator#combine(StatisticResult) combine} 43 * as {@code accumulator} and {@code combiner} functions of 44 * {@link java.util.stream.Collector Collector} on a parallel stream, 45 * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()} 46 * provides the necessary partitioning, isolation, and merging of results for 47 * safe and efficient parallel execution. 48 * 49 * @since 1.1 50 * @see org.apache.commons.numbers.core.Sum 51 */ 52 public final class Sum implements DoubleStatistic, StatisticAccumulator<Sum> { 53 54 /** {@link org.apache.commons.numbers.core.Sum Sum} used to compute the sum. */ 55 private final org.apache.commons.numbers.core.Sum delegate; 56 57 /** 58 * Create an instance. 59 */ 60 private Sum() { 61 this(org.apache.commons.numbers.core.Sum.create()); 62 } 63 64 /** 65 * Create an instance using the specified {@code sum}. 66 * 67 * @param sum Sum. 68 */ 69 Sum(org.apache.commons.numbers.core.Sum sum) { 70 delegate = sum; 71 } 72 73 /** 74 * Creates an instance. 75 * 76 * <p>The initial result is zero. 77 * 78 * @return {@code Sum} instance. 79 */ 80 public static Sum create() { 81 return new Sum(); 82 } 83 84 /** 85 * Returns an instance populated using the input {@code values}. 86 * 87 * <p>The result is {@code NaN} if any of the values is {@code NaN} 88 * or the sum at any point is a {@code NaN}. 89 * 90 * <p>When the input is an empty array, the result is zero. 91 * 92 * @param values Values. 93 * @return {@code Sum} instance. 94 */ 95 public static Sum of(double... values) { 96 return new Sum(org.apache.commons.numbers.core.Sum.of(values)); 97 } 98 99 /** 100 * Updates the state of the statistic to reflect the addition of {@code value}. 101 * 102 * @param value Value. 103 */ 104 @Override 105 public void accept(double value) { 106 delegate.accept(value); 107 } 108 109 /** 110 * Gets the sum of all input values. 111 * 112 * <p>When no values have been added, the result is zero. 113 * 114 * @return sum of all values. 115 */ 116 @Override 117 public double getAsDouble() { 118 return delegate.getAsDouble(); 119 } 120 121 @Override 122 public Sum combine(Sum other) { 123 delegate.add(other.delegate); 124 return this; 125 } 126 }