changeset 8357:427053023319

Update behavior of range() factories as per review feedback
author briangoetz
date Mon, 22 Apr 2013 21:13:06 -0400
parents ff1eb7b969bb
children dcafeb140b38
files src/share/classes/java/util/stream/DoubleStream.java src/share/classes/java/util/stream/IntStream.java src/share/classes/java/util/stream/LongStream.java src/share/classes/java/util/stream/Streams.java
diffstat 4 files changed, 71 insertions(+), 137 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/stream/DoubleStream.java	Mon Apr 22 16:23:30 2013 -0700
+++ b/src/share/classes/java/util/stream/DoubleStream.java	Mon Apr 22 21:13:06 2013 -0400
@@ -756,12 +756,12 @@
 
     /**
      * Returns a sequential {@code DoubleStream} from {@code startInclusive} (inclusive)
-     * to {@code endExclusive} (exclusive) by an incremental or decremental step of 1.0.
+     * to {@code endExclusive} (exclusive) by an incremental step of 1.0.
      *
      * @implSpec
      * The implementation behaves as if:
      * <pre>{@code
-     *     doubleRange(startInclusive, endExclusive, startInclusive <= endExclusive ? 1.0 : -1.0);
+     *     doubleRange(startInclusive, endExclusive, 1.0);
      * }</pre>
      *
      * @param startInclusive the (inclusive) initial value
@@ -770,19 +770,15 @@
      *         elements
      */
     public static DoubleStream range(double startInclusive, double endExclusive) {
-        return range(startInclusive, endExclusive, startInclusive <= endExclusive ? 1.0 : -1.0);
+        return range(startInclusive, endExclusive, 1.0);
     }
 
     /**
-     * Returns a sequential {@code DoubleStream} from {@code startInclusive} (inclusive)
-     * to {@code endExclusive} (exclusive) by {@code step}.
-     * <p>
-     * If {@code startInclusive} is less than {@code endExclusive} and {@code step} is greater
-     * than 0 then a stream of increasing values is returned. If {@code startInclusive}
-     * is greater than {@code endExclusive} and {@code step} is less than 0 then a stream
-     * of decreasing values is returned. If {@code startInclusive} is equal to
-     * {@code endExclusive} then an empty stream is returned.
-     * <p>
+     * Returns a sequential {@code DoubleStream} from {@code startInclusive}
+     * (inclusive) to {@code endExclusive} (exclusive) by {@code step}.
+     * If {@code startInclusive} is greater than {@code endExclusive}, an empty
+     * stream is returned.
+     *
      * An equivalent sequence of increasing values can be produced
      * sequentially using a {@code for} loop as follows:
      * <pre>{@code
@@ -799,41 +795,31 @@
      * @return A sequential {@code DoubleStream} for a range of {@code double}
      *         elements
      * @throws IllegalArgumentException
-     *         if {@code startInclusive} is greater than {@code endExclusive} and {@code step} is
-     *         greater than 0, or if {@code startInclusive} is less than {@code endExclusive} and
-     *         {@code step} is less than 0, or {@code step} is equal to 0,
-     *         or the size is greater than {@code Long.MAX_VALUE} or is
-     *         {@code NaN}.
+     *         if {@code step} is less than or equal to 0,
+     *         or the or the count of elements in the range would be greater
+     *         than {@code Long.MAX_VALUE} or is {@code NaN}.
      */
     public static DoubleStream range(double startInclusive, double endExclusive, double step) {
         // @@@ Need to check for ranges that may not produce distinct values
         //     such as when the step is very small
         //     Also clarify the size of the range which may produce more or less
         //     than expected
-        if (step > 0) {
-            // Decreasing range with an increasing step value
-            if (startInclusive > endExclusive) throw new IllegalArgumentException(
-                    String.format("Illegal range: startInclusive(%f) > endExclusive(%f), step(%f) > 0", startInclusive, endExclusive, step));
+        if (step <= 0 || Double.isNaN(step))
+            throw new IllegalArgumentException(String.format("Illegal step: %f", step));
+        else if (startInclusive > endExclusive)
+            return empty();
+        else {
+            double size = Math.ceil((endExclusive - startInclusive) / step);
+            if (Double.isNaN(size)) {
+                throw new IllegalArgumentException(
+                        String.format("Illegal range: %f size is NaN", size));
+            }
+            else if (size > Long.MAX_VALUE) {
+                throw new IllegalArgumentException(
+                        String.format("Illegal range: size %f > Long.MAX_VALUE", size));
+            }
+            else
+                return StreamSupport.doubleStream(new Streams.RangeDoubleSpliterator(startInclusive, endExclusive, step, 0, (long) size));
         }
-        else if (step < 0) {
-            // Increasing range with a decreasing step value
-            if (startInclusive < endExclusive) throw new IllegalArgumentException(
-                    String.format("Illegal range: startInclusive(%f) < endExclusive(%f), step(%f) < 0", startInclusive, endExclusive, step));
-        }
-        else if (step == 0) {
-            throw new IllegalArgumentException("Illegal range: step(0)");
-        }
-
-        double size = Math.ceil((endExclusive - startInclusive) / step);
-        if (Double.isNaN(size)) {
-            throw new IllegalArgumentException(
-                    String.format("Illegal range: %f size is NaN", size));
-        }
-        else if (size > Long.MAX_VALUE) {
-            throw new IllegalArgumentException(
-                    String.format("Illegal range: size %f > Long.MAX_VALUE", size));
-        }
-
-        return StreamSupport.doubleStream(new Streams.RangeDoubleSpliterator(startInclusive, endExclusive, step, 0, (long) size));
     }
 }
