changeset 5976:bc41f74b9bac it2-bootstrap

work on begin end
author mduigou
date Wed, 05 Sep 2012 12:42:15 -0700
parents c5b8758c8ae7
children 595508a2d66f
files src/share/classes/java/util/Arrays.java src/share/classes/java/util/streams/AbstractPipeline.java src/share/classes/java/util/streams/Sink.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 18 files changed, 118 insertions(+), 98 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Arrays.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/Arrays.java	Wed Sep 05 12:42:15 2012 -0700
@@ -3878,6 +3878,14 @@
         }
 
         @Override
+        public void begin(int size) {
+            index = 0;
+            if(size > length) {
+                System.out.println("Estimate greater than length. There might be blood.");
+            }
+        }
+
+        @Override
         public void accept(T t) {
             if (index > length) {
                 throw new IndexOutOfBoundsException(Integer.toString(index));
--- 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 12:42:15 2012 -0700
@@ -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/Sink.java	Wed Sep 05 13:59:09 2012 -0400
+++ b/src/share/classes/java/util/streams/Sink.java	Wed Sep 05 12:42:15 2012 -0700
@@ -30,7 +30,7 @@
 import java.util.functions.Block;
 
 /**
- * InternalSink
+ * Sink
  *
  * @author Brian Goetz
  */
@@ -43,20 +43,16 @@
      * Reset the sink state to receive a fresh data set.  This is used when a
      * Sink is being reused by multiple calculations.
      * @param size The approximate size of the data to be pushed downstream, if
-     * known or -1 if size is unknown.
+     * known or negative if size is unknown.
      * @@@ or something like that...
      */
     void begin(int size);
 
     /**
-     * Indicate that all element that is going to be pushed has been pushed.
-     * Chained sinks should dump their contents downstream and clear any stored
-     * state; terminal sinks should return their terminal state and clear any
-     * stored state.
-     *
-     * @return The terminal state of the sink.
+     * Indicate that all elements have been pushed. Chained sinks should dump
+     * their contents downstream and clear any stored state.
      */
-    void end();
+    void end() default {}
 
     @Override
     void apply(T t) default { accept(t); }
@@ -65,11 +61,6 @@
     void apply(K k, V v) default { accept(k, v); }
 
     public interface OfLinear<T> extends Sink<T, Object, Object> {
-        @Override
-        void begin(int size) default { }
-
-        @Override
-        void end() default { }
     }
 
     public static abstract class ChainedLinear<T> implements OfLinear<T> {
--- 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 12:42:15 2012 -0700
@@ -79,6 +79,14 @@
         }
 
         @Override
+        public void begin(int size) {
+            curSize = 0;
+            if(size > array.length) {
+                System.out.println("Estimate greater than length. There might be blood.");
+            }
+        }
+
+        @Override
         public void accept(T t) {
             if (curSize < array.length) {
                 array[curSize++] = t;
@@ -145,7 +153,7 @@
 
     private static class VariableStreamBuilder<T> extends AbstractCollection<T> implements StreamBuilder<T> {
         // Junky initial implementation that involves excessive copying.
-        private final List<T> list;
+        private final ArrayList<T> list;
 
         private VariableStreamBuilder(int initialSize) {
             list = new ArrayList<>(initialSize);
@@ -156,6 +164,14 @@
         }
 
         @Override
+        public void begin(int size) {
+            list.clear();
+            if(size >= 0) {
+                list.ensureCapacity(size);
+            }
+        }
+
+        @Override
         public void accept(T t) {
             list.add(t);
         }
@@ -206,6 +222,6 @@
                 return false;
             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 12:42:15 2012 -0700
@@ -0,0 +1,44 @@
+/*
+ * 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 TerminalSink<T, R> extends Sink<T, Object, Object> {
+
+    @Override
+    void end() default {}
+
+    /**
+     * Retrieve, and clear, the terminal state of the sink.
+     */
+    R getAndClearState();
+}
--- 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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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
@@ -60,8 +60,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 +115,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 12:42:15 2012 -0700
@@ -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,18 @@
  * @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 begin(int size) { }
+
             @Override
             public void accept(T t) {
                 block.apply(t);
@@ -61,7 +65,10 @@
 
     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 begin(int size) { }
+
             @Override
             public void accept(Object k, Object v) {
                 block.apply((K) k, (V) v);
@@ -80,7 +87,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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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, TreeUtils.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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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 12:42:15 2012 -0700
@@ -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);