1 /*
2 * Copyright (c) 2012, 2017, 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
43 *
44 * <p>{@code LongSummaryStatistics} can be used as a
45 * {@linkplain java.util.stream.Stream#collect(Collector) reduction}
46 * target for a {@linkplain java.util.stream.Stream stream}. For example:
47 *
48 * <pre> {@code
49 * LongSummaryStatistics stats = people.stream()
50 * .collect(Collectors.summarizingLong(Person::getAge));
51 *}</pre>
52 *
53 * This computes, in a single pass, the count of people, as well as the minimum,
54 * maximum, sum, and average of their ages.
55 *
56 * @implNote This implementation is not thread safe. However, it is safe to use
57 * {@link java.util.stream.Collectors#summarizingLong(java.util.function.ToLongFunction)
58 * Collectors.summarizingLong()} on a parallel stream, because the parallel
59 * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
60 * provides the necessary partitioning, isolation, and merging of results for
61 * safe and efficient parallel execution.
62 *
63 * <p>This implementation does not check for overflow of the sum.
64 * @since 1.8
65 */
66 public class LongSummaryStatistics implements LongConsumer, IntConsumer {
67 private long count;
68 private long sum;
69 private long min = Long.MAX_VALUE;
70 private long max = Long.MIN_VALUE;
71
72 /**
73 * Constructs an empty instance with zero count, zero sum,
74 * {@code Long.MAX_VALUE} min, {@code Long.MIN_VALUE} max and zero
75 * average.
76 */
77 public LongSummaryStatistics() { }
78
79 /**
80 * Constructs a non-empty instance with the specified {@code count},
81 * {@code min}, {@code max}, and {@code sum}.
82 *
83 * <p>If {@code count} is zero then the remaining arguments are ignored and
| 1 /*
2 * Copyright (c) 2012, 2019, 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
43 *
44 * <p>{@code LongSummaryStatistics} can be used as a
45 * {@linkplain java.util.stream.Stream#collect(Collector) reduction}
46 * target for a {@linkplain java.util.stream.Stream stream}. For example:
47 *
48 * <pre> {@code
49 * LongSummaryStatistics stats = people.stream()
50 * .collect(Collectors.summarizingLong(Person::getAge));
51 *}</pre>
52 *
53 * This computes, in a single pass, the count of people, as well as the minimum,
54 * maximum, sum, and average of their ages.
55 *
56 * @implNote This implementation is not thread safe. However, it is safe to use
57 * {@link java.util.stream.Collectors#summarizingLong(java.util.function.ToLongFunction)
58 * Collectors.summarizingLong()} on a parallel stream, because the parallel
59 * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
60 * provides the necessary partitioning, isolation, and merging of results for
61 * safe and efficient parallel execution.
62 *
63 * <p>This implementation does not check for overflow of the count or the sum.
64 * @since 1.8
65 */
66 public class LongSummaryStatistics implements LongConsumer, IntConsumer {
67 private long count;
68 private long sum;
69 private long min = Long.MAX_VALUE;
70 private long max = Long.MIN_VALUE;
71
72 /**
73 * Constructs an empty instance with zero count, zero sum,
74 * {@code Long.MAX_VALUE} min, {@code Long.MIN_VALUE} max and zero
75 * average.
76 */
77 public LongSummaryStatistics() { }
78
79 /**
80 * Constructs a non-empty instance with the specified {@code count},
81 * {@code min}, {@code max}, and {@code sum}.
82 *
83 * <p>If {@code count} is zero then the remaining arguments are ignored and
|