changeset 6343:198fc90ec015

spliterators know their size and the size is updated after splitting. this patch corrects the size returned by the StreamAccessor (and also simplifies construction)
author mduigou
date Wed, 24 Oct 2012 12:19:23 -0700
parents 87a48add175b
children e40752a8afe3
files src/share/classes/java/util/streams/Streamable.java src/share/classes/java/util/streams/Streams.java
diffstat 2 files changed, 7 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/streams/Streamable.java	Wed Oct 24 12:11:07 2012 -0700
+++ b/src/share/classes/java/util/streams/Streamable.java	Wed Oct 24 12:19:23 2012 -0700
@@ -25,7 +25,7 @@
 package java.util.streams;
 
 /**
- * Provides elements as a stream.
+ * Elements are presented as a Stream.
  *
  * @param <S> Type of stream.
  *
--- a/src/share/classes/java/util/streams/Streams.java	Wed Oct 24 12:11:07 2012 -0700
+++ b/src/share/classes/java/util/streams/Streams.java	Wed Oct 24 12:19:23 2012 -0700
@@ -138,12 +138,8 @@
         return new ValuePipeline<>(new SpliteratorStreamAccessor<>(source));
     }
 
-    public static<T> Stream<T> parallel(Spliterator<T> source, int sizeOrUnknown) {
-        return new ValuePipeline<>(new SpliteratorStreamAccessor<>(source, sizeOrUnknown));
-    }
-
-    public static<T> Stream<T> parallel(Spliterator<T> source, int sizeOrUnknown, int flags) {
-        return new ValuePipeline<>(new SpliteratorStreamAccessor<>(source, sizeOrUnknown, flags));
+    public static<T> Stream<T> parallel(Spliterator<T> source, int flags) {
+        return new ValuePipeline<>(new SpliteratorStreamAccessor<>(source, flags));
     }
 
     // Infinite streams
@@ -397,21 +393,15 @@
 
     private static class SpliteratorStreamAccessor<T> implements StreamAccessor<T> {
         private final Spliterator<T> spliterator;
-        private final int sizeOrUnknown;
         private final int flags;
 
         public SpliteratorStreamAccessor(Spliterator<T> it) {
-            this(it, -1, 0);
+            this(it, 0);
         }
 
-        public SpliteratorStreamAccessor(Spliterator<T> it, int sizeOrUnknown) {
-            this(it, sizeOrUnknown, 0);
-        }
-
-        public SpliteratorStreamAccessor(Spliterator<T> spliterator, int sizeOrUnknown, int flags) {
+        public SpliteratorStreamAccessor(Spliterator<T> spliterator, int flags) {
             this.spliterator = spliterator;
-            this.sizeOrUnknown = sizeOrUnknown;
-            this.flags = (sizeOrUnknown >= 0 ? StreamOpFlags.IS_SIZED : 0) |
+            this.flags = (spliterator.getSizeIfKnown() >= 0 ? StreamOpFlags.IS_SIZED : 0) |
                          (flags & (StreamOpFlags.IS_DISTINCT | StreamOpFlags.IS_SORTED | StreamOpFlags.IS_ORDERED));
         }
 
@@ -427,7 +417,7 @@
 
         @Override
         public int getSizeIfKnown() {
-            return sizeOrUnknown;
+            return spliterator.getSizeIfKnown();
         }
 
         @Override