jdk/src/share/classes/java/util/IntSummaryStatistics.java
author mduigou
Fri, 31 May 2013 11:06:04 -0700
changeset 17927 dc3d1b30cb78
parent 16922 6c918c972850
child 19214 e5901820c3c1
permissions -rw-r--r--
8015686: {Int|Long}SummaryStatistics toString() throws IllegalFormatConversionException Reviewed-by: dholmes, alanb, psandoz
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;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    28
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    29
/**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    30
 * A state object for collecting statistics such as count, min, max, sum, and
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    31
 * average.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    32
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    33
 * <p>This class is designed to work with (though does not require)
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    34
 * {@linkplain java.util.stream streams}. For example, you can compute
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    35
 * summary statistics on a stream of ints with:
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    36
 * <pre> {@code
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    37
 * IntSummaryStatistics stats = intStream.collect(IntSummaryStatistics::new,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    38
 *     IntSummaryStatistics::accept,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    39
 *     IntSummaryStatistics::combine);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    40
 * }</pre>
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    41
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    42
 * <p>{@code IntSummaryStatistics} can be used as a
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    43
 * {@linkplain java.util.stream.Stream#reduce(java.util.function.BinaryOperator) reduction}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    44
 * target for a {@linkplain java.util.stream.Stream stream}. For example:
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    45
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    46
 * <pre> {@code
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    47
 * IntSummaryStatistics stats = people.stream()
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    48
 *     .collect(Collectors.toIntSummaryStatistics(Person::getDependents));
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    49
 *}</pre>
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    50
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    51
 * 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
    52
 * maximum, sum, and average of their number of dependents.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    53
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    54
 * @implNote This implementation is not thread safe. However, it is safe to use
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    55
 * {@link java.util.stream.Collectors#toIntSummaryStatistics(java.util.function.ToIntFunction)
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    56
 * Collectors.toIntStatistics()} on a parallel stream, because the parallel
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    57
 * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    58
 * provides the necessary partitioning, isolation, and merging of results for
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    59
 * safe and efficient parallel execution.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    60
 *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    61
 * <p>This implementation does not check for overflow of the sum.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    62
 * @since 1.8
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    63
 */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    64
public class IntSummaryStatistics implements IntConsumer {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    65
    private long count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    66
    private long sum;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    67
    private int min = Integer.MAX_VALUE;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    68
    private int max = Integer.MIN_VALUE;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    69
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    70
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    71
     * Construct an empty instance with zero count, zero sum,
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    72
     * {@code Integer.MAX_VALUE} min, {@code Integer.MIN_VALUE} max and zero
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    73
     * average.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    74
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    75
    public IntSummaryStatistics() { }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    76
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    77
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    78
     * Records a new value into the summary information
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    79
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    80
     * @param value the input value
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    81
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    82
    @Override
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    83
    public void accept(int value) {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    84
        ++count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    85
        sum += value;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    86
        min = Math.min(min, value);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    87
        max = Math.max(max, value);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    88
    }
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
     * Combines the state of another {@code IntSummaryStatistics} into this one.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    92
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    93
     * @param other another {@code IntSummaryStatistics}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    94
     * @throws NullPointerException if {@code other} is null
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    95
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    96
    public void combine(IntSummaryStatistics other) {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    97
        count += other.count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    98
        sum += other.sum;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
    99
        min = Math.min(min, other.min);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   100
        max = Math.max(max, other.max);
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   101
    }
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
     * Returns the count of values recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   105
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   106
     * @return the count of values
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   107
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   108
    public final long getCount() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   109
        return count;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   110
    }
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
     * Returns the sum of values recorded, or zero if no values have been
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   114
     * recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   115
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   116
     * @return the sum of values, or zero if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   117
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   118
    public final long getSum() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   119
        return sum;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   120
    }
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
     * Returns the minimum value recorded, or {@code Integer.MAX_VALUE} if no
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   124
     * values have been recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   125
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   126
     * @return the minimum value, or {@code Integer.MAX_VALUE} if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   127
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   128
    public final int getMin() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   129
        return min;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   130
    }
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
     * Returns the maximum value recorded, or {@code Integer.MIN_VALUE} if no
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   134
     * values have been recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   135
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   136
     * @return the maximum value, or {@code Integer.MIN_VALUE} if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   137
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   138
    public final int getMax() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   139
        return max;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   140
    }
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
     * Returns the average of values recorded, or zero if no values have been
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   144
     * recorded.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   145
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   146
     * @return the average of values, or zero if none
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   147
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   148
    public final double getAverage() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   149
        return getCount() > 0 ? (double) getSum() / getCount() : 0.0d;
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   150
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   151
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   152
    @Override
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   153
    /**
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   154
     * {@inheritDoc}
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   155
     *
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   156
     * Returns a non-empty string representation of this object suitable for
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   157
     * debugging. The exact presentation format is unspecified and may vary
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   158
     * between implementations and versions.
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   159
     */
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   160
    public String toString() {
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   161
        return String.format(
17927
dc3d1b30cb78 8015686: {Int|Long}SummaryStatistics toString() throws IllegalFormatConversionException
mduigou
parents: 16922
diff changeset
   162
            "%s{count=%d, sum=%d, min=%d, average=%f, max=%d}",
16922
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   163
            this.getClass().getSimpleName(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   164
            getCount(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   165
            getSum(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   166
            getMin(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   167
            getAverage(),
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   168
            getMax());
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   169
    }
6c918c972850 8010953: Add primitive summary statistics utils
mduigou
parents:
diff changeset
   170
}