Port OpenJDK8 java.util.*SummaryStatistics & add tests
Ported OpenJDK8 java.util.DoubleSummaryStatistics,
java.util.IntSummaryStatistics, java.util.LongSummaryStatistics.
Bug: 27426738
Change-Id: Ie8e3f7fc148615c38f2cf7bce971617599d00813
diff --git a/luni/src/test/java/libcore/java/util/DoubleSummaryStatisticsTest.java b/luni/src/test/java/libcore/java/util/DoubleSummaryStatisticsTest.java
new file mode 100644
index 0000000..313b8c6
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/DoubleSummaryStatisticsTest.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.util;
+
+import java.util.DoubleSummaryStatistics;
+
+public class DoubleSummaryStatisticsTest extends junit.framework.TestCase {
+
+ private static final double data1[] = {22.4, -53.4, 74.8, -12.4, 17, 0, 100};
+ private static final double data2[] = {1.2, 3.5, 2.7, 1, 7.6};
+
+ public void test_empty() {
+ DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
+ assertEquals(0, dss.getCount());
+ assertEquals(0.0d, dss.getSum());
+ assertEquals(0.0d, dss.getAverage());
+ assertEquals(Double.POSITIVE_INFINITY, dss.getMin());
+ assertEquals(Double.NEGATIVE_INFINITY, dss.getMax());
+ }
+
+ public void test_accept() {
+ DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
+ dss.accept(100.5d);
+ assertEquals(1, dss.getCount());
+ assertEquals(100.5d, dss.getSum());
+ dss.accept(45.0d);
+ assertEquals(2, dss.getCount());
+ assertEquals(145.5d, dss.getSum());
+ }
+
+ public void test_combine() {
+ DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData2();
+ DoubleSummaryStatistics dssCombined = getDoubleSummaryStatisticsData1();
+ dssCombined.combine(dss1);
+ assertEquals(12, dssCombined.getCount());
+ assertEquals(164.4d, dssCombined.getSum());
+ assertEquals(100.0d, dssCombined.getMax());
+ assertEquals(-53.4d, dssCombined.getMin());
+ assertEquals(13.7, dssCombined.getAverage(), 1E-6);
+ }
+
+ public void test_getCount() {
+ DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
+ assertEquals(data1.length, dss1.getCount());
+ }
+
+ public void test_getSum() {
+ DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
+ assertEquals(148.4, dss1.getSum());
+
+ dss1.accept(Double.NaN);
+ assertEquals(Double.NaN, dss1.getSum());
+ }
+
+ public void test_getMin() {
+ DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
+ assertEquals(-53.4d, dss1.getMin());
+
+ dss1.accept(Double.NaN);
+ assertEquals(Double.NaN, dss1.getMin());
+ }
+
+ public void test_getMax() {
+ DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
+ assertEquals(100.0d, dss1.getMax());
+
+ dss1.accept(Double.NaN);
+ assertEquals(Double.NaN, dss1.getMax());
+ }
+
+ public void test_getAverage() {
+ DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
+ assertEquals(21.2, dss1.getAverage(), 1E-6);
+
+ dss1.accept(Double.NaN);
+ assertEquals(Double.NaN, dss1.getAverage());
+ }
+
+ private static DoubleSummaryStatistics getDoubleSummaryStatisticsData1() {
+ DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
+ for (double value : data1) {
+ dss.accept(value);
+ }
+ return dss;
+ }
+
+ private static DoubleSummaryStatistics getDoubleSummaryStatisticsData2() {
+ DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
+ for (double value : data2) {
+ dss.accept(value);
+ }
+ return dss;
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/IntSummaryStatisticsTest.java b/luni/src/test/java/libcore/java/util/IntSummaryStatisticsTest.java
new file mode 100644
index 0000000..925b29a
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/IntSummaryStatisticsTest.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.util;
+
+import java.util.IntSummaryStatistics;
+
+public class IntSummaryStatisticsTest extends junit.framework.TestCase {
+
+ private static final int data1[] = {2, -5, 7, -1, 1, 0, 100};
+ private static final int data2[] = {1, 3, 2, 1, 7};
+
+ public void test_empty() {
+ IntSummaryStatistics iss = new IntSummaryStatistics();
+ assertEquals(0, iss.getCount());
+ assertEquals(0, iss.getSum());
+ assertEquals(0.0d, iss.getAverage());
+ assertEquals(Integer.MAX_VALUE, iss.getMin());
+ assertEquals(Integer.MIN_VALUE, iss.getMax());
+ }
+
+ public void test_accept() {
+ IntSummaryStatistics iss = new IntSummaryStatistics();
+ iss.accept(5);
+ assertEquals(1, iss.getCount());
+ assertEquals(5, iss.getSum());
+ iss.accept(10);
+ assertEquals(2, iss.getCount());
+ assertEquals(15, iss.getSum());
+ }
+
+ public void test_combine() {
+ IntSummaryStatistics iss1 = getIntSummaryStatisticsData2();
+ IntSummaryStatistics issCombined = getIntSummaryStatisticsData1();
+ issCombined.combine(iss1);
+
+ assertEquals(12, issCombined.getCount());
+ assertEquals(118, issCombined.getSum());
+ assertEquals(100, issCombined.getMax());
+ assertEquals(-5, issCombined.getMin());
+ assertEquals(9.833333d, issCombined.getAverage(), 1E-6);
+ }
+
+ public void test_getCount() {
+ IntSummaryStatistics iss1 = getIntSummaryStatisticsData1();
+ assertEquals(data1.length, iss1.getCount());
+ }
+
+ public void test_getSum() {
+ IntSummaryStatistics iss1 = getIntSummaryStatisticsData1();
+ assertEquals(104, iss1.getSum());
+ }
+
+ public void test_getMin() {
+ IntSummaryStatistics iss1 = getIntSummaryStatisticsData1();
+ assertEquals(-5, iss1.getMin());
+ }
+
+ public void test_getMax() {
+ IntSummaryStatistics iss1 = getIntSummaryStatisticsData1();
+ assertEquals(100, iss1.getMax());
+ }
+
+ public void test_getAverage() {
+ IntSummaryStatistics iss1 = getIntSummaryStatisticsData1();
+ assertEquals(14.857142, iss1.getAverage(), 1E-6);
+ }
+
+ private static IntSummaryStatistics getIntSummaryStatisticsData1() {
+ IntSummaryStatistics iss = new IntSummaryStatistics();
+ for (int value : data1) {
+ iss.accept(value);
+ }
+ return iss;
+ }
+
+ private static IntSummaryStatistics getIntSummaryStatisticsData2() {
+ IntSummaryStatistics iss = new IntSummaryStatistics();
+ for (int value : data2) {
+ iss.accept(value);
+ }
+ return iss;
+ }
+}
diff --git a/luni/src/test/java/libcore/java/util/LongSummaryStatisticsTest.java b/luni/src/test/java/libcore/java/util/LongSummaryStatisticsTest.java
new file mode 100644
index 0000000..300c087
--- /dev/null
+++ b/luni/src/test/java/libcore/java/util/LongSummaryStatisticsTest.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package libcore.java.util;
+
+import java.util.LongSummaryStatistics;
+
+public class LongSummaryStatisticsTest extends junit.framework.TestCase {
+
+ private static final long data1[] = {2, -5, 7, -1, 1, 0, 100};
+ private static final long data2[] = {1, 3, 2, 1, 7};
+
+ public void test_empty() {
+ LongSummaryStatistics lss = new LongSummaryStatistics();
+ assertEquals(0, lss.getCount());
+ assertEquals(0, lss.getSum());
+ assertEquals(0.0d, lss.getAverage());
+ assertEquals(Long.MAX_VALUE, lss.getMin());
+ assertEquals(Long.MIN_VALUE, lss.getMax());
+ }
+
+ public void test_accept() {
+ LongSummaryStatistics lss = new LongSummaryStatistics();
+
+ // For long values
+ lss.accept(100L);
+ assertEquals(1, lss.getCount());
+ assertEquals(100L, lss.getSum());
+ lss.accept(250L);
+ assertEquals(2, lss.getCount());
+ assertEquals(350L, lss.getSum());
+
+ // for int values
+ lss.accept(50);
+ assertEquals(3, lss.getCount());
+ assertEquals(400L, lss.getSum());
+ lss.accept(200);
+ assertEquals(4, lss.getCount());
+ assertEquals(600L, lss.getSum());
+ }
+
+ public void test_combine() {
+ LongSummaryStatistics lss1 = getLongSummaryStatisticsData2();
+ LongSummaryStatistics lssCombined = getLongSummaryStatisticsData1();
+ lssCombined.combine(lss1);
+
+ assertEquals(12, lssCombined.getCount());
+ assertEquals(118L, lssCombined.getSum());
+ assertEquals(100L, lssCombined.getMax());
+ assertEquals(-5L, lssCombined.getMin());
+ assertEquals(9.833333, lssCombined.getAverage(), 1E-6);
+ }
+
+ public void test_getCount() {
+ LongSummaryStatistics lss1 = getLongSummaryStatisticsData1();
+ assertEquals(data1.length, lss1.getCount());
+ }
+
+ public void test_getSum() {
+ LongSummaryStatistics lss1 = getLongSummaryStatisticsData1();
+ assertEquals(104L, lss1.getSum());
+ }
+
+ public void test_getMin() {
+ LongSummaryStatistics lss1 = getLongSummaryStatisticsData1();
+ assertEquals(-5L, lss1.getMin());
+ }
+
+ public void test_getMax() {
+ LongSummaryStatistics lss1 = getLongSummaryStatisticsData1();
+ assertEquals(100L, lss1.getMax());
+ }
+
+ public void test_getAverage() {
+ LongSummaryStatistics lss1 = getLongSummaryStatisticsData1();
+ assertEquals(14.857142, lss1.getAverage(), 1E-6);
+ }
+
+ private static LongSummaryStatistics getLongSummaryStatisticsData1() {
+ LongSummaryStatistics lss = new LongSummaryStatistics();
+ for (long value : data1) {
+ lss.accept(value);
+ }
+ return lss;
+ }
+
+ private static LongSummaryStatistics getLongSummaryStatisticsData2() {
+ LongSummaryStatistics lss = new LongSummaryStatistics();
+ for (long value : data2) {
+ lss.accept(value);
+ }
+ return lss;
+ }
+}
diff --git a/ojluni/src/main/java/java/util/DoubleSummaryStatistics.java b/ojluni/src/main/java/java/util/DoubleSummaryStatistics.java
new file mode 100644
index 0000000..599bd18
--- /dev/null
+++ b/ojluni/src/main/java/java/util/DoubleSummaryStatistics.java
@@ -0,0 +1,232 @@
+/*
+ * Copyright (c) 2012, 2013, 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.DoubleConsumer;
+
+/**
+ * A state object for collecting statistics such as count, min, max, sum, and
+ * average.
+ *
+ * <p>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 doubles with:
+ * <pre> {@code
+ * DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new,
+ * DoubleSummaryStatistics::accept,
+ * DoubleSummaryStatistics::combine);
+ * }</pre>
+ *
+ * <p>{@code DoubleSummaryStatistics} can be used as a
+ * {@linkplain java.util.stream.Stream#collect(Collector) reduction}
+ * target for a {@linkplain java.util.stream.Stream stream}. For example:
+ *
+ * <pre> {@code
+ * DoubleSummaryStatistics stats = people.stream()
+ * .collect(Collectors.summarizingDouble(Person::getWeight));
+ *}</pre>
+ *
+ * This computes, in a single pass, the count of people, as well as the minimum,
+ * maximum, sum, and average of their weights.
+ *
+ * @implNote This implementation is not thread safe. However, it is safe to use
+ * {@link java.util.stream.Collectors#summarizingDouble(java.util.function.ToDoubleFunction)
+ * Collectors.toDoubleStatistics()} 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.
+ * @since 1.8
+ */
+public class DoubleSummaryStatistics implements DoubleConsumer {
+ private long count;
+ private double sum;
+ private double sumCompensation; // Low order bits of sum
+ private double simpleSum; // Used to compute right sum for non-finite inputs
+ private double min = Double.POSITIVE_INFINITY;
+ private double max = Double.NEGATIVE_INFINITY;
+
+ /**
+ * Construct an empty instance with zero count, zero sum,
+ * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY}
+ * max and zero average.
+ */
+ public DoubleSummaryStatistics() { }
+
+ /**
+ * Records another value into the summary information.
+ *
+ * @param value the input value
+ */
+ @Override
+ public void accept(double value) {
+ ++count;
+ simpleSum += value;
+ sumWithCompensation(value);
+ min = Math.min(min, value);
+ max = Math.max(max, value);
+ }
+
+ /**
+ * Combines the state of another {@code DoubleSummaryStatistics} into this
+ * one.
+ *
+ * @param other another {@code DoubleSummaryStatistics}
+ * @throws NullPointerException if {@code other} is null
+ */
+ public void combine(DoubleSummaryStatistics other) {
+ count += other.count;
+ simpleSum += other.simpleSum;
+ sumWithCompensation(other.sum);
+ sumWithCompensation(other.sumCompensation);
+ min = Math.min(min, other.min);
+ max = Math.max(max, other.max);
+ }
+
+ /**
+ * Incorporate a new double value using Kahan summation /
+ * compensated summation.
+ */
+ private void sumWithCompensation(double value) {
+ double tmp = value - sumCompensation;
+ double velvel = sum + tmp; // Little wolf of rounding error
+ sumCompensation = (velvel - sum) - tmp;
+ sum = velvel;
+ }
+
+ /**
+ * Return the count of values recorded.
+ *
+ * @return the count of values
+ */
+ public final long getCount() {
+ return count;
+ }
+
+ /**
+ * Returns the sum of values recorded, or zero if no values have been
+ * recorded.
+ *
+ * If any recorded value is a NaN or the sum is at any point a NaN
+ * then the sum will be NaN.
+ *
+ * <p> The value of a floating-point sum is a function both of the
+ * input values as well as the order of addition operations. The
+ * order of addition operations of this method is intentionally
+ * not defined to allow for implementation flexibility to improve
+ * the speed and accuracy of the computed result.
+ *
+ * In particular, this method may be implemented using compensated
+ * summation or other technique to reduce the error bound in the
+ * numerical sum compared to a simple summation of {@code double}
+ * values.
+ *
+ * @apiNote Values sorted by increasing absolute magnitude tend to yield
+ * more accurate results.
+ *
+ * @return the sum of values, or zero if none
+ */
+ public final double getSum() {
+ // Better error bounds to add both terms as the final sum
+ double tmp = sum + sumCompensation;
+ if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
+ // If the compensated sum is spuriously NaN from
+ // accumulating one or more same-signed infinite values,
+ // return the correctly-signed infinity stored in
+ // simpleSum.
+ return simpleSum;
+ else
+ return tmp;
+ }
+
+ /**
+ * Returns the minimum recorded value, {@code Double.NaN} if any recorded
+ * value was NaN or {@code Double.POSITIVE_INFINITY} if no values were
+ * recorded. Unlike the numerical comparison operators, this method
+ * considers negative zero to be strictly smaller than positive zero.
+ *
+ * @return the minimum recorded value, {@code Double.NaN} if any recorded
+ * value was NaN or {@code Double.POSITIVE_INFINITY} if no values were
+ * recorded
+ */
+ public final double getMin() {
+ return min;
+ }
+
+ /**
+ * Returns the maximum recorded value, {@code Double.NaN} if any recorded
+ * value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were
+ * recorded. Unlike the numerical comparison operators, this method
+ * considers negative zero to be strictly smaller than positive zero.
+ *
+ * @return the maximum recorded value, {@code Double.NaN} if any recorded
+ * value was NaN or {@code Double.NEGATIVE_INFINITY} if no values were
+ * recorded
+ */
+ public final double getMax() {
+ return max;
+ }
+
+ /**
+ * Returns the arithmetic mean of values recorded, or zero if no
+ * values have been recorded.
+ *
+ * If any recorded value is a NaN or the sum is at any point a NaN
+ * then the average will be code NaN.
+ *
+ * <p>The average returned can vary depending upon the order in
+ * which values are recorded.
+ *
+ * This method may be implemented using compensated summation or
+ * other technique to reduce the error bound in the {@link #getSum
+ * numerical sum} used to compute the average.
+ *
+ * @apiNote Values sorted by increasing absolute magnitude tend to yield
+ * more accurate results.
+ *
+ * @return the arithmetic mean of values, or zero if none
+ */
+ public final double getAverage() {
+ return getCount() > 0 ? getSum() / getCount() : 0.0d;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * Returns a non-empty string representation of this object suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ */
+ @Override
+ public String toString() {
+ return String.format(
+ "%s{count=%d, sum=%f, min=%f, average=%f, max=%f}",
+ this.getClass().getSimpleName(),
+ getCount(),
+ getSum(),
+ getMin(),
+ getAverage(),
+ getMax());
+ }
+}
diff --git a/ojluni/src/main/java/java/util/IntSummaryStatistics.java b/ojluni/src/main/java/java/util/IntSummaryStatistics.java
new file mode 100644
index 0000000..d16f224
--- /dev/null
+++ b/ojluni/src/main/java/java/util/IntSummaryStatistics.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright (c) 2012, 2013, 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;
+
+/**
+ * A state object for collecting statistics such as count, min, max, sum, and
+ * average.
+ *
+ * <p>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:
+ * <pre> {@code
+ * IntSummaryStatistics stats = intStream.collect(IntSummaryStatistics::new,
+ * IntSummaryStatistics::accept,
+ * IntSummaryStatistics::combine);
+ * }</pre>
+ *
+ * <p>{@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:
+ *
+ * <pre> {@code
+ * IntSummaryStatistics stats = people.stream()
+ * .collect(Collectors.summarizingInt(Person::getDependents));
+ *}</pre>
+ *
+ * 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.toIntStatistics()} 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.
+ *
+ * <p>This implementation does not check for overflow of 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;
+
+ /**
+ * Construct an empty instance with zero count, zero sum,
+ * {@code Integer.MAX_VALUE} min, {@code Integer.MIN_VALUE} max and zero
+ * average.
+ */
+ public IntSummaryStatistics() { }
+
+ /**
+ * Records a new value into the summary information
+ *
+ * @param value the input value
+ */
+ @Override
+ public void accept(int value) {
+ ++count;
+ sum += value;
+ min = Math.min(min, value);
+ max = Math.max(max, value);
+ }
+
+ /**
+ * Combines the state of another {@code IntSummaryStatistics} into this one.
+ *
+ * @param other another {@code IntSummaryStatistics}
+ * @throws NullPointerException if {@code other} is null
+ */
+ public void combine(IntSummaryStatistics other) {
+ count += other.count;
+ sum += other.sum;
+ min = Math.min(min, other.min);
+ max = Math.max(max, other.max);
+ }
+
+ /**
+ * Returns the count of values recorded.
+ *
+ * @return the count of values
+ */
+ public final long getCount() {
+ return count;
+ }
+
+ /**
+ * Returns the sum of values recorded, or zero if no values have been
+ * recorded.
+ *
+ * @return the sum of values, or zero if none
+ */
+ public final long getSum() {
+ return sum;
+ }
+
+ /**
+ * Returns the minimum value recorded, or {@code Integer.MAX_VALUE} if no
+ * values have been recorded.
+ *
+ * @return the minimum value, or {@code Integer.MAX_VALUE} if none
+ */
+ public final int getMin() {
+ return min;
+ }
+
+ /**
+ * Returns the maximum value recorded, or {@code Integer.MIN_VALUE} if no
+ * values have been recorded.
+ *
+ * @return the maximum value, or {@code Integer.MIN_VALUE} if none
+ */
+ public final int getMax() {
+ return max;
+ }
+
+ /**
+ * Returns the arithmetic mean of values recorded, or zero if no values have been
+ * recorded.
+ *
+ * @return the arithmetic mean of values, or zero if none
+ */
+ public final double getAverage() {
+ return getCount() > 0 ? (double) getSum() / getCount() : 0.0d;
+ }
+
+ @Override
+ /**
+ * {@inheritDoc}
+ *
+ * Returns a non-empty string representation of this object suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ */
+ public String toString() {
+ return String.format(
+ "%s{count=%d, sum=%d, min=%d, average=%f, max=%d}",
+ this.getClass().getSimpleName(),
+ getCount(),
+ getSum(),
+ getMin(),
+ getAverage(),
+ getMax());
+ }
+}
diff --git a/ojluni/src/main/java/java/util/LongSummaryStatistics.java b/ojluni/src/main/java/java/util/LongSummaryStatistics.java
new file mode 100644
index 0000000..2ba3959
--- /dev/null
+++ b/ojluni/src/main/java/java/util/LongSummaryStatistics.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2012, 2013, 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.function.LongConsumer;
+
+/**
+ * A state object for collecting statistics such as count, min, max, sum, and
+ * average.
+ *
+ * <p>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 longs with:
+ * <pre> {@code
+ * LongSummaryStatistics stats = longStream.collect(LongSummaryStatistics::new,
+ * LongSummaryStatistics::accept,
+ * LongSummaryStatistics::combine);
+ * }</pre>
+ *
+ * <p>{@code LongSummaryStatistics} can be used as a
+ * {@linkplain java.util.stream.Stream#collect(Collector)} reduction}
+ * target for a {@linkplain java.util.stream.Stream stream}. For example:
+ *
+ * <pre> {@code
+ * LongSummaryStatistics stats = people.stream()
+ * .collect(Collectors.summarizingLong(Person::getAge));
+ *}</pre>
+ *
+ * This computes, in a single pass, the count of people, as well as the minimum,
+ * maximum, sum, and average of their ages.
+ *
+ * @implNote This implementation is not thread safe. However, it is safe to use
+ * {@link java.util.stream.Collectors#summarizingLong(java.util.function.ToLongFunction)
+ * Collectors.toLongStatistics()} 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.
+ *
+ * <p>This implementation does not check for overflow of the sum.
+ * @since 1.8
+ */
+public class LongSummaryStatistics implements LongConsumer, IntConsumer {
+ private long count;
+ private long sum;
+ private long min = Long.MAX_VALUE;
+ private long max = Long.MIN_VALUE;
+
+ /**
+ * Construct an empty instance with zero count, zero sum,
+ * {@code Long.MAX_VALUE} min, {@code Long.MIN_VALUE} max and zero
+ * average.
+ */
+ public LongSummaryStatistics() { }
+
+ /**
+ * Records a new {@code int} value into the summary information.
+ *
+ * @param value the input value
+ */
+ @Override
+ public void accept(int value) {
+ accept((long) value);
+ }
+
+ /**
+ * Records a new {@code long} value into the summary information.
+ *
+ * @param value the input value
+ */
+ @Override
+ public void accept(long value) {
+ ++count;
+ sum += value;
+ min = Math.min(min, value);
+ max = Math.max(max, value);
+ }
+
+ /**
+ * Combines the state of another {@code LongSummaryStatistics} into this
+ * one.
+ *
+ * @param other another {@code LongSummaryStatistics}
+ * @throws NullPointerException if {@code other} is null
+ */
+ public void combine(LongSummaryStatistics other) {
+ count += other.count;
+ sum += other.sum;
+ min = Math.min(min, other.min);
+ max = Math.max(max, other.max);
+ }
+
+ /**
+ * Returns the count of values recorded.
+ *
+ * @return the count of values
+ */
+ public final long getCount() {
+ return count;
+ }
+
+ /**
+ * Returns the sum of values recorded, or zero if no values have been
+ * recorded.
+ *
+ * @return the sum of values, or zero if none
+ */
+ public final long getSum() {
+ return sum;
+ }
+
+ /**
+ * Returns the minimum value recorded, or {@code Long.MAX_VALUE} if no
+ * values have been recorded.
+ *
+ * @return the minimum value, or {@code Long.MAX_VALUE} if none
+ */
+ public final long getMin() {
+ return min;
+ }
+
+ /**
+ * Returns the maximum value recorded, or {@code Long.MIN_VALUE} if no
+ * values have been recorded
+ *
+ * @return the maximum value, or {@code Long.MIN_VALUE} if none
+ */
+ public final long getMax() {
+ return max;
+ }
+
+ /**
+ * Returns the arithmetic mean of values recorded, or zero if no values have been
+ * recorded.
+ *
+ * @return The arithmetic mean of values, or zero if none
+ */
+ public final double getAverage() {
+ return getCount() > 0 ? (double) getSum() / getCount() : 0.0d;
+ }
+
+ @Override
+ /**
+ * {@inheritDoc}
+ *
+ * Returns a non-empty string representation of this object suitable for
+ * debugging. The exact presentation format is unspecified and may vary
+ * between implementations and versions.
+ */
+ public String toString() {
+ return String.format(
+ "%s{count=%d, sum=%d, min=%d, average=%f, max=%d}",
+ this.getClass().getSimpleName(),
+ getCount(),
+ getSum(),
+ getMin(),
+ getAverage(),
+ getMax());
+ }
+}
diff --git a/openjdk_java_files.mk b/openjdk_java_files.mk
index fc69ca4..a88ab72 100644
--- a/openjdk_java_files.mk
+++ b/openjdk_java_files.mk
@@ -695,6 +695,9 @@
ojluni/src/main/java/java/util/OptionalDouble.java \
ojluni/src/main/java/java/util/PrimitiveIterator.java \
ojluni/src/main/java/java/util/Tripwire.java \
+ ojluni/src/main/java/java/util/DoubleSummaryStatistics.java \
+ ojluni/src/main/java/java/util/IntSummaryStatistics.java \
+ ojluni/src/main/java/java/util/LongSummaryStatistics.java \
ojluni/src/main/java/java/util/function/BiConsumer.java \
ojluni/src/main/java/java/util/function/BiFunction.java \
ojluni/src/main/java/java/util/function/BinaryOperator.java \