You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/06/26 21:59:48 UTC

[groovy] 01/01: GROOVY-10473: add `Stream#getAt(int)` and `#getAt(IntRange)` extensions

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

emilles pushed a commit to branch GROOVY-10473
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit b8c3c306058bffa20bbfbfb8971c3fb6852c4e9e
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Jun 26 16:59:27 2022 -0500

    GROOVY-10473: add `Stream#getAt(int)` and `#getAt(IntRange)` extensions
---
 .../groovy/runtime/StreamGroovyMethods.java        | 82 ++++++++++++++++++++++
 1 file changed, 82 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java
index 2dd1df2ac8..83636a71e0 100644
--- a/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/StreamGroovyMethods.java
@@ -18,6 +18,8 @@
  */
 package org.codehaus.groovy.runtime;
 
+import groovy.lang.IntRange;
+
 import java.lang.reflect.Array;
 import java.util.Arrays;
 import java.util.Collection;
@@ -45,6 +47,86 @@ public class StreamGroovyMethods {
     private StreamGroovyMethods() {
     }
 
+    /**
+     * Returns element at {@code index} or {@code null}.
+     * <p>
+     * This is a <a href="package-summary.html#StreamOps">short-circuiting
+     * terminal 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[-1] }
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[0] == 'foo'
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[1] == 'bar'
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[2] == 'baz'
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[3] === null
+     * </pre>
+     *
+     * @throws IllegalArgumentException if {@code index} is negative
+     *
+     * @since 5.0.0
+     */
+    public static <T> T getAt(final Stream<T> self, final int index) {
+        return self.skip(index).findFirst().orElse(null);
+    }
+
+    /**
+     * Returns element(s) in {@code range} or an empty list.
+     * <p>
+     * This is a <a href="package-summary.html#StreamOps">short-circuiting
+     * terminal 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[-1..0] }
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * shouldFail(IllegalArgumentException) { stream[0..-1] }
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[0..<1] == ['foo']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[1..<2] == ['bar']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[2..<3] == ['baz']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[3..<4] == []
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[0<..2] == ['bar','baz']
+     *
+     * stream = ['foo','bar','baz'].stream()
+     * assert stream[0..99] == ['foo','bar','baz']
+     * </pre>
+     *
+     * @throws IllegalArgumentException for negative index or reverse range
+     *
+     * @since 5.0.0
+     */
+    public static <T> List<T> getAt(final Stream<T> self, final IntRange range) {
+        if (range.isReverse()) throw new IllegalArgumentException("reverse range");
+        return self.skip(range.getFromInt()).limit(range.size()).collect(Collectors.toList());
+    }
+
+    //--------------------------------------------------------------------------
+
     /**
      * Returns a lazily concatenated stream whose elements are all the elements of this stream followed by all the elements of the {@link Collection} object.
      *