groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: Rename method name and tweak implementation
Date Sat, 28 Dec 2019 10:10:25 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan 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 43fa4c7  Rename method name and tweak implementation
43fa4c7 is described below

commit 43fa4c77013e5cdeb7c21ea5f9f7faa09febf5c1
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sat Dec 28 18:10:12 2019 +0800

    Rename method name and tweak implementation
---
 src/main/java/groovy/lang/MetaClassImpl.java     |  4 +--
 src/main/java/org/apache/groovy/util/Arrays.java | 11 ++++-----
 src/test/org/apache/groovy/util/ArraysTest.java  | 31 ++++++++++++++----------
 3 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index ae99626..1484ac3 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -109,7 +109,7 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
-import static org.apache.groovy.util.Arrays.merge;
+import static org.apache.groovy.util.Arrays.concat;
 import static org.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage;
 import static org.codehaus.groovy.reflection.ReflectionCache.isAssignableFrom;
 
@@ -186,7 +186,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         metaMethodIndex = new MetaMethodIndex(theCachedClass);
         final MetaMethod[] metaMethods = theCachedClass.getNewMetaMethods();
         if (add != null && add.length != 0) {
-            myNewMetaMethods = merge(metaMethods, add);
+            myNewMetaMethods = concat(metaMethods, add);
             additionalMetaMethods = metaMethods;
         } else {
             myNewMetaMethods = metaMethods;
diff --git a/src/main/java/org/apache/groovy/util/Arrays.java b/src/main/java/org/apache/groovy/util/Arrays.java
index 1912848..67b8abb 100644
--- a/src/main/java/org/apache/groovy/util/Arrays.java
+++ b/src/main/java/org/apache/groovy/util/Arrays.java
@@ -28,16 +28,15 @@ import java.lang.reflect.Array;
 public class Arrays {
 
     /**
-     * Merge arrays
+     * Concatenate arrays
      *
      * @param arrays arrays to merge
-     * @param <T> array type
-     * @return the merged array
+     * @param <T> array component type
+     * @return the concatenated array
      */
     @SuppressWarnings("unchecked")
-    public static <T> T[] merge(T[]... arrays) {
-        if (0 == arrays.length) throw new IllegalArgumentException("arrays should not be
empty");
-        if (1 == arrays.length) return arrays[0];
+    public static <T> T[] concat(T[]... arrays) {
+        if (0 == arrays.length) return null;
 
         int resultLength = java.util.Arrays.stream(arrays).map(e -> e.length).reduce(0,
Integer::sum);
         T[] resultArray = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(),
resultLength);
diff --git a/src/test/org/apache/groovy/util/ArraysTest.java b/src/test/org/apache/groovy/util/ArraysTest.java
index f46e336..37ca71b 100644
--- a/src/test/org/apache/groovy/util/ArraysTest.java
+++ b/src/test/org/apache/groovy/util/ArraysTest.java
@@ -23,36 +23,41 @@ import org.junit.Test;
 
 public class ArraysTest {
     @Test
-    public void testMerge0() {
-        try {
-            Integer[] result = Arrays.merge();
-            Assert.fail("should throw IllegalArgumentException");
-        } catch (IllegalArgumentException e) {
-            Assert.assertTrue(e.getMessage().contains("arrays should not be empty"));
-        }
+    public void testConcat0() {
+        Assert.assertNull(Arrays.concat());
     }
 
     @Test
-    public void testMerge1() {
+    public void testConcat1() {
         Integer[] a = new Integer[] {1, 2};
-        Integer[] result = Arrays.merge(a);
+        Integer[] result = Arrays.concat(a);
+        Assert.assertNotSame(a, result);
         Assert.assertArrayEquals(new Integer[] {1, 2}, result);
     }
 
     @Test
-    public void testMerge2() {
+    public void testConcat2() {
+        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 testConcat3() {
         Integer[] a = new Integer[] {1, 2};
         Integer[] b = new Integer[] {3, 4};
-        Integer[] result = Arrays.merge(a, b);
+        Integer[] result = Arrays.concat(a, b);
         Assert.assertArrayEquals(new Integer[] {1, 2, 3, 4}, result);
     }
 
     @Test
-    public void testMerge3() {
+    public void testConcat4() {
         Integer[] a = new Integer[] {1, 2};
         Integer[] b = new Integer[] {3, 4};
         Integer[] c = new Integer[] {5, 6, 7, 8, 9};
-        Integer[] result = Arrays.merge(a, b, c);
+        Integer[] result = Arrays.concat(a, b, c);
         Assert.assertArrayEquals(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8, 9}, result);
     }
 }


Mime
View raw message