changeset 5975:a62508cbfa2e it2-bootstrap

Rename StatefulSink to TerminalSink; back out some workarounds for compiler issues
author briangoetz
date Wed, 05 Sep 2012 15:19:02 -0400
parents c5b8758c8ae7
children 595508a2d66f
files src/share/classes/java/lang/AbstractStringBuilder.java src/share/classes/java/util/Collection.java src/share/classes/java/util/StringJoiner.java src/share/classes/java/util/streams/AbstractPipeline.java src/share/classes/java/util/streams/LinearPipeline.java src/share/classes/java/util/streams/StatefulSink.java src/share/classes/java/util/streams/StreamBuilders.java src/share/classes/java/util/streams/TerminalSink.java src/share/classes/java/util/streams/ops/CumulateOp.java src/share/classes/java/util/streams/ops/FoldOp.java src/share/classes/java/util/streams/ops/ForEachOp.java src/share/classes/java/util/streams/ops/GroupByOp.java src/share/classes/java/util/streams/ops/SeedlessFoldOp.java src/share/classes/java/util/streams/ops/ShortCircuitTerminalOp.java src/share/classes/java/util/streams/ops/TerminalOp.java src/share/classes/java/util/streams/ops/ToArrayOp.java src/share/classes/java/util/streams/ops/TreeUtils.java test-ng/tests/org/openjdk/tests/java/util/LambdaTestHelpers.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/TeeOpTest.java
diffstat 20 files changed, 90 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/AbstractStringBuilder.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/lang/AbstractStringBuilder.java	Wed Sep 05 15:19:02 2012 -0400
@@ -1457,6 +1457,7 @@
     public void addAll(Stream<? extends CharSequence> stream) {
         if (stream.isParallel())
             stream = stream.sequential();
+        // @@@ Replace with this::append when compiler supports it
         stream.forEach((CharSequence e)-> { append(e); });
     }
 }
--- a/src/share/classes/java/util/Collection.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/Collection.java	Wed Sep 05 15:19:02 2012 -0400
@@ -505,7 +505,7 @@
     void addAll(Stream<? extends E> stream) default {
         if (stream.isParallel())
             stream = stream.sequential();
-        // @@@ Would use this::add but compiler doens't support that yet.
+        // @@@ Replace with this::add when compiler supports it
         stream.forEach((E e) -> { add(e); });
     }
 }
--- a/src/share/classes/java/util/StringJoiner.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/StringJoiner.java	Wed Sep 05 15:19:02 2012 -0400
@@ -214,6 +214,7 @@
     public void addAll(Stream<? extends CharSequence> stream) {
         if (stream.isParallel())
             stream = stream.sequential();
+        // @@@ Replace with this::add when compiler supports it
         stream.forEach((CharSequence e) -> { add(e); });
     }
 
--- a/src/share/classes/java/util/streams/AbstractPipeline.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/AbstractPipeline.java	Wed Sep 05 15:19:02 2012 -0400
@@ -147,7 +147,7 @@
         }
         else {
             // Push traversal
-            StatefulSink<U, V> terminalSink = terminal.sink();
+            TerminalSink<U, V> terminalSink = terminal.sink();
             source.into(wrapSink(ops, terminalSink));
             return terminalSink.getAndClearState();
         }
--- a/src/share/classes/java/util/streams/LinearPipeline.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/LinearPipeline.java	Wed Sep 05 15:19:02 2012 -0400
@@ -158,14 +158,7 @@
 
     @Override
     public U reduce(final U seed, final BinaryOperator<U> op) {
-        // @@@ Temporary hack to work around compiler error
-        Factory<U> seedFactory = new Factory<U>() {
-            @Override
-            public U make() {
-                return seed;
-            }
-        };
-        return pipeline(new FoldOp<>(seedFactory, op, op));
+        return pipeline(new FoldOp<>(seed, op, op));
     }
 
     @Override