--- a/src/share/classes/java/util/stream/IntStream.java	Mon Apr 22 16:23:30 2013 -0700
+++ b/src/share/classes/java/util/stream/IntStream.java	Mon Apr 22 21:13:06 2013 -0400
@@ -758,12 +758,12 @@
 
     /**
      * Returns a sequential {@code IntStream} from {@code startInclusive} (inclusive)
-     * to {@code endExclusive} (exclusive) by an incremental or decremental step of 1.
+     * to {@code endExclusive} (exclusive) by an incremental step of 1.
      *
      * @implSpec
      * The implementation behaves as if:
      * <pre>{@code
-     *     intRange(startInclusive, endExclusive, startInclusive <= endExclusive ? 1 : -1);
+     *     intRange(startInclusive, endExclusive, 1);
      * }</pre>
      *
      * @param startInclusive the (inclusive) initial value
@@ -772,18 +772,14 @@
      *         elements
      */
     public static IntStream range(int startInclusive, int endExclusive) {
-        return range(startInclusive, endExclusive, startInclusive <= endExclusive ? 1 : -1);
+        return range(startInclusive, endExclusive, 1);
     }
 
     /**
      * Returns a sequential {@code IntStream} from {@code startInclusive} (inclusive)
-     * to {@code endExclusive} (exclusive) by {@code step}.
-     * <p>
-     * If {@code startInclusive} is less than {@code endExclusive} and {@code step} is greater
-     * than 0 then a stream of increasing values is returned. If {@code startInclusive}
-     * is greater than {@code endExclusive} and {@code step} is less than 0 then a stream
-     * of decreasing values is returned. If {@code startInclusive} is equal to
-     * {@code endExclusive} then an empty stream is returned.
+     * to {@code endExclusive} (exclusive) by a non-negative {@code step}.
+     * If {@code startInclusive} is greater than {@code endExclusive}, an empty
+     * stream is returned.
      * <p>
      * An equivalent sequence of increasing values can be produced
      * sequentially using a {@code for} loop as follows:
@@ -793,30 +789,18 @@
      *
      * @param startInclusive the (inclusive) initial value
      * @param endExclusive the exclusive upper bound
-     * @param step the difference between consecutive values
+     * @param step the non-negative difference between consecutive values
      * @return A sequential {@code IntStream} for a range of {@code int}
      *         elements
      * @throws IllegalArgumentException
-     *         if {@code startInclusive} is greater than {@code endExclusive} and {@code step} is
-     *         greater than 0, or if {@code startInclusive} is less than {@code endExclusive} and
-     *         {@code step} is less than 0, or if {@code step} is equal to 0.
+     *         if {@code step} is less than or equal to 0
      */
     public static IntStream range(int startInclusive, int endExclusive, int step) {
-        if (step > 0) {
-            // Decreasing range with an increasing step value
-            if (startInclusive > endExclusive) throw new IllegalArgumentException(
-                    String.format("Illegal range: startInclusive(%d) > endExclusive(%d), step(%d) > 0",
-                                  startInclusive, endExclusive, step));
-        }
-        else if (step < 0) {
-            // Increasing range with a decreasing step value
-            if (startInclusive < endExclusive) throw new IllegalArgumentException(
-                    String.format("Illegal range: startInclusive(%d) < endExclusive(%d), step(%d) < 0",
-                                  startInclusive, endExclusive, step));
-        }
-        else if (step == 0) {
-            throw new IllegalArgumentException("Illegal range: step(0)");
-        }
-        return StreamSupport.intStream(new Streams.RangeIntSpliterator(startInclusive, endExclusive, step));
+        if (step <= 0)
+            throw new IllegalArgumentException(String.format("Illegal step: %d", step));
+        else if (startInclusive > endExclusive)
+            return empty();
+        else
+            return StreamSupport.intStream(new Streams.RangeIntSpliterator(startInclusive, endExclusive, step));
     }
 }
