OpenJDK / lambda / lambda / jdk
changeset 7550:e4c9aab508b1
- OptionalDouble should use Double.compare for equality.
- Collections.LongStatistics/DoubleStatistics use optional values
for min/max and ensure initial internal min/max values are set correctly
for assignment when accepting the first input value.
author | psandoz |
---|---|
date | Fri, 01 Mar 2013 11:31:35 +0100 |
parents | 9ccdccc3c754 |
children | 4bf1ce19c7cd |
files | src/share/classes/java/util/OptionalDouble.java src/share/classes/java/util/stream/Collectors.java |
diffstat | 2 files changed, 36 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/OptionalDouble.java Thu Feb 28 18:35:14 2013 +0100 +++ b/src/share/classes/java/util/OptionalDouble.java Fri Mar 01 11:31:35 2013 +0100 @@ -216,7 +216,9 @@ } OptionalDouble other = (OptionalDouble) o; - return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent; + return (isPresent && other.isPresent) + ? Double.compare(value, other.value) == 0 + : isPresent == other.isPresent; } @Override
--- a/src/share/classes/java/util/stream/Collectors.java Thu Feb 28 18:35:14 2013 +0100 +++ b/src/share/classes/java/util/stream/Collectors.java Fri Mar 01 11:31:35 2013 +0100 @@ -36,6 +36,7 @@ import java.util.NoSuchElementException; import java.util.Objects; import java.util.OptionalDouble; +import java.util.OptionalLong; import java.util.Set; import java.util.StringJoiner; import java.util.function.BiConsumer; @@ -458,8 +459,8 @@ public static final class LongStatistics implements LongConsumer, IntConsumer { private long count; private long sum; - private long min; - private long max; + private long min = Long.MAX_VALUE; + private long max = Long.MIN_VALUE; @Override public void accept(int value) { @@ -489,24 +490,34 @@ return sum; } - public long getMin() { - return min; + public OptionalLong getMin() { + return count > 0 ? OptionalLong.of(min) : OptionalLong.empty(); } - public long getMax() { - return max; + public OptionalLong getMax() { + return count > 0 ? OptionalLong.of(max) : OptionalLong.empty(); } public OptionalDouble getMean() { return count > 0 ? OptionalDouble.of((double) sum / count) : OptionalDouble.empty(); } + + @Override + public String toString() { + return new StringJoiner(", ", this.getClass().getSimpleName() + "{", "}"). + add("count=" + getCount()). + add("sum=" + getSum()). + add("min=" + getMin()). + add("max=" + getMax()). + toString(); + } } public static final class DoubleStatistics implements DoubleConsumer { private long count; private double sum; - private double min; - private double max; + private double min = Double.POSITIVE_INFINITY; + private double max = Double.NEGATIVE_INFINITY; @Override public void accept(double value) { @@ -531,17 +542,27 @@ return sum; } - public double getMin() { - return min; + public OptionalDouble getMin() { + return count > 0 ? OptionalDouble.of(min) : OptionalDouble.empty(); } - public double getMax() { - return max; + public OptionalDouble getMax() { + return count > 0 ? OptionalDouble.of(max) : OptionalDouble.empty(); } public OptionalDouble getMean() { return count > 0 ? OptionalDouble.of(sum / count) : OptionalDouble.empty(); } + + @Override + public String toString() { + return new StringJoiner(", ", this.getClass().getSimpleName() + "{", "}"). + add("count=" + getCount()). + add("sum=" + getSum()). + add("min=" + getMin()). + add("max=" + getMax()). + toString(); + } } public static Collector.OfLong<LongStatistics> toLongStatistics() {