Mercurial > hg > openjdk > lambda > jdk
changeset 7525:f2d99a1cee33
Simplify TerminalSink and AccumulatingSink protocols; replace explicit clear-state calls with more aggressive nulling of intermediate containers
author | briangoetz |
---|---|
date | Fri, 22 Feb 2013 16:59:34 -0500 |
parents | 9b2d5bf5f4d9 |
children | 1ad34774128e |
files | src/share/classes/java/util/stream/DistinctOp.java src/share/classes/java/util/stream/FindOp.java src/share/classes/java/util/stream/ForEachOp.java src/share/classes/java/util/stream/OpUtils.java src/share/classes/java/util/stream/ReduceOp.java src/share/classes/java/util/stream/TerminalSink.java |
diffstat | 6 files changed, 32 insertions(+), 101 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/DistinctOp.java Fri Feb 22 13:20:01 2013 -0500 +++ b/src/share/classes/java/util/stream/DistinctOp.java Fri Feb 22 16:59:34 2013 -0500 @@ -165,15 +165,8 @@ } @Override - public void clearState() { - set = null; - } - - @Override - public Set<T> getAndClearState() { - Set<T> result = set; - set = null; - return result; + public Set<T> get() { + return set; } @Override @@ -205,20 +198,6 @@ } @Override - public void clearState() { - seenNull = false; - lastSeen = null; - super.clearState(); - } - - @Override - public Set<T> getAndClearState() { - seenNull = false; - lastSeen = null; - return super.getAndClearState(); - } - - @Override public void accept(T t) { if (t == null) { if (!seenNull) {
--- a/src/share/classes/java/util/stream/FindOp.java Fri Feb 22 13:20:01 2013 -0500 +++ b/src/share/classes/java/util/stream/FindOp.java Fri Feb 22 16:59:34 2013 -0500 @@ -104,7 +104,7 @@ /** Specialization of {@code FindSink} for reference streams */ static class OfRef<T> extends FindSink<T, Optional<T>> { @Override - public Optional<T> getAndClearState() { + public Optional<T> get() { return hasValue ? Optional.of(value) : null; } } @@ -118,7 +118,7 @@ } @Override - public OptionalInt getAndClearState() { + public OptionalInt get() { return hasValue ? OptionalInt.of(value) : null; } } @@ -132,7 +132,7 @@ } @Override - public OptionalLong getAndClearState() { + public OptionalLong get() { return hasValue ? OptionalLong.of(value) : null; } } @@ -146,7 +146,7 @@ } @Override - public OptionalDouble getAndClearState() { + public OptionalDouble get() { return hasValue ? OptionalDouble.of(value) : null; } } @@ -190,7 +190,7 @@ @Override public <S> O evaluateSequential(PipelineHelper<S, T> helper) { - O result = helper.into(sinkSupplier.get(), helper.sourceSpliterator()).getAndClearState(); + O result = helper.into(sinkSupplier.get(), helper.sourceSpliterator()).get(); return result != null ? result : emptyValue; } @@ -237,7 +237,7 @@ @Override protected O doLeaf() { - O result = helper.into(op.sinkSupplier.get(), spliterator).getAndClearState(); + O result = helper.into(op.sinkSupplier.get(), spliterator).get(); if (!op.mustFindFirst) { if (result != null) shortCircuit(result);
--- a/src/share/classes/java/util/stream/ForEachOp.java Fri Feb 22 13:20:01 2013 -0500 +++ b/src/share/classes/java/util/stream/ForEachOp.java Fri Feb 22 16:59:34 2013 -0500 @@ -52,7 +52,7 @@ /** Specialization of {@code TerminalSink} with void result */ protected interface VoidTerminalSink<T> extends TerminalSink<T, Void> { @Override - default public Void getAndClearState() { + default public Void get() { return null; } @@ -114,7 +114,7 @@ @Override public <S> Void evaluateSequential(PipelineHelper<S, T> helper) { - return helper.into(sink, helper.sourceSpliterator()).getAndClearState(); + return helper.into(sink, helper.sourceSpliterator()).get(); } @Override
--- a/src/share/classes/java/util/stream/OpUtils.java Fri Feb 22 13:20:01 2013 -0500 +++ b/src/share/classes/java/util/stream/OpUtils.java Fri Feb 22 16:59:34 2013 -0500 @@ -99,7 +99,7 @@ public static<P_IN, P_OUT, R, S extends AccumulatingSink<P_OUT, R, S>> R parallelReduce(PipelineHelper<P_IN, P_OUT> helper, Supplier<S> factory) { S sink = new ReduceTask<>(helper, factory).invoke(); - return sink.getAndClearState(); + return sink.get(); } /** @@ -112,7 +112,6 @@ */ public interface AccumulatingSink<T, R, K extends AccumulatingSink<T, R, K>> extends TerminalSink<T, R> { public void combine(K other); - public void clearState(); } @@ -202,7 +201,7 @@ for (; child != null; child = child.nextSibling) { S otherResult = child.getLocalResult(); result.combine(otherResult); - otherResult.clearState(); + child.setLocalResult(null); // GC otherResult } setLocalResult(result); }
--- a/src/share/classes/java/util/stream/ReduceOp.java Fri Feb 22 13:20:01 2013 -0500 +++ b/src/share/classes/java/util/stream/ReduceOp.java Fri Feb 22 16:59:34 2013 -0500 @@ -78,7 +78,7 @@ } public <S> R evaluateSequential(PipelineHelper<S, T> helper) { - return helper.into(sinkSupplier.get(), helper.sourceSpliterator()).getAndClearState(); + return helper.into(sinkSupplier.get(), helper.sourceSpliterator()).get(); } @Override @@ -94,13 +94,8 @@ private static abstract class Box<U> { protected U state; - public void clearState() { - state = null; - } - - public U getAndClearState() { - try { return state; } - finally { state = null; } + public U get() { + return state; } } @@ -117,14 +112,8 @@ state = null; } - public void clearState() { - empty = true; - state = null; - } - - public Optional<U> getAndClearState() { - try { return empty ? Optional.empty() : Optional.of(state); } - finally { clearState(); } + public Optional<U> get() { + return empty ? Optional.empty() : Optional.of(state); } } @@ -138,13 +127,8 @@ state = 0; } - public void clearState() { - state = 0; - } - - public Integer getAndClearState() { - try { return state; } - finally { state = 0; } + public Integer get() { + return state; } } @@ -160,14 +144,8 @@ state = 0; } - public void clearState() { - empty = true; - state = 0; - } - - public OptionalInt getAndClearState() { - try { return empty ? OptionalInt.empty() : OptionalInt.of(state); } - finally { state = 0; } + public OptionalInt get() { + return empty ? OptionalInt.empty() : OptionalInt.of(state); } } @@ -181,13 +159,8 @@ state = 0; } - public void clearState() { - state = 0; - } - - public Long getAndClearState() { - try { return state; } - finally { state = 0; } + public Long get() { + return state; } } @@ -203,14 +176,8 @@ state = 0; } - public void clearState() { - empty = true; - state = 0; - } - - public OptionalLong getAndClearState() { - try { return empty ? OptionalLong.empty() : OptionalLong.of(state); } - finally { state = 0; } + public OptionalLong get() { + return empty ? OptionalLong.empty() : OptionalLong.of(state); } } @@ -224,13 +191,8 @@ state = 0; } - public void clearState() { - state = 0; - } - - public Double getAndClearState() { - try { return state; } - finally { state = 0; } + public Double get() { + return state; } } @@ -246,14 +208,8 @@ state = 0; } - public void clearState() { - empty = true; - state = 0; - } - - public OptionalDouble getAndClearState() { - try { return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } - finally { state = 0; } + public OptionalDouble get() { + return empty ? OptionalDouble.empty() : OptionalDouble.of(state); } }
--- a/src/share/classes/java/util/stream/TerminalSink.java Fri Feb 22 13:20:01 2013 -0500 +++ b/src/share/classes/java/util/stream/TerminalSink.java Fri Feb 22 16:59:34 2013 -0500 @@ -24,6 +24,8 @@ */ package java.util.stream; +import java.util.function.Supplier; + /** * A Sink which accumulates state as elements are accepted, and allows a result * to be retrieved after the computation is finished. @@ -33,9 +35,4 @@ * * @since 1.8 */ -interface TerminalSink<T, R> extends Sink<T> { - /** - * Retrieve and clear the result - */ - R getAndClearState(); -} +interface TerminalSink<T, R> extends Sink<T>, Supplier<R> { }