--- a/src/share/classes/java/util/stream/LongStream.java	Mon Apr 22 16:23:30 2013 -0700
+++ b/src/share/classes/java/util/stream/LongStream.java	Mon Apr 22 21:13:06 2013 -0400
@@ -749,12 +749,12 @@
 
     /**
      * Returns a sequential {@code LongStream} from {@code startInclusive} (inclusive)
-     * to {@code endExclusive} (exclusive) by an incremental or decremental step of 1.
+     * to {@code endExclusive} (exclusive) by an incremental step of 1.
      *
      * @implSpec
      * The implementation behaves as if:
      * <pre>{@code
-     *     longRange(startInclusive, endExclusive, startInclusive <= endExclusive ? 1 : -1);
+     *     longRange(startInclusive, endExclusive, 1);
      * }</pre>
      *
      * @param startInclusive the (inclusive) initial value
@@ -763,20 +763,16 @@
      *         elements
      */
     public static LongStream range(long startInclusive, final long endExclusive) {
-        return range(startInclusive, endExclusive, startInclusive <= endExclusive ? 1 : -1);
+        return range(startInclusive, endExclusive, 1);
     }
 
     /**
-     * Returns a sequential {@code LongStream} from {@code startInclusive} (inclusive)
-     * to {@code endExclusive} (exclusive) by {@code step}.
-     * <p>
-     * If {@code startInclusive} is less than {@code endExclusive} and {@code step} is greater
-     * than 0 then a stream of increasing values is returned. If {@code startInclusive}
-     * is greater than {@code endExclusive} and {@code step} is less than 0 then a stream
-     * of decreasing values is returned. If {@code startInclusive} is equal to
-     * {@code endExclusive} then an empty stream is returned.
-     * <p>
-     * An equivalent sequence of increasing values can be produced
+     * Returns a sequential {@code LongStream} from {@code startInclusive}
+     * (inclusive) to {@code endExclusive} (exclusive) by {@code step}.
+     * If {@code startInclusive} is greater than {@code endExclusive}, an empty
+     * stream is returned.
+     *
+     * <p>An equivalent sequence of increasing values can be produced
      * sequentially using a {@code for} loop as follows:
      * <pre>{@code
      *     for (long i = startInclusive; i < endExclusive ; i += step) { ... }
@@ -788,24 +784,14 @@
      * @return A sequential {@code LongStream} for a range of {@code long}
      *         elements
      * @throws IllegalArgumentException
-     *         if {@code startInclusive} is greater than {@code endExclusive} and {@code step} is
-     *         greater than 0, or if {@code startInclusive} is less than {@code endExclusive} and
-     *         {@code step} is less than 0, or if {@code step} is equal to 0.
+     *         if {@code step} is less than or equal to 0
      */
     public static LongStream range(long startInclusive, final long endExclusive, final long step) {
-        if (step > 0) {
-            // Decreasing range with an increasing step value
-            if (startInclusive > endExclusive) throw new IllegalArgumentException(
-                    String.format("Illegal range: startInclusive(%d) > endExclusive(%d), step(%d) > 0", startInclusive, endExclusive, step));
-        }
-        else if (step < 0) {
-            // Increasing range with a decreasing step value
-            if (startInclusive < endExclusive) throw new IllegalArgumentException(
-                    String.format("Illegal range: startInclusive(%d) < endExclusive(%d), step(%d) < 0", startInclusive, endExclusive, step));
-        }
-        else if (step == 0) {
-            throw new IllegalArgumentException("Illegal range: step(0)");
-        }
-        return StreamSupport.longStream(new Streams.RangeLongSpliterator(startInclusive, endExclusive, step));
+        if (step <= 0)
+            throw new IllegalArgumentException(String.format("Illegal step: %d", step));
+        else if (startInclusive > endExclusive)
+            return empty();
+        else
+            return StreamSupport.longStream(new Streams.RangeLongSpliterator(startInclusive, endExclusive, step));
     }
 }
