commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject [1/2] [lang] LANG-1178: ArrayUtils.removeAll(Object array, int... indices) should do the clone, not its callers (closes #116)
Date Thu, 12 May 2016 17:49:50 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master 28f7862ab -> f02261849


LANG-1178: ArrayUtils.removeAll(Object array, int... indices) should do the clone, not its
callers (closes #116)


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/5eae0a64
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/5eae0a64
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/5eae0a64

Branch: refs/heads/master
Commit: 5eae0a646469672d1cc5385bd7bd6aedc4aed19b
Parents: 28f7862
Author: Henri Yandell <hyandell@amazon.com>
Authored: Thu Nov 19 21:58:47 2015 +0000
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Thu May 12 19:43:31 2016 +0200

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ArrayUtils.java    | 37 +++++++++++---------
 1 file changed, 20 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/5eae0a64/src/main/java/org/apache/commons/lang3/ArrayUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index efd9c4a..6fe185d 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -6536,7 +6536,7 @@ public class ArrayUtils {
      */
     @SuppressWarnings("unchecked") // removeAll() always creates an array of the same type
as its input
     public static <T> T[] removeAll(final T[] array, final int... indices) {
-        return (T[]) removeAll((Object) array, clone(indices));
+        return (T[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -6628,7 +6628,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static byte[] removeAll(final byte[] array, final int... indices) {
-        return (byte[]) removeAll((Object) array, clone(indices));
+        return (byte[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -6717,7 +6717,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static short[] removeAll(final short[] array, final int... indices) {
-        return (short[]) removeAll((Object) array, clone(indices));
+        return (short[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -6806,7 +6806,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static int[] removeAll(final int[] array, final int... indices) {
-        return (int[]) removeAll((Object) array, clone(indices));
+        return (int[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -6895,7 +6895,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static char[] removeAll(final char[] array, final int... indices) {
-        return (char[]) removeAll((Object) array, clone(indices));
+        return (char[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -6984,7 +6984,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static long[] removeAll(final long[] array, final int... indices) {
-        return (long[]) removeAll((Object) array, clone(indices));
+        return (long[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -7073,7 +7073,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static float[] removeAll(final float[] array, final int... indices) {
-        return (float[]) removeAll((Object) array, clone(indices));
+        return (float[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -7162,7 +7162,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static double[] removeAll(final double[] array, final int... indices) {
-        return (double[]) removeAll((Object) array, clone(indices));
+        return (double[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -7247,7 +7247,7 @@ public class ArrayUtils {
      * @since 3.0.1
      */
     public static boolean[] removeAll(final boolean[] array, final int... indices) {
-        return (boolean[]) removeAll((Object) array, clone(indices));
+        return (boolean[]) removeAll((Object) array, indices);
     }
 
     /**
@@ -7309,7 +7309,7 @@ public class ArrayUtils {
     /**
      * Removes multiple array elements specified by index.
      * @param array source
-     * @param indices to remove, WILL BE SORTED--so only clones of user-owned arrays!
+     * @param indices to remove
      * @return new array of same type minus elements specified by unique values of {@code
indices}
      * @since 3.0.1
      */
@@ -7317,14 +7317,15 @@ public class ArrayUtils {
     static Object removeAll(final Object array, final int... indices) {
         final int length = getLength(array);
         int diff = 0; // number of distinct indexes, i.e. number of entries that will be
removed
+        int[] clonedIndices = clone(indices);
+        Arrays.sort(clonedIndices);
 
-        if (isNotEmpty(indices)) {
-            Arrays.sort(indices);
-
-            int i = indices.length;
+        // identify length of result array
+        if (isNotEmpty(clonedIndices)) {
+            int i = clonedIndices.length;
             int prevIndex = length;
             while (--i >= 0) {
-                final int index = indices[i];
+                final int index = clonedIndices[i];
                 if (index < 0 || index >= length) {
                     throw new IndexOutOfBoundsException("Index: " + index + ", Length: "
+ length);
                 }
@@ -7335,12 +7336,14 @@ public class ArrayUtils {
                 prevIndex = index;
             }
         }
+
+        // create result array
         final Object result = Array.newInstance(array.getClass().getComponentType(), length
- diff);
         if (diff < length) {
             int end = length; // index just after last copy
             int dest = length - diff; // number of entries so far not copied
-            for (int i = indices.length - 1; i >= 0; i--) {
-                final int index = indices[i];
+            for (int i = clonedIndices.length - 1; i >= 0; i--) {
+                final int index = clonedIndices[i];
                 if (end - index > 1) { // same as (cp > 0)
                     final int cp = end - index - 1;
                     dest -= cp;


Mime
View raw message