--- a/src/share/classes/java/util/streams/StatefulSink.java	Wed Sep 05 13:59:09 2012 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-/*
- * Copyright (c) 2012, 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.streams;
-
-/**
- * A KitchenSink which accumulates state as elements are accepted.
- *
- * @param <T> The type of elements to be accepted.
- * @param <R> The type of the terminal state.
- *
- * @author Brian Goetz
- */
-public interface StatefulSink<T, R> extends Sink<T, Object, Object> {
-    /**
-     * Retrieve, and clear, the current state of the sink.
-     */
-    R getAndClearState();
-
-    @Override
-    void begin(int size) default { }
-
-    @Override
-    void end() default { }
-}
--- a/src/share/classes/java/util/streams/StreamBuilders.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/StreamBuilders.java	Wed Sep 05 15:19:02 2012 -0400
@@ -207,5 +207,4 @@
             return StreamBuilders.equals(this, (StreamBuilder) obj);
         }
     }
-
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/util/streams/TerminalSink.java	Wed Sep 05 15:19:02 2012 -0400
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2012, 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.streams;
+
+/**
+ * A Sink which accumulates state as elements are accepted, and allows that
+ * state to be retrieved after the computation is finished
+ *
+ * @param <T> The type of elements to be accepted.
+ * @param <R> The type of the terminal state.
+ *
+ * @author Brian Goetz
+ */
+public interface TerminalSink<T, R> extends Sink<T, Object, Object> {
+    /**
+     * Retrieve, and clear, the current state of the sink.
+     */
+    R getAndClearState();
+
+    @Override
+    void begin(int size) default { }
+
+    @Override
+    void end() default { }
+}
--- a/src/share/classes/java/util/streams/ops/CumulateOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/CumulateOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -51,8 +51,8 @@
     }
 
     @Override