--- a/src/share/classes/java/util/stream/Streams.java	Mon Apr 22 16:23:30 2013 -0700
+++ b/src/share/classes/java/util/stream/Streams.java	Mon Apr 22 21:13:06 2013 -0400
@@ -229,7 +229,7 @@
 
         @Override
         public boolean tryAdvance(IntConsumer consumer) {
-            boolean hasNext = (step > 0) ? from < upTo : from > upTo;
+            boolean hasNext = from < upTo;
             if (hasNext) {
                 consumer.accept(from);
                 from += step;
@@ -241,13 +241,8 @@
         public void forEachRemaining(IntConsumer consumer) {
             int hUpTo = upTo;
             int hStep = step; // hoist accesses and checks from loop
-            int i = from;
-            if (step > 0)
-                for (; i < hUpTo; i += hStep)
-                    consumer.accept(i);
-            else
-                for (; i > hUpTo; i += hStep)
-                    consumer.accept(i);
+            for (int i = from; i < hUpTo; i += hStep)
+                consumer.accept(i);
             from = upTo;
         }
 
@@ -261,14 +256,12 @@
         public int characteristics() {
             return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
                    Spliterator.IMMUTABLE | Spliterator.NONNULL |
-                   Spliterator.DISTINCT | (step > 0 ? Spliterator.SORTED : 0);
+                   Spliterator.DISTINCT | Spliterator.SORTED;
         }
 
         @Override
         public Comparator<? super Integer> getComparator() {
-            if (step > 0)
-                return null;
-            throw new IllegalStateException();
+            return null;
         }
 
         @Override
@@ -283,10 +276,7 @@
             int bisection = (upTo - from) / 2;
             // If bisection > step then round down to nearest multiple of step
             // otherwise round up to step
-            if (step > 0)
-                return bisection > step ? bisection - bisection % step : step;
-            else
-                return bisection < step ? bisection - bisection % step : step;
+            return bisection > step ? bisection - bisection % step : step;
         }
     }
 
@@ -306,7 +296,7 @@
 
         @Override
         public boolean tryAdvance(LongConsumer consumer) {
-            boolean hasNext = (step > 0) ? from < upTo : from > upTo;
+            boolean hasNext = from < upTo;
             if (hasNext) {
                 consumer.accept(from);
                 from += step;
@@ -318,13 +308,8 @@
         public void forEachRemaining(LongConsumer consumer) {
             long hUpTo = upTo;
             long hStep = step; // hoist accesses and checks from loop
-            long i = from;
-            if (step > 0)
-                for (; i < hUpTo; i += hStep)
-                    consumer.accept(i);
-            else
-                for (; i > hUpTo; i += hStep)
-                    consumer.accept(i);
+            for (long i = from; i < hUpTo; i += hStep)
+                consumer.accept(i);
             from = upTo;
         }
 
@@ -338,14 +323,12 @@
         public int characteristics() {
             return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
                    Spliterator.IMMUTABLE | Spliterator.NONNULL |
-                   Spliterator.DISTINCT | (step > 0 ? Spliterator.SORTED : 0);
+                   Spliterator.DISTINCT | Spliterator.SORTED;
         }
 
         @Override
         public Comparator<? super Long> getComparator() {
-            if (step > 0)
-                return null;
-            throw new IllegalStateException();
+            return null;
         }
 
         @Override
@@ -360,10 +343,7 @@
             long bisection = (upTo - from) / 2;
             // If bisection > step then round down to nearest multiple of step
             // otherwise round up to step
-            if (step > 0)
-                return bisection > step ? bisection - bisection % step : step;
-            else
-                return bisection < step ? bisection - bisection % step : step;
+            return bisection > step ? bisection - bisection % step : step;
         }
     }
 
@@ -427,14 +407,12 @@
         public int characteristics() {
             return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
                    Spliterator.IMMUTABLE | Spliterator.NONNULL |
-                   Spliterator.DISTINCT | (step > 0 ? Spliterator.SORTED : 0);
+                   Spliterator.DISTINCT | Spliterator.SORTED;
         }
 
         @Override
         public Comparator<? super Double> getComparator() {
-            if (step > 0)
-                return null;
-            throw new IllegalStateException();
+            return null;
         }
 
         @Override