1 /* 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.util; 26 27 import java.util.function.DoubleConsumer; 28 import java.util.stream.Collector; 29 30 /** 31 * A state object for collecting statistics such as count, min, max, sum, and 32 * average. 33 * 34 * <p>This class is designed to work with (though does not require) 35 * {@linkplain java.util.stream streams}. For example, you can compute 36 * summary statistics on a stream of doubles with: 37 * <pre> {@code 38 * DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new, 39 * DoubleSummaryStatistics::accept, 40 * DoubleSummaryStatistics::combine); 41 * }</pre> 42 * 43 * <p>{@code DoubleSummaryStatistics} can be used as a 44 * {@linkplain java.util.stream.Stream#collect(Collector) reduction} 45 * target for a {@linkplain java.util.stream.Stream stream}. For example: 46 * 47 * <pre> {@code 48 * DoubleSummaryStatistics stats = people.stream() 49 * .collect(Collectors.summarizingDouble(Person::getWeight)); 50 *}</pre> 51 * 52 * This computes, in a single pass, the count of people, as well as the minimum, 53 * maximum, sum, and average of their weights. 54 * 55 * @implNote This implementation is not thread safe. However, it is safe to use 56 * {@link java.util.stream.Collectors#summarizingDouble(java.util.function.ToDoubleFunction) 57 * Collectors.toDoubleStatistics()} on a parallel stream, because the parallel 58 * implementation of {@link java.util.stream.Stream#collect Stream.collect()} 59 * provides the necessary partitioning, isolation, and merging of results for 60 * safe and efficient parallel execution. 61 * @since 1.8 62 */ 63 public class DoubleSummaryStatistics implements DoubleConsumer { 64 private long count; 65 private double sum; 66 private double sumCompensation; // Low order bits of sum 67 private double min = Double.POSITIVE_INFINITY; 68 private double max = Double.NEGATIVE_INFINITY; 69 70 /** 71 * Construct an empty instance with zero count, zero sum, 72 * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY} 73 * max and zero average. 74 */ 75 public DoubleSummaryStatistics() { } 76 77 /** 78 * Records another value into the summary information. 79 * 80 * @param value the input value 81 */ 82 @Override 83 public void accept(double value) { 84 ++count; 85 sumWithCompensation(value); 86 min = Math.min(min, value); 87 max = Math.max(max, value); 88 } 89 90 /** 91 * Combines the state of another {@code DoubleSummaryStatistics} into this 92 * one. 93 * 94 * @param other another {@code DoubleSummaryStatistics} 95 * @throws NullPointerException if {@code other} is null 96 */ 97 public void combine(DoubleSummaryStatistics other) { 98 count += other.count; 99 sumWithCompensation(other.sum); 100 sumWithCompensation(other.sumCompensation); 101 min = Math.min(min, other.min); 102 max = Math.max(max, other.max); 103 } 104 105 /** 106 * Incorporate a new double value using Kahan summation / 107 * compensated summation. 108 */ 109 private void sumWithCompensation(double value) { 110 double tmp = value - sumCompensation; 111 double velvel = sum + tmp; // Little wolf of rounding error 112 sumCompensation = (velvel - sum) - tmp; 113 sum = velvel; 114 } 115 116 /** 117 * Return the count of values recorded. 118 * 119 * @return the count of values 120 */ 121 public final long getCount() { 122 return count; 123 } 124 125 /** 126 * Returns the sum of values recorded, or zero if no values have been 127 * recorded. 128 * 129 * If any recorded value is a NaN or the sum is at any point a NaN 130 * then the sum will be NaN. 131 * 132 * <p> The value of a floating-point sum is a function both of the 133 * input values as well as the order of addition operations. The 134 * order of addition operations of this method is intentionally 135 * not defined to allow for implementation flexibility to improve 136 * the speed and accuracy of the computed result. 137 * 138 * In particular, this method may be implemented using compensated 139 * summation or other technique to reduce the error bound in the 140 * numerical sum compared to a simple summation of {@code double} 141 * values. 142 * 143 * @apiNote Values sorted by increasing absolute magnitude tend to yield 144 * more accurate results. 145 * 146 * @return the sum of values, or zero if none 147 */ 148 public final double getSum() { 149 return sum; 150 } 151 152 /** 153 * Returns the minimum recorded value, {@code Double.NaN} if any recorded 154 * value was NaN or {@code Double.POSITIVE_INFINITY} if no values were 155 * recorded. Unlike the numerical comparison operators, this method 156 * considers negative zero to be strictly smaller than positive zero. 157 * 158 * @return the minimum recorded value, {@code Double.NaN} if any recorded 159 * value was NaN or {@code Double.POSITIVE_INFINITY} if no values were 160 * recorded 161 */ 162 public final double getMin() { 163 return min; 164 } 165 166 /** 167 * Returns the maximum recorded value, {@code Double.NaN} if any recorded 168 * value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were 169 * recorded. Unlike the numerical comparison operators, this method 170 * considers negative zero to be strictly smaller than positive zero. 171 * 172 * @return the maximum recorded value, {@code Double.NaN} if any recorded 173 * value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were 174 * recorded 175 */ 176 public final double getMax() { 177 return max; 178 } 179 180 /** 181 * Returns the arithmetic mean of values recorded, or zero if no 182 * values have been recorded. 183 * 184 * If any recorded value is a NaN or the sum is at any point a NaN 185 * then the average will be code NaN. 186 * 187 * <p>The average returned can vary depending upon the order in 188 * which values are recorded. 189 * 190 * This method may be implemented using compensated summation or 191 * other technique to reduce the error bound in the {@link #getSum 192 * numerical sum} used to compute the average. 193 * 194 * @apiNote Values sorted by increasing absolute magnitude tend to yield 195 * more accurate results. 196 * 197 * @return the arithmetic mean of values, or zero if none 198 */ 199 public final double getAverage() { 200 return getCount() > 0 ? getSum() / getCount() : 0.0d; 201 } 202 203 /** 204 * {@inheritDoc} 205 * 206 * Returns a non-empty string representation of this object suitable for 207 * debugging. The exact presentation format is unspecified and may vary 208 * between implementations and versions. 209 */ 210 @Override 211 public String toString() { 212 return String.format( 213 "%s{count=%d, sum=%f, min=%f, average=%f, max=%f}", 214 this.getClass().getSimpleName(), 215 getCount(), 216 getSum(), 217 getMin(), 218 getAverage(), 219 getMax()); 220 } 221 }--- EOF ---