groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch GROOVY_3_0_X updated: Minor refactoring: simplify code
Date Fri, 27 Dec 2019 20:58:17 GMT
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 4b6d548  Minor refactoring: simplify code
4b6d548 is described below

commit 4b6d548de7067e807461309ab19af9a9c1281ff3
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sat Dec 28 03:19:00 2019 +0800

    Minor refactoring: simplify code
    
    (cherry picked from commit b7029f690f7d8ddbb0e8af8508847094eeea2491)
---
 src/main/java/groovy/lang/MetaClassImpl.java     |  8 ++--
 src/main/java/org/apache/groovy/util/Arrays.java | 56 ++++++++++++++++++++++++
 src/main/java/org/apache/groovy/util/Maps.java   |  4 +-
 3 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 66b842c..ae99626 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -109,6 +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.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage;
 import static org.codehaus.groovy.reflection.ReflectionCache.isAssignableFrom;
 
@@ -184,11 +185,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         this.registry = GroovySystem.getMetaClassRegistry();
         metaMethodIndex = new MetaMethodIndex(theCachedClass);
         final MetaMethod[] metaMethods = theCachedClass.getNewMetaMethods();
-        if (add != null && !(add.length == 0)) {
-            List<MetaMethod> arr = new ArrayList<>();
-            arr.addAll(Arrays.asList(metaMethods));
-            arr.addAll(Arrays.asList(add));
-            myNewMetaMethods = arr.toArray(MetaMethod.EMPTY_ARRAY);
+        if (add != null && add.length != 0) {
+            myNewMetaMethods = merge(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
new file mode 100644
index 0000000..1912848
--- /dev/null
+++ b/src/main/java/org/apache/groovy/util/Arrays.java
@@ -0,0 +1,56 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.groovy.util;
+
+import java.lang.reflect.Array;
+
+/**
+ * Array utilities.
+ *
+ * @since 3.0.0
+ */
+public class Arrays {
+
+    /**
+     * Merge arrays
+     *
+     * @param arrays arrays to merge
+     * @param <T> array type
+     * @return the merged 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];
+
+        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);
+
+        for (int i = 0, n = arrays.length, curr = 0; i < n; i++) {
+            T[] array = arrays[i];
+            int length = array.length;
+            System.arraycopy(array, 0, resultArray, curr, length);
+            curr += length;
+        }
+
+        return resultArray;
+    }
+
+    private Arrays() {}
+}
diff --git a/src/main/java/org/apache/groovy/util/Maps.java b/src/main/java/org/apache/groovy/util/Maps.java
index b560a4f..e145fdd 100644
--- a/src/main/java/org/apache/groovy/util/Maps.java
+++ b/src/main/java/org/apache/groovy/util/Maps.java
@@ -26,7 +26,7 @@ import java.util.Map;
  * Map utilities.
  * @since 2.5.0
  */
-public abstract class Maps {
+public class Maps {
 
     public static <K, V> Map<K, V> of(K k1, V v1) {
         Map<K, V> map = new LinkedHashMap<K, V>();
@@ -5817,4 +5817,6 @@ public abstract class Maps {
 
         return Collections.<V, K>unmodifiableMap(resultMap);
     }
+
+    private Maps() {}
 }


Mime
View raw message