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


The following commit(s) were added to refs/heads/master by this push:
     new 56d872b20d GROOVY-11255: DGM#flatten flattens nested structures 
including arrays but not primitive arrays
56d872b20d is described below

commit 56d872b20d20e76dc6d8303994e32164eb85b101
Author: Paul King <pa...@asert.com.au>
AuthorDate: Wed Dec 20 22:08:50 2023 +1000

    GROOVY-11255: DGM#flatten flattens nested structures including arrays but 
not primitive arrays
---
 .../codehaus/groovy/runtime/DefaultGroovyMethods.java  |  9 ++++++---
 src/test/groovy/ListTest.groovy                        | 18 +++++++++++++++---
 2 files changed, 21 insertions(+), 6 deletions(-)

diff --git 
a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 6999785f8b..555b54231b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -3469,9 +3469,11 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
                 for (BigDecimal i = self; i.compareTo(to1) >= 0; i = 
i.subtract(one)) {
                     closure.call(i);
                 }
-            } else
+            } else {
                 throw new GroovyRuntimeException("The argument (" + to +
-                        ") to downto() cannot be greater than the value (" + 
self + ") it's called on.");        } else if (to instanceof BigInteger) {
+                    ") to downto() cannot be greater than the value (" + self 
+ ") it's called on.");
+            }
+        } else if (to instanceof BigInteger) {
             BigDecimal to1 = new BigDecimal((BigInteger) to);
             if (self.compareTo(to1) >= 0) {
                 for (BigDecimal i = self; i.compareTo(to1) >= 0; i = 
i.subtract(one)) {
@@ -5796,7 +5798,8 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
         if (element instanceof Collection) {
             flatten((Collection<T>) element, addTo, flattenOptionals);
         } else if (element != null && element.getClass().isArray()) {
-            flatten(new ArrayIterable<>((T[]) element), addTo, 
flattenOptionals);
+            // handles non-primitive case too
+            
flatten(DefaultTypeTransformation.primitiveArrayToUnmodifiableList(element), 
addTo, flattenOptionals);
         } else {
             Object flattened = element;
             if (flattened instanceof Optional && flattenOptionals) {
diff --git a/src/test/groovy/ListTest.groovy b/src/test/groovy/ListTest.groovy
index 8e892a0af2..d20ef0773f 100644
--- a/src/test/groovy/ListTest.groovy
+++ b/src/test/groovy/ListTest.groovy
@@ -92,7 +92,7 @@ class ListTest extends GroovyTestCase {
         l = [1, 2, 7]
         assert l.max() == 7
 
-        // GROOVY-1006        
+        // GROOVY-1006
         l = [1, 3.2, 4L, (short) 7]
         assert l.max() == (short) 7
     }
@@ -107,7 +107,7 @@ class ListTest extends GroovyTestCase {
         l = [1, 2, 7]
         assert l.min() == 1
 
-        // GROOVY-1006        
+        // GROOVY-1006
         l = [(long) 1, 3.2, 4L, (short) 7]
         assert l.min() == (long) 1
     }
@@ -162,7 +162,7 @@ class ListTest extends GroovyTestCase {
         def list = [1, 1]
         assert list - [] == list
 
-        // GROOVY-1006    
+        // GROOVY-1006
         list = [1, 2, 2, 3, 1]
         assert list - [] == list
     }
@@ -240,6 +240,18 @@ class ListTest extends GroovyTestCase {
         assert flat == [1, 3, 20, 21, 22, 23, 24, 33]
     }
 
+    void testFlattenWithPrimitiveArray() {
+        char[] fooChars = 'foo'.chars
+        assert [fooChars].flatten() == ['f', 'o', 'o']
+    }
+
+    void testFlattenWithMultiDimensionalPrimitiveArray() {
+        int[][] identityMatrix = [[1, 0], [0, 1]]
+        assert identityMatrix.flatten() == [1, 0, 0, 1]
+        assert [identityMatrix].flatten() == [1, 0, 0, 1]
+        assert [[identityMatrix]].flatten() == [1, 0, 0, 1]
+    }
+
     void testListsAndRangesCompare() {
         def l = [1, 2, 3]
         def r = 1..3

Reply via email to