changeset 7650:c4d4cd1fc2ae

Make primitive stream specializations match form of collect() from Stream
author briangoetz
date Thu, 14 Mar 2013 15:18:29 -0400
parents bd7a6f339e43
children 6d89a67f6159 e24f6efedc1e
files src/share/classes/java/util/stream/Collectors.java src/share/classes/java/util/stream/DoublePipeline.java src/share/classes/java/util/stream/DoubleStream.java src/share/classes/java/util/stream/IntPipeline.java src/share/classes/java/util/stream/IntStream.java src/share/classes/java/util/stream/LongPipeline.java src/share/classes/java/util/stream/LongStream.java
diffstat 7 files changed, 58 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/Collectors.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/Collectors.java	Thu Mar 14 15:18:29 2013 -0400
@@ -308,7 +308,6 @@
                                    downstream.combiner(), downstream.isConcurrent());
     }
 
-
     public static<T, K> GroupingCollector<T, K> groupingBy(Function<? super T, ? extends K> classifier) {
         return groupingBy(classifier, HashMap::new);
     }
--- a/src/share/classes/java/util/stream/DoublePipeline.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/DoublePipeline.java	Thu Mar 14 15:18:29 2013 -0400
@@ -29,6 +29,8 @@
 import java.util.PrimitiveIterator;
 import java.util.Spliterator;
 import java.util.Spliterators;
+import java.util.function.BiConsumer;
+import java.util.function.BinaryOperator;
 import java.util.function.BooleanSupplier;
 import java.util.function.DoubleBinaryOperator;
 import java.util.function.DoubleConsumer;
@@ -37,6 +39,7 @@
 import java.util.function.DoubleUnaryOperator;
 import java.util.function.IntFunction;
 import java.util.function.ObjDoubleConsumer;
