You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/01/23 11:25:37 UTC
groovy git commit: Add DGM `stream` to array(closes #656)
Repository: groovy
Updated Branches:
refs/heads/master a5bc46ef9 -> 50f60cbf7
Add DGM `stream` to array(closes #656)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/50f60cbf
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/50f60cbf
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/50f60cbf
Branch: refs/heads/master
Commit: 50f60cbf76ccb7652e0fe53356953a0a19aa41ca
Parents: a5bc46e
Author: Daniel Sun <re...@hotmail.com>
Authored: Tue Jan 23 13:41:01 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Tue Jan 23 19:23:51 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/50f60cbf/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 19136cf..6c15626 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.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/50f60cbf/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()
+ '''
+ }
}