You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/07/03 13:21:36 UTC

[groovy] branch master updated: GROOVY-10672: Provide a stream take method taking an IntRange

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 167e940f0e GROOVY-10672: Provide a stream take method taking an IntRange
167e940f0e is described below

commit 167e940f0ef4365866205399620eda69ab92a930
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Jun 29 16:48:30 2022 +1000

    GROOVY-10672: Provide a stream take method taking an IntRange
---
 .../groovy/runtime/StreamGroovyMethods.java        | 62 ++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java
index 0b004bffa1..ca1ff8db5b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java
@@ -18,6 +18,7 @@
  */
 package org.codehaus.groovy.runtime;
 
+import groovy.lang.EmptyRange;
 import groovy.lang.IntRange;
 
 import java.lang.reflect.Array;
@@ -123,6 +124,67 @@ public class StreamGroovyMethods {
         return self.skip(range.getFromInt()).limit(range.size()).collect(Collectors.toList());
     }
 
+    /**
+     * Returns a (possibly empty) stream.
+     * <p>
+     * This is a <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/package-summary.html#StreamOps">short-circuiting intermediate operation</a>.
+     *
+     * <pre class="groovyTestCase">
+     * import java.util.stream.Stream
+     * import static groovy.test.GroovyAssert.shouldFail
+     *
+     * Stream<String> stream = ['foo','bar','baz'].stream()
+     * shouldFail(IllegalArgumentException) { stream.from(-1..0) }
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * shouldFail(IllegalArgumentException) { stream.from(0..-1) }
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(0..<1).toList() == ['foo']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(1..<2).toList() == ['bar']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(2..<3).toList() == ['baz']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(3..<4).toList() == []
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(0<..2).toList() == ['bar','baz']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(0<..<2).toList() == ['bar']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream.from(0..99).toList() == ['foo','bar','baz']
+     * </pre>
+     *
+     * @throws IllegalArgumentException for negative index or reverse range
+     *
+     * @since 5.0.0
+     */
+    public static <T> Stream<T> from(final Stream<T> self, final IntRange range) {
+        if (range.isReverse()) throw new IllegalArgumentException("reverse range");
+        return self.skip(range.getFromInt()).limit(range.size());
+    }
+
+    /**
+     * Returns an empty stream.
+     * <p>
+     * <pre class="groovyTestCase">
+     * import java.util.stream.Stream
+     * Stream<String> stream = ['foo','bar','baz'].stream()
+     * assert !stream.from(1..<1).findAny().isPresent()
+     * </pre>
+     *
+     * @since 5.0.0
+     */
+    public static <T> Stream<T> from(final Stream<T> self, final EmptyRange range) {
+        return Stream.empty();
+    }
+
     //--------------------------------------------------------------------------
 
     /**