changeset 7599:988d94a4b83e

Add count() method on all stream types
author briangoetz
date Sun, 10 Mar 2013 18:52:10 -0400
parents a36b274075f5
children 6b22b3d5d3e9
files 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 src/share/classes/java/util/stream/ReferencePipeline.java src/share/classes/java/util/stream/Stream.java
diffstat 8 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/DoublePipeline.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/DoublePipeline.java	Sun Mar 10 18:52:10 2013 -0400
@@ -288,6 +288,11 @@
     }
 
     @Override
+    public long count() {
+        return map(e -> null).map(e -> 1).sum();
+    }
+
+    @Override
     public double reduce(double identity, DoubleBinaryOperator op) {
         return pipeline(ReduceOps.makeDouble(identity, op));
     }
--- a/src/share/classes/java/util/stream/DoubleStream.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/DoubleStream.java	Sun Mar 10 18:52:10 2013 -0400
@@ -105,5 +105,7 @@
 
     OptionalDouble average();
 
+    long count();
+
     double[] toArray();
 }
--- a/src/share/classes/java/util/stream/IntPipeline.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/IntPipeline.java	Sun Mar 10 18:52:10 2013 -0400
@@ -304,6 +304,11 @@
     }
 
     @Override
+    public long count() {
+        return longs().map(e -> 1L).sum();
+    }
+
+    @Override
     public OptionalDouble average() {
         return longs().average();
     }
--- a/src/share/classes/java/util/stream/IntStream.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/IntStream.java	Sun Mar 10 18:52:10 2013 -0400
@@ -163,6 +163,8 @@
 
     OptionalInt max();
 
+    long count();
+
     OptionalDouble average();
 
     int[] toArray();
--- a/src/share/classes/java/util/stream/LongPipeline.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/LongPipeline.java	Sun Mar 10 18:52:10 2013 -0400
@@ -300,6 +300,11 @@
     }
 
     @Override
+    public long count() {
+        return map(e -> 1L).sum();
+    }
+
+    @Override
     public long reduce(long identity, LongBinaryOperator op) {
         return pipeline(ReduceOps.makeLong(identity, op));
     }
--- a/src/share/classes/java/util/stream/LongStream.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/LongStream.java	Sun Mar 10 18:52:10 2013 -0400
@@ -107,5 +107,7 @@
 
     OptionalDouble average();
 
+    long count();
+
     long[] toArray();
 }
--- a/src/share/classes/java/util/stream/ReferencePipeline.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/ReferencePipeline.java	Sun Mar 10 18:52:10 2013 -0400
@@ -405,4 +405,9 @@
         return reduce(Comparators.lesserOf(comparator));
 
     }
+
+    @Override
+    public long count() {
+        return map(e -> 1L).sum();
+    }
 }
--- a/src/share/classes/java/util/stream/Stream.java	Sun Mar 10 18:08:43 2013 -0400
+++ b/src/share/classes/java/util/stream/Stream.java	Sun Mar 10 18:52:10 2013 -0400
@@ -513,6 +513,12 @@
     Optional<T> min(Comparator<? super T> comparator);
 
     /**
+     * Return the count of elements in this stream.
+     * @return The count of elements in this stream.
+     */
+    long count();
+
+    /**
      * Return whether any elements of this stream match the provided {@code Predicate}.  May not evaluate the
      * predicate on all elements if not necessary for determining the result.  This is
      * a <a href="package-summary.html#StreamOps">short-circuiting terminal operation</a>.