/* * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.util; import java.util.function.IntConsumer; import java.util.stream.Collector; /** * A state object for collecting statistics such as count, min, max, sum, and * average. * *
This class is designed to work with (though does not require) * {@linkplain java.util.stream streams}. For example, you can compute * summary statistics on a stream of ints with: *
{@code * IntSummaryStatistics stats = intStream.collect(IntSummaryStatistics::new, * IntSummaryStatistics::accept, * IntSummaryStatistics::combine); * }* *
{@code IntSummaryStatistics} can be used as a * {@linkplain java.util.stream.Stream#collect(Collector) reduction} * target for a {@linkplain java.util.stream.Stream stream}. For example: * *
{@code * IntSummaryStatistics stats = people.stream() * .collect(Collectors.summarizingInt(Person::getDependents)); *}* * This computes, in a single pass, the count of people, as well as the minimum, * maximum, sum, and average of their number of dependents. * * @implNote This implementation is not thread safe. However, it is safe to use * {@link java.util.stream.Collectors#summarizingInt(java.util.function.ToIntFunction) * Collectors.summarizingInt()} on a parallel stream, because the parallel * implementation of {@link java.util.stream.Stream#collect Stream.collect()} * provides the necessary partitioning, isolation, and merging of results for * safe and efficient parallel execution. * *
This implementation does not check for overflow of the count or the sum. * @since 1.8 */ public class IntSummaryStatistics implements IntConsumer { private long count; private long sum; private int min = Integer.MAX_VALUE; private int max = Integer.MIN_VALUE; /** * Constructs an empty instance with zero count, zero sum, * {@code Integer.MAX_VALUE} min, {@code Integer.MIN_VALUE} max and zero * average. */ public IntSummaryStatistics() { } /** * Constructs a non-empty instance with the specified {@code count}, * {@code min}, {@code max}, and {@code sum}. * *
If {@code count} is zero then the remaining arguments are ignored and * an empty instance is constructed. * *
If the arguments are inconsistent then an {@code IllegalArgumentException} * is thrown. The necessary consistent argument conditions are: *