changeset 6414:16ebde8b1e17

Fix bug in Streams.stream() where ArraySpliterator was being wrapped incorrectly with size-ignorant spliterator
author briangoetz
date Thu, 15 Nov 2012 16:41:55 -0500
parents 3832536fbed9
children ab258565c0c9
files src/share/classes/java/util/streams/Streams.java src/share/classes/java/util/streams/ops/SortedOp.java test-ng/tests/org/openjdk/tests/java/util/streams/ops/SortedOpTest.java
diffstat 3 files changed, 30 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/Streams.java	Thu Nov 15 14:27:37 2012 -0500
+++ b/src/share/classes/java/util/streams/Streams.java	Thu Nov 15 16:41:55 2012 -0500
@@ -111,7 +111,7 @@
         }, flags);
     }
 
-    public static<U> Stream<U> stream(Spliterator.Sequential<U> spliterator, int flags) {
+    public static<U> Stream<U> stream(Spliterator<U> spliterator, int flags) {
         return new ReferencePipeline<>(spliterator, flags);
     }
 
@@ -133,7 +133,7 @@
 
     public static <T> Stream<T> stream(T[] source, int offset, int length) {
         // Note use of full-service Spliterator here -- harmless because PARALLEL flag is not set
-        return stream(new ArraySpliterator<>(source, offset, length),
+        return stream((Spliterator) new ArraySpliterator<>(source, offset, length),
                       StreamOpFlags.IS_SIZED | StreamOpFlags.IS_ORDERED);
     }
 
--- a/src/share/classes/java/util/streams/ops/SortedOp.java	Thu Nov 15 14:27:37 2012 -0500
+++ b/src/share/classes/java/util/streams/ops/SortedOp.java	Thu Nov 15 16:41:55 2012 -0500
@@ -76,35 +76,33 @@
         if (StreamOpFlags.SORTED.isKnown(flags)) {
             return sink;
         }
-        // Optimized version if we know the size
-        // @@@ Commented out pending resolution of why size=-1 in this case
-//        else if (StreamOpFlags.SIZED.isKnown(flags)) {
-//            return new Sink.ChainedValue<T>(sink) {
-//                T[] array;
-//                int offset;
-//
-//                PriorityQueue<T> pq;
-//                @Override
-//                public void begin(int size) {
-//                    array = (T[]) new Object[size];
-//                }
-//
-//                @Override
-//                public void end() {
-//                    Arrays.sort(array, comparator);
-//                    downstream.begin(array.length);
-//                    for (T t : array)
-//                        downstream.apply(t);
-//                    downstream.end();
-//                    array = null;
-//                }
-//
-//                @Override
-//                public void apply(T t) {
-//                    array[offset++] = t;
-//                }
-//            };
-//        }
+        else if (StreamOpFlags.SIZED.isKnown(flags)) {
+            // Optimized version if we know the size
+            return new Sink.ChainedValue<T>(sink) {
+                T[] array;
+                int offset;
+
+                @Override
+                public void begin(int size) {
+                    array = (T[]) new Object[size];
+                }
+
+                @Override
+                public void end() {
+                    Arrays.sort(array, comparator);
+                    downstream.begin(array.length);
+                    for (T t : array)
+                        downstream.apply(t);
+                    downstream.end();
+                    array = null;
+                }
+
+                @Override
+                public void apply(T t) {
+                    array[offset++] = t;
+                }
+            };
+        }
         else {
             return new Sink.ChainedValue<T>(sink) {
                 ArrayList<T> list;
--- a/test-ng/tests/org/openjdk/tests/java/util/streams/ops/SortedOpTest.java	Thu Nov 15 14:27:37 2012 -0500
+++ b/test-ng/tests/org/openjdk/tests/java/util/streams/ops/SortedOpTest.java	Thu Nov 15 16:41:55 2012 -0500
@@ -111,6 +111,7 @@
 
         Node<Integer> result = exerciseOps(data, op);
         assertSorted(result.iterator());
+        // @@@ Can make stronger assertions about content equality
         Assert.assertEquals(result.size(), data.size());
 
         result = exerciseOps(data, new SortedOp<>(cInteger.reverse()));