+import java.util.function.ObjIntConsumer;
 import java.util.function.Supplier;
 
 /**
@@ -303,6 +306,15 @@
     }
 
     @Override
+    public <R> R collect(Supplier<R> resultFactory, ObjDoubleConsumer<R> accumulator, BiConsumer<R, R> combiner) {
+        BinaryOperator<R> operator = (left, right) -> {
+            combiner.accept(left, right);
+            return left;
+        };
+        return collect(new Collectors.DoubleCollectorImpl<>(resultFactory, accumulator, operator, false));
+    }
+
+    @Override
     public <R> R collect(Collector.OfDouble<R> collector) {
         return pipeline(ReduceOps.makeDouble(collector));
     }
--- a/src/share/classes/java/util/stream/DoubleStream.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/DoubleStream.java	Thu Mar 14 15:18:29 2013 -0400
@@ -25,12 +25,16 @@
 package java.util.stream;
 
 import java.util.*;
+import java.util.function.BiConsumer;
 import java.util.function.BooleanSupplier;
 import java.util.function.DoubleBinaryOperator;
 import java.util.function.DoubleConsumer;
 import java.util.function.DoubleFunction;
 import java.util.function.DoublePredicate;
 import java.util.function.DoubleUnaryOperator;
+import java.util.function.ObjDoubleConsumer;
+import java.util.function.ObjIntConsumer;
+import java.util.function.Supplier;
 
 /**
  * A sequence of {@code double} elements supporting sequential and parallel bulk operations.
@@ -79,6 +83,10 @@
 
     OptionalDouble reduce(DoubleBinaryOperator op);
 
+    <R> R collect(Supplier<R> resultFactory,
+                  ObjDoubleConsumer<R> accumulator,
+                  BiConsumer<R, R> combiner);
+
     <R> R collect(Collector.OfDouble<R> collector);
 
     <R> R collectUnordered(Collector.OfDouble<R> collector);
--- a/src/share/classes/java/util/stream/IntPipeline.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/IntPipeline.java	Thu Mar 14 15:18:29 2013 -0400
@@ -30,6 +30,8 @@
 import java.util.PrimitiveIterator;
 import java.util.Spliterator;
 import java.util.Spliterators;
+import java.util.function.BiConsumer;
+import java.util.function.BinaryOperator;
 import java.util.function.BooleanSupplier;
 import java.util.function.IntBinaryOperator;
 import java.util.function.IntConsumer;
@@ -324,6 +326,15 @@
     }
 
     @Override
+    public <R> R collect(Supplier<R> resultFactory, ObjIntConsumer<R> accumulator, BiConsumer<R, R> combiner) {
+        BinaryOperator<R> operator = (left, right) -> {
+            combiner.accept(left, right);
+            return left;
+        };
+        return collect(new Collectors.IntCollectorImpl<>(resultFactory, accumulator, operator, false));
+    }
+
+    @Override
     public <R> R collect(Collector.OfInt<R> collector) {
         return pipeline(ReduceOps.makeInt(collector));
     }
--- a/src/share/classes/java/util/stream/IntStream.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/IntStream.java	Thu Mar 14 15:18:29 2013 -0400
@@ -25,12 +25,15 @@
 package java.util.stream;
 
 import java.util.*;
+import java.util.function.BiConsumer;
 import java.util.function.BooleanSupplier;
 import java.util.function.IntBinaryOperator;
 import java.util.function.IntConsumer;
 import java.util.function.IntFunction;
 import java.util.function.IntPredicate;
 import java.util.function.IntUnaryOperator;
+import java.util.function.ObjIntConsumer;
+import java.util.function.Supplier;
 
 /**
  * A sequence of int elements supporting sequential and parallel bulk operations.  Streams support lazy transformative
@@ -139,6 +142,10 @@
 
     OptionalInt reduce(IntBinaryOperator op);
 
+    <R> R collect(Supplier<R> resultFactory,
+                  ObjIntConsumer<R> accumulator,
+                  BiConsumer<R, R> combiner);
+
     <R> R collect(Collector.OfInt<R> collector);
 
     <R> R collectUnordered(Collector.OfInt<R> collector);
--- a/src/share/classes/java/util/stream/LongPipeline.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/LongPipeline.java	Thu Mar 14 15:18:29 2013 -0400
@@ -30,6 +30,8 @@
 import java.util.PrimitiveIterator;
 import java.util.Spliterator;
 import java.util.Spliterators;
+import java.util.function.BiConsumer;
+import java.util.function.BinaryOperator;
 import java.util.function.BooleanSupplier;
 import java.util.function.IntFunction;
 import java.util.function.LongBinaryOperator;
@@ -37,6 +39,7 @@
 import java.util.function.LongFunction;
 import java.util.function.LongPredicate;
 import java.util.function.LongUnaryOperator;
+import java.util.function.ObjIntConsumer;
 import java.util.function.ObjLongConsumer;
 import java.util.function.Supplier;
 
@@ -315,6 +318,15 @@
     }
 
     @Override
+    public <R> R collect(Supplier<R> resultFactory, ObjLongConsumer<R> accumulator, BiConsumer<R, R> combiner) {
+        BinaryOperator<R> operator = (left, right) -> {
+            combiner.accept(left, right);
+            return left;
+        };
+        return collect(new Collectors.LongCollectorImpl<>(resultFactory, accumulator, operator, false));
+    }
+
+    @Override
     public <R> R collect(Collector.OfLong<R> collector) {
         return pipeline(ReduceOps.makeLong(collector));
     }
--- a/src/share/classes/java/util/stream/LongStream.java	Thu Mar 14 12:06:03 2013 -0700
+++ b/src/share/classes/java/util/stream/LongStream.java	Thu Mar 14 15:18:29 2013 -0400
@@ -25,12 +25,16 @@
 package java.util.stream;
 
 import java.util.*;
+import java.util.function.BiConsumer;
 import java.util.function.BooleanSupplier;
 import java.util.function.LongBinaryOperator;
 import java.util.function.LongConsumer;
 import java.util.function.LongFunction;
 import java.util.function.LongPredicate;
 import java.util.function.LongUnaryOperator;
+import java.util.function.ObjIntConsumer;
+import java.util.function.ObjLongConsumer;
+import java.util.function.Supplier;
 
 /**
  * A sequence of {@code long} elements supporting sequential and parallel bulk operations.
@@ -81,6 +85,10 @@
 
     OptionalLong reduce(LongBinaryOperator op);
 
+    <R> R collect(Supplier<R> resultFactory,
+                  ObjLongConsumer<R> accumulator,
+                  BiConsumer<R, R> combiner);
+
     <R> R collect(Collector.OfLong<R> collector);
 
     <R> R collectUnordered(Collector.OfLong<R> collector);