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/28 14:27:43 UTC

[groovy] branch master updated: 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 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 7129194e78 GROOVY-10473: add `Stream` `getAt(int)` and `getAt(IntRange)` extensions
7129194e78 is described below

commit 7129194e78335c6540a2d021cf6aff3c7dc897f2
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        | 80 ++++++++++++++++++++++
 1 file changed, 80 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..0b004bffa1 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,84 @@ public class StreamGroovyMethods {
     private StreamGroovyMethods() {
     }
 
+    /**
+     * Returns element at {@code index} or {@code null}.
+     * <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 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="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/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.
      *