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/08/29 06:54:04 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-9703: remove superflous System.arraycopy (closes #1354)

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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new ec3f08b  GROOVY-9703: remove superflous System.arraycopy (closes #1354)
ec3f08b is described below

commit ec3f08bee00040b75fc004bbeda5a5ee6169c925
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Aug 26 19:18:06 2020 -0500

    GROOVY-9703: remove superflous System.arraycopy (closes #1354)
---
 .../groovy/runtime/DefaultGroovyMethods.java       | 28 +++++++++++++++-------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 17c3d22..200905e 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -11886,20 +11886,32 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * Reverse the items in an array. If mutate is true, the original array is modified in place and returned.
      * Otherwise, a new array containing the reversed items is produced.
      *
+     * <pre class="groovyTestCase">
+     * def array = new Object[] {1,2,3}
+     * def yarra = array.reverse(true)
+     * assert array == 3..1
+     * assert yarra == 3..1
+     *
+     * yarra = array.reverse(false)
+     * assert array == 3..1
+     * assert yarra == 1..3
+     * </pre>
+     *
      * @param self   an array
-     * @param mutate true if the array itself should be reversed in place and returned, false if a new array should be created
+     * @param mutate {@code true} if the array itself should be reversed in place, {@code false} if a new array should be created
      * @return an array containing the reversed items
+     *
      * @since 1.8.1
      */
-    @SuppressWarnings("unchecked")
     public static <T> T[] reverse(T[] self, boolean mutate) {
-        if (!mutate) {
-            return (T[]) toList(new ReverseListIterator<T>(Arrays.asList(self))).toArray();
+        List<T> list = Arrays.asList(self);
+        if (mutate) {
+            Collections.reverse(list);
+            return self;
         }
-        List<T> items = Arrays.asList(self);
-        Collections.reverse(items);
-        System.arraycopy(items.toArray(), 0, self, 0, items.size());
-        return self;
+        @SuppressWarnings("unchecked")
+        T[] result = (T[]) toList(new ReverseListIterator<>(list)).toArray();
+        return result;
     }
 
     /**