changeset 5850:8a0c6d378c2d it2-bootstrap

work on ToArray
author mduigou
date Tue, 14 Aug 2012 10:29:46 -0700
parents 5f2ae9c265f0
children 1dea2f94eed3
files src/share/classes/java/util/streams/SequentialPipeline.java src/share/classes/java/util/streams/ops/ToArrayOp.java test-ng/tests/org/openjdk/tests/java/util/LambdaTestHelpers.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/ToArrayOpTest.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/UniqOpTest.java
diffstat 5 files changed, 129 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/SequentialPipeline.java	Mon Aug 13 13:32:13 2012 -0700
+++ b/src/share/classes/java/util/streams/SequentialPipeline.java	Tue Aug 14 10:29:46 2012 -0700
@@ -110,11 +110,11 @@
             Sink<T> wrappedSink = stage.sink(sink);
             if (stage.isStateful()) {
                 StatefulSink<U, Void> statefulSink = (StatefulSink<U, Void>) wrappedSink;
+                // @@@ supply size if known
                 statefulSink.begin(-1);
                 upstream.forEach(wrappedSink);
                 statefulSink.end();
-            }
-            else {
+            } else {
                 upstream.forEach(wrappedSink);
             }
         } else {
@@ -137,9 +137,7 @@
 
     @Override
     public Object[] toArray() {
-        // @@@ NYI
-        // If stream flags support exact sizing, use a FixedStreamBuilder
-        throw new UnsupportedOperationException("nyi");
+        return pipeline(ToArrayOp.<U>singleton());
     }
 
     public<V> Stream<V> pipeline(StatelessOp<U, V> op) {
--- a/src/share/classes/java/util/streams/ops/ToArrayOp.java	Mon Aug 13 13:32:13 2012 -0700
+++ b/src/share/classes/java/util/streams/ops/ToArrayOp.java	Tue Aug 14 10:29:46 2012 -0700
@@ -24,18 +24,60 @@
  */
 package java.util.streams.ops;
 
+import com.sun.tools.classfile.ConstantPool;
+import java.util.Arrays;
 import java.util.streams.ParallelStreamable;
 import java.util.streams.StatefulSink;
 
 /**
  * ToArrayOp
- *
+ * <p/>
  * @author Brian Goetz
  */
 public class ToArrayOp<T> implements EagerOp<T, Object[]> {
+
+    private final static ToArrayOp<?> INSTANCE = new ToArrayOp<>();
+
+    public static <T> ToArrayOp<T> singleton() {
+        return (ToArrayOp<T>) INSTANCE;
+    }
+
     @Override
     public StatefulSink<T, Object[]> sink() {
-        throw new UnsupportedOperationException("nyi");
+        return new StatefulSink<T, Object[]>() {
+            private static final int DEFAULT_CHUNK = 16;
+            Object[] elements;
+            int count;
+
+            @Override
+            public void begin(int size) {
+                elements = new Object[size >= 0 ? size : DEFAULT_CHUNK];
+                count = 0;
+            }
+
+            @Override
+            public void accept(T t) {
+                if (count >= elements.length) {
+                    // !?! mduigou smarter expansion policy needed
+                    Arrays.copyOf(elements, elements.length + DEFAULT_CHUNK);
+                }
+
+                elements[count++] = t;
+            }
+
+            @Override
+            public Object[] end() {
+                Object[] result;
+                if (count == elements.length) {
+                    result = elements;
+                } else {
+                    result = Arrays.copyOf(elements, count);
+                }
+                elements = null;
+
+                return result;
+            }
+        };
     }
 //
 //    @Override
--- a/test-ng/tests/org/openjdk/tests/java/util/LambdaTestHelpers.java	Mon Aug 13 13:32:13 2012 -0700
+++ b/test-ng/tests/org/openjdk/tests/java/util/LambdaTestHelpers.java	Tue Aug 14 10:29:46 2012 -0700
@@ -27,6 +27,7 @@
 import java.util.*;
 import java.util.functions.*;
 import java.util.streams.ParallelStreamable;
+import java.util.streams.StatefulSink;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
@@ -61,8 +62,9 @@
     public static final FlatMapper<String, Character> flattenChars = new FlatMapper<String, Character>() {
         @Override
         public void flatMapInto(Sink<? super Character> sink, String element) {
-            for (int i=0; i<element.length(); i++)
+            for (int i=0; i<element.length(); i++) {
                 sink.accept(element.charAt(i));
+            }
         }
     };
 
@@ -90,15 +92,17 @@
 
     public static List<Double> asDoubles(List<Integer> integers) {
         ArrayList<Double> list = new ArrayList<>();
-        for (Integer i : integers)
+        for (Integer i : integers) {
             list.add((double) i);
+        }
         return list;
     }
 
     public static List<Long> asLongs(List<Integer> integers) {
         ArrayList<Long> list = new ArrayList<>();
-        for (Integer i : integers)
+        for (Integer i : integers) {
             list.add((long) i);
+        }
         return list;
     }
 
@@ -275,4 +279,21 @@
         }
         return iterables;
     }
+
+    public static <T> Sink<T> iteratorToSink(Iterator<? extends T> iterator, Sink<? super T> sink) {
+        while(iterator.hasNext()) {
+            sink.accept(iterator.next());
+        }
+
+        return (Sink<T>) sink;
+    }
+
+    public static <T,V> V iteratorToStatefulSink(Iterator<? extends T> iterator, StatefulSink<? super T, ? extends V> sink) {
+        sink.begin(-1);
+        while(iterator.hasNext()) {
+            sink.accept(iterator.next());
+        }
+
+        return sink.end();
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/ToArrayOpTest.java	Tue Aug 14 10:29:46 2012 -0700
@@ -0,0 +1,57 @@
+/*
+ * 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 org.openjdk.tests.java.util.streams.ops;
+
+import org.testng.annotations.Test;
+
+import java.util.streams.ops.FilterOp;
+import java.util.streams.ops.MapOp;
+import java.util.streams.ops.ToArrayOp;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
+import static org.openjdk.tests.java.util.LambdaTestHelpers.pEven;
+import static org.openjdk.tests.java.util.LambdaTestHelpers.pOdd;
+
+/**
+ * ToArrayOpTest
+ *
+ */
+public class ToArrayOpTest extends StreamOpTestCase {
+    public void testRawIterator() {
+        ToArrayOp<Integer> op = ToArrayOp.singleton();
+        assertCountSum((List<Integer>) (List) Arrays.asList(
+                iteratorToStatefulSink(countTo(0).iterator(), op.sink())), 0, 0);
+        assertCountSum((List<Integer>) (List) Arrays.asList((Integer[])
+                iteratorToStatefulSink(countTo(10).iterator(), op.sink())), 10, 55);
+    }
+
+    @Test(dataProvider = "opArrays")
+    public void testOps(Integer[] data) {
+        ToArrayOp<Integer> op = ToArrayOp.singleton();
+      //  assertConsistentOpBehavior(data, op, null);
+    }
+}
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/UniqOpTest.java	Mon Aug 13 13:32:13 2012 -0700
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/UniqOpTest.java	Tue Aug 14 10:29:46 2012 -0700
@@ -35,9 +35,8 @@
 import static org.openjdk.tests.java.util.LambdaTestHelpers.*;
 
 /**
- * SortedOpTest
+ * UniqOpTest
  *
- * @author Brian Goetz
  */
 public class UniqOpTest extends StreamOpTestCase {
     public void testRawIterator() {