jdk/src/java.base/share/classes/java/util/IntSummaryStatistics.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
child 31262 b694a580405b
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     1
/*
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     4
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    10
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    15
 * accompanied this code).
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    16
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    20
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    23
 * questions.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    24
 */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    25
package java.util;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    26
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    27
import java.util.function.IntConsumer;
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
    28
import java.util.stream.Collector;
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    29
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    30
/**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    31
 * A state object for collecting statistics such as count, min, max, sum, and
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    32
 * average.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    33
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    34
 * <p>This class is designed to work with (though does not require)
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    35
 * {@linkplain java.util.stream streams}. For example, you can compute
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    36
 * summary statistics on a stream of ints with:
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    37
 * <pre> {@code
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    38
 * IntSummaryStatistics stats = intStream.collect(IntSummaryStatistics::new,
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
    39
 *                                                IntSummaryStatistics::accept,
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
    40
 *                                                IntSummaryStatistics::combine);
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    41
 * }</pre>
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    42
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    43
 * <p>{@code IntSummaryStatistics} can be used as a
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
    44
 * {@linkplain java.util.stream.Stream#collect(Collector) reduction}
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    45
 * target for a {@linkplain java.util.stream.Stream stream}. For example:
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    46
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    47
 * <pre> {@code
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    48
 * IntSummaryStatistics stats = people.stream()
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
    49
 *                                    .collect(Collectors.summarizingInt(Person::getDependents));
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    50
 *}</pre>
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    51
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    52
 * This computes, in a single pass, the count of people, as well as the minimum,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    53
 * maximum, sum, and average of their number of dependents.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    54
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    55
 * @implNote This implementation is not thread safe. However, it is safe to use
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
    56
 * {@link java.util.stream.Collectors#summarizingInt(java.util.function.ToIntFunction)
24513
2b9212fa87d8 8043772: Typos in Double/Int/LongSummaryStatistics.java
igerasim
parents: 19214
diff changeset
    57
 * Collectors.summarizingInt()} on a parallel stream, because the parallel
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    58
 * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    59
 * provides the necessary partitioning, isolation, and merging of results for
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    60
 * safe and efficient parallel execution.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    61
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    62
 * <p>This implementation does not check for overflow of the sum.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    63
 * @since 1.8
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    64
 */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    65
public class IntSummaryStatistics implements IntConsumer {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    66
    private long count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    67
    private long sum;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    68
    private int min = Integer.MAX_VALUE;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    69
    private int max = Integer.MIN_VALUE;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    70
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    71
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    72
     * Construct an empty instance with zero count, zero sum,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    73
     * {@code Integer.MAX_VALUE} min, {@code Integer.MIN_VALUE} max and zero
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    74
     * average.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    75
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    76
    public IntSummaryStatistics() { }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    77
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    78
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    79
     * Records a new value into the summary information
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    80
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    81
     * @param value the input value
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    82
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    83
    @Override
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    84
    public void accept(int value) {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    85
        ++count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    86
        sum += value;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    87
        min = Math.min(min, value);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    88
        max = Math.max(max, value);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    89
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    90
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    91
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    92
     * Combines the state of another {@code IntSummaryStatistics} into this one.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    93
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    94
     * @param other another {@code IntSummaryStatistics}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    95
     * @throws NullPointerException if {@code other} is null
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    96
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    97
    public void combine(IntSummaryStatistics other) {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    98
        count += other.count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    99
        sum += other.sum;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   100
        min = Math.min(min, other.min);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   101
        max = Math.max(max, other.max);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   102
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   103
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   104
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   105
     * Returns the count of values recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   106
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   107
     * @return the count of values
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   108
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   109
    public final long getCount() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   110
        return count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   111
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   112
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   113
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   114
     * Returns the sum of values recorded, or zero if no values have been
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   115
     * recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   116
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   117
     * @return the sum of values, or zero if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   118
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   119
    public final long getSum() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   120
        return sum;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   121
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   122
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   123
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   124
     * Returns the minimum value recorded, or {@code Integer.MAX_VALUE} if no
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   125
     * values have been recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   126
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   127
     * @return the minimum value, or {@code Integer.MAX_VALUE} if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   128
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   129
    public final int getMin() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   130
        return min;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   131
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   132
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   133
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   134
     * Returns the maximum value recorded, or {@code Integer.MIN_VALUE} if no
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   135
     * values have been recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   136
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   137
     * @return the maximum value, or {@code Integer.MIN_VALUE} if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   138
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   139
    public final int getMax() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   140
        return max;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   141
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   142
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   143
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
   144
     * Returns the arithmetic mean of values recorded, or zero if no values have been
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   145
     * recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   146
     *
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 17927
diff changeset
   147
     * @return the arithmetic mean of values, or zero if none
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   148
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   149
    public final double getAverage() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   150
        return getCount() > 0 ? (double) getSum() / getCount() : 0.0d;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   151
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   152
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   153
    @Override
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   154
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   155
     * {@inheritDoc}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   156
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   157
     * Returns a non-empty string representation of this object suitable for
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   158
     * debugging. The exact presentation format is unspecified and may vary
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   159
     * between implementations and versions.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   160
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   161
    public String toString() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   162
        return String.format(
17927
dc3d1b30cb78 8015686: {Int|Long}SummaryStatistics toString() throws IllegalFormatConversionException
mduigou
parents: 16922
diff changeset
   163
            "%s{count=%d, sum=%d, min=%d, average=%f, max=%d}",
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   164
            this.getClass().getSimpleName(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   165
            getCount(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   166
            getSum(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   167
            getMin(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   168
            getAverage(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   169
            getMax());
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   170
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   171
}