groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Add DGM `stream` to array(closes #656)
Date Tue, 23 Jan 2018 11:28:42 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X aa7e37c24 -> 0502d362e


Add DGM `stream` to array(closes #656)

(cherry picked from commit 50f60cb)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/0502d362
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/0502d362
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/0502d362

Branch: refs/heads/GROOVY_2_6_X
Commit: 0502d362e50c5591bfd670de14bd4bb831c9d513
Parents: aa7e37c
Author: Daniel Sun <realbluesun@hotmail.com>
Authored: Tue Jan 23 13:41:01 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Tue Jan 23 19:28:37 2018 +0800

----------------------------------------------------------------------
 .../vmplugin/v8/PluginDefaultGroovyMethods.java | 103 +++++++++++++++++++
 .../v8/PluginDefaultGroovyMethodsTest.groovy    |  68 ++++++++++++
 2 files changed, 171 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/0502d362/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
b/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
index 5a51219..77b1780 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
@@ -18,10 +18,12 @@
  */
 package org.codehaus.groovy.vmplugin.v8;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 import java.util.stream.Stream;
 
 /**
@@ -65,4 +67,105 @@ public class PluginDefaultGroovyMethods {
     public static <T> Set<T> toSet(Stream<T> stream) {
         return stream.collect(Collectors.<T>toSet());
     }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param <T> The type of the array elements
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static <T> Stream<T> stream(T[] self) {
+        return Arrays.stream(self);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Integer> stream(int[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Long> stream(long[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Double> stream(double[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Character> stream(char[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Byte> stream(byte[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Short> stream(short[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Boolean> stream(boolean[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
+    /**
+     * Returns a sequential {@link Stream} with the specified array as its
+     * source.
+     *
+     * @param self The array, assumed to be unmodified during use
+     * @return a {@code Stream} for the array
+     */
+    public static Stream<Float> stream(float[] self) {
+        return IntStream.range(0, self.length).mapToObj(i -> self[i]);
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/0502d362/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
b/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
index 1c82c6e..2f48a10 100644
--- a/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
@@ -55,4 +55,72 @@ class PluginDefaultGroovyMethodsTest extends StaticTypeCheckingTestCase
{
             assert set.sort() == set.stream().toSet().sort()
         '''
     }
+
+    void testObjectArrayToStream() {
+        assertScript '''
+            def array = ["Hello", "World"] as Object[]
+            assert array == array.stream().toArray()
+        '''
+
+        assertScript '''
+            def array = ["Hello", "World"] as String[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testIntArrayToStream() {
+        assertScript '''
+            def array = [1, 2] as int[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testLongArrayToStream() {
+        assertScript '''
+            def array = [1, 2] as long[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testDoubleArrayToStream() {
+        assertScript '''
+            def array = [1, 2] as double[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testCharArrayToStream() {
+        assertScript '''
+            def array = [65, 66] as char[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testByteArrayToStream() {
+        assertScript '''
+            def array = [65, 66] as byte[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testShortArrayToStream() {
+        assertScript '''
+            def array = [65, 66] as short[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testBooleanArrayToStream() {
+        assertScript '''
+            def array = [true, false] as boolean[]
+            assert array == array.stream().toArray()
+        '''
+    }
+
+    void testFloatArrayToStream() {
+        assertScript '''
+            def array = [65, 66] as float[]
+            assert array == array.stream().toArray()
+        '''
+    }
 }


Mime
View raw message