-    public StatefulSink<T, T> sink(final Sink sink) {
-        return new StatefulSink<T, T>() {
+    public TerminalSink<T, T> sink(final Sink sink) {
+        return new TerminalSink<T, T>() {
             private boolean first;
             private T state;
 
@@ -179,7 +179,7 @@
                     }
                     else {
                         leafData = StreamBuilders.make();
-                        StatefulSink<T, T> terminalSink = sink(leafData);
+                        TerminalSink<T, T> terminalSink = sink(leafData);
                         source.into(problem.helper.sink(terminalSink));
                         upward = terminalSink.getAndClearState();
                         // Special case -- if problem.depth == 0, just wrap the result and be done
--- a/src/share/classes/java/util/streams/ops/FoldOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/FoldOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -28,7 +28,7 @@
 import java.util.streams.Spliterator;
 import java.util.concurrent.RecursiveTask;
 import java.util.functions.*;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 
 /**
  * FoldOp
@@ -46,10 +46,9 @@
         this.combiner = combiner;
     }
 
-// @@@ Workaround for compiler bug; resurrect this when bug is fixed
-//    public FoldOp(U seed, Combiner<U, T, U> reducer, BinaryOperator<U> combiner) {
-//        this(()->seed, reducer, combiner);
-//    }
+    public FoldOp(U seed, Combiner<U, T, U> reducer, BinaryOperator<U> combiner) {
+        this(()->seed, reducer, combiner);
+    }
 
     @Override
     public U evaluate(Iterator<T> iterator) {
@@ -60,8 +59,8 @@
     }
 
     @Override
-    public StatefulSink<T, U> sink() {
-        return new StatefulSink<T, U>() {
+    public TerminalSink<T, U> sink() {
+        return new TerminalSink<T, U>() {
             U state;
 
             @Override
@@ -115,7 +114,7 @@
                 return op.combiner.operate(leftResult, rightResult);
             }
             else {
-                final StatefulSink<T, U> reduceStage = op.sink();
+                final TerminalSink<T, U> reduceStage = op.sink();
                 source.into(helper.sink(reduceStage));
                 return reduceStage.getAndClearState();
             }
--- a/src/share/classes/java/util/streams/ops/ForEachOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/ForEachOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -31,7 +31,7 @@
 import java.util.functions.BiBlock;
 import java.util.functions.Block;
 import java.util.streams.Sink;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 
 /**
  * ForEachOp
@@ -39,14 +39,14 @@
  * @author Brian Goetz
  */
 public class ForEachOp<T> implements TerminalOp<T,Void> {
-    private final StatefulSink<T,Void> sink;
+    private final TerminalSink<T,Void> sink;
 
-    protected ForEachOp(StatefulSink<T, Void> sink) {
+    protected ForEachOp(TerminalSink<T, Void> sink) {
         this.sink = Objects.requireNonNull(sink);
     }
 
     public static<T> ForEachOp<T> make(final Block<? super T> block) {
-        return new ForEachOp<>(new StatefulSink<T, Void>() {
+        return new ForEachOp<>(new TerminalSink<T, Void>() {
             @Override
             public void accept(T t) {
                 block.apply(t);
@@ -61,7 +61,7 @@
 
     public static<K,V> ForEachOp<Mapping<K,V>> make(final BiBlock<? super K, ? super V> block) {
         Objects.requireNonNull(block);
-        return new ForEachOp<>(new StatefulSink<Mapping<K,V>, Void>() {
+        return new ForEachOp<>(new TerminalSink<Mapping<K,V>, Void>() {
             @Override
             public void accept(Object k, Object v) {
                 block.apply((K) k, (V) v);
@@ -80,7 +80,7 @@
     }
 
     @Override
-    public StatefulSink<T, Void> sink() {
+    public TerminalSink<T, Void> sink() {
         return sink;
     }
 
--- a/src/share/classes/java/util/streams/ops/GroupByOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/GroupByOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -46,8 +46,8 @@
     }
 
     @Override
-    public StatefulSink<T, Map<K, Collection<T>>> sink() {
-        return new StatefulSink<T, Map<K, Collection<T>>>() {
+    public TerminalSink<T, Map<K, Collection<T>>> sink() {
+        return new TerminalSink<T, Map<K, Collection<T>>>() {
             private Map<K, StreamBuilder<T>> map;
 
             @Override
--- a/src/share/classes/java/util/streams/ops/SeedlessFoldOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/SeedlessFoldOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -27,7 +27,7 @@
 import java.util.Objects;
 import java.util.Optional;
 import java.util.functions.BinaryOperator;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 
 /**
  * SeedlessFoldOp
@@ -57,8 +57,8 @@
     }
 
     @Override
-    public StatefulSink<T, Optional<T>> sink() {
-        return new StatefulSink<T, Optional<T>>() {
+    public TerminalSink<T, Optional<T>> sink() {
+        return new TerminalSink<T, Optional<T>>() {
             private boolean first = true;
             private T state;
 
--- a/src/share/classes/java/util/streams/ops/ShortCircuitTerminalOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/ShortCircuitTerminalOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -46,7 +46,7 @@
     public U evaluate(Iterator<T> iterator);
 
     @Override
-    StatefulSink<T, U> sink() default { throw new UnsupportedOperationException(); }
+    TerminalSink<T, U> sink() default { throw new UnsupportedOperationException(); }
 
     @Override
     boolean isShortCircuit() default { return true; }
--- a/src/share/classes/java/util/streams/ops/TerminalOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/TerminalOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -25,7 +25,7 @@
 package java.util.streams.ops;
 
 import java.util.Iterator;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 
 /**
  * EagerOp
@@ -41,7 +41,7 @@
      * @return
      */
     public U evaluate(Iterator<T> iterator) default {
-        StatefulSink<T,U> sink = sink();
+        TerminalSink<T,U> sink = sink();
         sink.begin(-1);
         while (iterator.hasNext())
             sink.accept(iterator.next());
@@ -49,14 +49,14 @@
         return sink.getAndClearState();
     }
 
-    public StatefulSink<T, U> sink();
+    public TerminalSink<T, U> sink();
 
     public boolean isShortCircuit() default { return false; }
 
     @Override
     <V> U computeParallel(ParallelOpHelper<T, V> helper) default {
         // dumb default serial version
-        StatefulSink<T, U> toArraySink = sink();
+        TerminalSink<T, U> toArraySink = sink();
         helper.spliterator().into(helper.sink(toArraySink));
         return toArraySink.getAndClearState();
     }
--- a/src/share/classes/java/util/streams/ops/ToArrayOp.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/ToArrayOp.java	Wed Sep 05 15:19:02 2012 -0400
@@ -25,7 +25,7 @@
 package java.util.streams.ops;
 
 import java.util.Arrays;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 
 /**
  * ToArrayOp
@@ -44,8 +44,8 @@
 
 
     @Override
-    public StatefulSink<T, Object[]> sink() {
-        return new StatefulSink<T, Object[]>() {
+    public TerminalSink<T, Object[]> sink() {
+        return new TerminalSink<T, Object[]>() {
             private static final int DEFAULT_CHUNK = 16;
             Object[] elements;
             int count = 0;
--- a/src/share/classes/java/util/streams/ops/TreeUtils.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/ops/TreeUtils.java	Wed Sep 05 15:19:02 2012 -0400
@@ -84,7 +84,7 @@
 
     public static class CollectorOp<T> implements TerminalOp<T, TreeUtils.Node<T>> {
         @Override
-        public StatefulSink<T, TreeUtils.Node<T>> sink() {
+        public TerminalSink<T, Node<T>> sink() {
             throw new UnsupportedOperationException();
         }
 
--- a/test-ng/tests/org/openjdk/tests/java/util/LambdaTestHelpers.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/LambdaTestHelpers.java	Wed Sep 05 15:19:02 2012 -0400
@@ -26,7 +26,7 @@
 
 import java.util.*;
 import java.util.functions.*;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 import java.util.streams.Stream;
 
 import static org.testng.Assert.assertEquals;
@@ -248,7 +248,7 @@
         }
     }
 
-    public static <T,V> V iteratorToStatefulSink(Iterator<? extends T> iterator, StatefulSink<? super T, ? extends V> sink) {
+    public static <T,V> V iteratorToStatefulSink(Iterator<? extends T> iterator, TerminalSink<? super T, ? extends V> sink) {
         sink.begin(-1);
         while(iterator.hasNext()) {
             sink.accept(iterator.next());
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/StreamOpTestCase.java	Wed Sep 05 15:19:02 2012 -0400
@@ -151,7 +151,7 @@
         }
         else {
             // First pass -- create a sink and evaluate, with no size advice
-            StatefulSink<T, U> terminalSink = terminalOp.sink();
+            TerminalSink<T, U> terminalSink = terminalOp.sink();
             Sink<T, ?, ?> sink = sink(terminalSink, ops);
             sink.begin(-1);
             data.forEach(sink);
@@ -159,7 +159,7 @@
             answer = terminalSink.getAndClearState();
 
             // Create a sink and evaluate, with size advice
-            StatefulSink<T, U> terminalSink2 = terminalOp.sink();
+            TerminalSink<T, U> terminalSink2 = terminalOp.sink();
             Sink<T, ?, ?> sink2 = sink(terminalSink2, ops);
             sink2.begin(data.size());
             data.forEach(sink2);
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/TeeOpTest.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/TeeOpTest.java	Wed Sep 05 15:19:02 2012 -0400
@@ -26,7 +26,7 @@
 
 import java.util.Arrays;
 import java.util.Optional;
-import java.util.streams.StatefulSink;
+import java.util.streams.TerminalSink;
 import org.testng.annotations.Test;
 
 import java.util.streams.ops.TeeOp;
@@ -45,7 +45,7 @@
  */
 public class TeeOpTest extends StreamOpTestCase {
     public void testRawIterator() {
-        StatefulSink<Integer,Object[]> copy = ToArrayOp.<Integer>singleton().sink();
+        TerminalSink<Integer,Object[]> copy = ToArrayOp.<Integer>singleton().sink();
         copy.begin(-1);
         assertCountSum(TeeOp.iterator(countTo(0).iterator(), copy), 0, 0);
         copy.end();
@@ -63,7 +63,7 @@
     }
 
     public void testTee() {
-        StatefulSink<Integer,Object[]> copy = ToArrayOp.<Integer>singleton().sink();
+        TerminalSink<Integer,Object[]> copy = ToArrayOp.<Integer>singleton().sink();
         copy.begin(-1);
         assertCountSum(countTo(0).stream().tee(copy), 0, 0);
         copy.end();
@@ -82,7 +82,7 @@
 
     @Test(dataProvider = "opArrays", dataProviderClass = StreamTestDataProvider.class)
     public void testOps(String name, TestData<Integer> data) {
-        StatefulSink<Integer,Optional<Integer>> fold = new SeedlessFoldOp<>(rPlus).sink();
+        TerminalSink<Integer,Optional<Integer>> fold = new SeedlessFoldOp<>(rPlus).sink();
         fold.begin(-1);
         StreamResult<Integer> result = exerciseOps(data, new TeeOp<>(fold));
         assertContentsUnordered(result, data);