You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/05/17 07:10:19 UTC

[groovy] branch master updated (1f24e2e -> 1e9650d)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 1f24e2e  GROOVY-9553: bump picocli to 4.3.2
     new 1e31f16  GROOVY-9545: add extension method for stream->array using class literal (closes #1241)
     new 1e9650d  GROOVY-9545: add extension method for stream->array using class literal (minor refactor)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../vmplugin/v8/PluginDefaultGroovyMethods.java    | 43 +++++++++++++++++++++-
 1 file changed, 41 insertions(+), 2 deletions(-)


[groovy] 01/02: GROOVY-9545: add extension method for stream->array using class literal (closes #1241)

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 1e31f16b585359a75b376302f25d9d15aa5eab27
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri May 8 14:41:00 2020 -0500

    GROOVY-9545: add extension method for stream->array using class literal (closes #1241)
---
 .../vmplugin/v8/PluginDefaultGroovyMethods.java    | 34 ++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

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 76a7d7c..cd63f4f 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
@@ -29,6 +29,7 @@ import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.runtime.NullObject;
 import org.codehaus.groovy.runtime.RangeInfo;
 
+import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.util.Arrays;
 import java.util.Enumeration;
@@ -502,8 +503,37 @@ public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
+     * Returns an array containing the elements of the stream.
+     * <pre class="groovyTestCase">
+     * assert Arrays.equals([].stream().toArray(Object), new Object[0])
+     * assert Arrays.equals([].stream().toArray(String), new String[0])
+     * assert Arrays.equals(['x'].stream().toArray(Object), ['x'].toArray())
+     * assert Arrays.equals(['x'].stream().toArray(String), ['x'] as String[])
+     * assert Arrays.equals(['x'].stream().toArray(CharSequence), ['x'] as CharSequence[])
+     *
+     * groovy.test.GroovyAssert.shouldFail(ArrayStoreException) {
+     *   ['x'].stream().toArray(Thread)
+     * }
+     *
+     * // Stream#toArray(IntFunction) should still be used for closure literal:
+     * assert Arrays.equals(['x'].stream().toArray { n -> new String[n] }, ['x'] as String[])
+     *
+     * // Stream#toArray(IntFunction) should still be used for method reference:
+     * assert Arrays.equals(['x'].stream().toArray(String[]::new), ['x'] as String[])
+     * </pre>
+     *
+     * @param self the stream
+     * @param type the array element type
+     *
+     * @since x.y.z
+     */
+    public static <T> T[] toArray(final Stream<? extends T> self, final Class<T> type) {
+        return self.toArray(length -> (T[]) Array.newInstance(type, length));
+    }
+
+    /**
      * Accumulates the elements of stream into a new List.
-     * @param self the Stream
+     * @param self the stream
      * @param <T> the type of element
      * @return a new {@code java.util.List} instance
      *
@@ -515,7 +545,7 @@ public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport {
 
     /**
      * Accumulates the elements of stream into a new Set.
-     * @param self the Stream
+     * @param self the stream
      * @param <T> the type of element
      * @return a new {@code java.util.Set} instance
      *


[groovy] 02/02: GROOVY-9545: add extension method for stream->array using class literal (minor refactor)

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 1e9650df4360ba5a5ad5f2a9b8b73ca031e4cadc
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun May 17 17:09:21 2020 +1000

    GROOVY-9545: add extension method for stream->array using class literal (minor refactor)
---
 .../groovy/vmplugin/v8/PluginDefaultGroovyMethods.java    | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

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 cd63f4f..48e150e 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v8/PluginDefaultGroovyMethods.java
@@ -505,14 +505,22 @@ public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     /**
      * Returns an array containing the elements of the stream.
      * <pre class="groovyTestCase">
+     * import static groovy.test.GroovyAssert.shouldFail
+     *
      * assert Arrays.equals([].stream().toArray(Object), new Object[0])
      * assert Arrays.equals([].stream().toArray(String), new String[0])
+     * assert Arrays.equals([].stream().toArray(String[]), new String[0][])
      * assert Arrays.equals(['x'].stream().toArray(Object), ['x'].toArray())
      * assert Arrays.equals(['x'].stream().toArray(String), ['x'] as String[])
+     * assert Arrays.deepEquals([['x'] as String[]].stream().toArray(String[]), [['x'] as String[]] as String[][])
      * assert Arrays.equals(['x'].stream().toArray(CharSequence), ['x'] as CharSequence[])
      *
-     * groovy.test.GroovyAssert.shouldFail(ArrayStoreException) {
-     *   ['x'].stream().toArray(Thread)
+     * shouldFail(ArrayStoreException) {
+     *     ['x'].stream().toArray(Thread)
+     * }
+     *
+     * shouldFail(IllegalArgumentException) {
+     *     ['x'].stream().toArray((Class) null)
      * }
      *
      * // Stream#toArray(IntFunction) should still be used for closure literal:
@@ -525,9 +533,10 @@ public class PluginDefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @param self the stream
      * @param type the array element type
      *
-     * @since x.y.z
+     * @since 3.0.4
      */
     public static <T> T[] toArray(final Stream<? extends T> self, final Class<T> type) {
+        if (type == null) throw new IllegalArgumentException("type cannot be null");
         return self.toArray(length -> (T[]) Array.newInstance(type, length));
     }