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 2019/12/28 10:34:30 UTC

[groovy] branch GROOVY_3_0_X updated: Improve the robustness of `Arrays.concat`

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new b1e3909  Improve the robustness of `Arrays.concat`
b1e3909 is described below

commit b1e39093ef5eb73900efdd8c29f55b3a231336ef
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Dec 28 18:32:33 2019 +0800

    Improve the robustness of `Arrays.concat`
    
    (cherry picked from commit d919b2023f0b1813c4c1acfe260f39363f268aed)
---
 src/main/java/org/apache/groovy/util/Arrays.java | 13 ++++++++++---
 src/test/org/apache/groovy/util/ArraysTest.java  | 14 ++++++++++++--
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/groovy/util/Arrays.java b/src/main/java/org/apache/groovy/util/Arrays.java
index 67b8abb..de3f85e 100644
--- a/src/main/java/org/apache/groovy/util/Arrays.java
+++ b/src/main/java/org/apache/groovy/util/Arrays.java
@@ -28,7 +28,7 @@ import java.lang.reflect.Array;
 public class Arrays {
 
     /**
-     * Concatenate arrays
+     * Concatenate arrays and ignore null array
      *
      * @param arrays arrays to merge
      * @param <T> array component type
@@ -36,13 +36,20 @@ public class Arrays {
      */
     @SuppressWarnings("unchecked")
     public static <T> T[] concat(T[]... arrays) {
-        if (0 == arrays.length) return null;
+        if (null == arrays || 0 == arrays.length) return null;
 
-        int resultLength = java.util.Arrays.stream(arrays).map(e -> e.length).reduce(0, Integer::sum);
+        int resultLength =
+                java.util.Arrays.stream(arrays)
+                        .filter(e -> null != e)
+                        .map(e -> e.length)
+                        .reduce(0, Integer::sum);
         T[] resultArray = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), resultLength);
 
         for (int i = 0, n = arrays.length, curr = 0; i < n; i++) {
             T[] array = arrays[i];
+
+            if (null == array) continue;
+
             int length = array.length;
             System.arraycopy(array, 0, resultArray, curr, length);
             curr += length;
diff --git a/src/test/org/apache/groovy/util/ArraysTest.java b/src/test/org/apache/groovy/util/ArraysTest.java
index 37ca71b..f6e15d3 100644
--- a/src/test/org/apache/groovy/util/ArraysTest.java
+++ b/src/test/org/apache/groovy/util/ArraysTest.java
@@ -25,6 +25,7 @@ public class ArraysTest {
     @Test
     public void testConcat0() {
         Assert.assertNull(Arrays.concat());
+        Assert.assertNull(Arrays.concat(null));
     }
 
     @Test
@@ -38,7 +39,7 @@ public class ArraysTest {
     @Test
     public void testConcat2() {
         Integer[] a = new Integer[] {1, 2};
-        Integer[] b = new Integer[0];
+        Integer[] b = null;
         Integer[] result = Arrays.concat(a, b);
         Assert.assertNotSame(a, result);
         Assert.assertArrayEquals(new Integer[] {1, 2}, result);
@@ -47,13 +48,22 @@ public class ArraysTest {
     @Test
     public void testConcat3() {
         Integer[] a = new Integer[] {1, 2};
+        Integer[] b = new Integer[0];
+        Integer[] result = Arrays.concat(a, b);
+        Assert.assertNotSame(a, result);
+        Assert.assertArrayEquals(new Integer[] {1, 2}, result);
+    }
+
+    @Test
+    public void testConcat4() {
+        Integer[] a = new Integer[] {1, 2};
         Integer[] b = new Integer[] {3, 4};
         Integer[] result = Arrays.concat(a, b);
         Assert.assertArrayEquals(new Integer[] {1, 2, 3, 4}, result);
     }
 
     @Test
-    public void testConcat4() {
+    public void testConcat5() {
         Integer[] a = new Integer[] {1, 2};
         Integer[] b = new Integer[] {3, 4};
         Integer[] c = new Integer[] {5, 6, 7, 8, 9};