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 2017/08/16 14:46:27 UTC
[2/2] groovy git commit: Add toList and toSet DGMs to
java.util.stream.Stream
Add toList and toSet DGMs to java.util.stream.Stream
(cherry picked from commit fca6e0f)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/3229ea43
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/3229ea43
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/3229ea43
Branch: refs/heads/GROOVY_2_6_X
Commit: 3229ea43af1cd2ba8d7910a4c6b43cce4f104332
Parents: ba8e6a1
Author: sunlan <su...@apache.org>
Authored: Wed Aug 16 22:36:48 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Wed Aug 16 22:46:15 2017 +0800
----------------------------------------------------------------------
build.gradle | 4 ++++
.../vmplugin/v8/PluginDefaultGroovyMethods.java | 23 ++++++++++++++++++++
.../v8/PluginDefaultGroovyMethodsTest.groovy | 13 +++++++++++
3 files changed, 40 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/3229ea43/build.gradle
----------------------------------------------------------------------
diff --git a/build.gradle b/build.gradle
index aef3ce2..5f46375 100644
--- a/build.gradle
+++ b/build.gradle
@@ -92,6 +92,10 @@ allprojects {
group = 'org.codehaus.groovy'
version = groovyVersion
repositories {
+ if (!InetAddress.getByName('www.google.com').isReachable(3000)) {
+ // Try to use Aliyun maven repository when building in China
+ maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
+ }
jcenter()
maven { url 'http://dl.bintray.com/melix/thirdparty-apache' } // openbeans
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/3229ea43/src/main/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java b/src/main/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
index b26a381..19136cf 100644
--- a/src/main/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
@@ -18,7 +18,11 @@
*/
package org.codehaus.groovy.vmplugin.v8;
+import java.util.List;
import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
/**
* Defines new Groovy methods which appear on normal JDK 8
@@ -42,4 +46,23 @@ 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());
+ }
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/3229ea43/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..1c82c6e 100644
--- a/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethodsTest.groovy
@@ -42,4 +42,17 @@ 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()
+ '''
+ }
}