Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 09e4a10de -> d1f987b43
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/d1f987b4 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d1f987b4 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d1f987b4 Branch: refs/heads/GROOVY_2_5_X Commit: d1f987b43499e0f1a9312991b5ec79994ddde9e8 Parents: 09e4a10 Author: Daniel Sun <[email protected]> Authored: Tue Jan 23 13:41:01 2018 +0800 Committer: sunlan <[email protected]> Committed: Tue Jan 23 19:35:51 2018 +0800 ---------------------------------------------------------------------- .../vmplugin/v8/PluginDefaultGroovyMethods.java | 126 +++++++++++++++++++ .../v8/PluginDefaultGroovyMethodsTest.groovy | 81 ++++++++++++ 2 files changed, 207 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/d1f987b4/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 b26a381..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,7 +18,13 @@ */ 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; /** * Defines new Groovy methods which appear on normal JDK 8 @@ -42,4 +48,124 @@ public class PluginDefaultGroovyMethods { return optional.isPresent(); } + /** + * Accumulates the elements of stream into a new List. + * @param stream the Stream + * @param <T> + * @return a new {@code java.util.List} instance + */ + public static <T> List<T> toList(Stream<T> stream) { + return stream.collect(Collectors.toList()); + } + + /** + * Accumulates the elements of stream into a new Set. + * @param stream the Stream + * @param <T> + * @return a new {@code java.util.Set} instance + */ + 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/d1f987b4/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 6139654..2f48a10 100644 --- a/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy +++ b/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy @@ -42,4 +42,85 @@ class PluginDefaultGroovyMethodsTest extends StaticTypeCheckingTestCase { ''' } + void testStreamToList() { + assertScript ''' + def list = [1, 2, 3] + assert list == list.stream().toList() + ''' + } + + void testStreamToSet() { + assertScript ''' + def set = [1, 2, 3] as Set + 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() + ''' + } }
