From 3d69a1f2cdfa07da2dcfa8740592e4843cdee1c4 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Fri, 11 Sep 2009 18:27:18 +0000 Subject: [PATCH] ArrayUtils generics git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@813971 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ArrayUtils.java | 791 +++++++++--------- .../commons/lang/ArrayUtilsAddTest.java | 123 +-- 2 files changed, 467 insertions(+), 447 deletions(-) diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index 560b4c7f6..a4c810a9f 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -5,9 +5,9 @@ * 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. @@ -28,7 +28,7 @@ import org.apache.commons.lang.builder.ToStringStyle; /** *

Operations on arrays, primitive arrays (like int[]) and * primitive wrapper arrays (like Integer[]).

- * + * *

This class tries to handle null input gracefully. * An exception will not be thrown for a null * array input. However, an Object array that contains a null @@ -143,7 +143,7 @@ public class ArrayUtils { public ArrayUtils() { super(); } - + // Basic methods handling multi-dimensional arrays //----------------------------------------------------------------------- /** @@ -153,7 +153,7 @@ public class ArrayUtils { * multi-dimensional primitive arrays.

* *

The format is that of Java source code, for example {a,b}.

- * + * * @param array the array to get a toString for, may be null * @return a String representation of the array, '{}' if null array input */ @@ -168,11 +168,12 @@ public class ArrayUtils { * multi-dimensional primitive arrays.

* *

The format is that of Java source code, for example {a,b}.

- * + * * @param array the array to get a toString for, may be null * @param stringIfNull the String to return if the array is null * @return a String representation of the array - */ + */ + @SuppressWarnings("unchecked") public static String toString(Object array, String stringIfNull) { if (array == null) { return stringIfNull; @@ -182,9 +183,9 @@ public class ArrayUtils { /** *

Get a hashCode for an array handling multi-dimensional arrays correctly.

- * + * *

Multi-dimensional primitive arrays are also handled correctly by this method.

- * + * * @param array the array to get a hashCode for, may be null * @return a hashCode for the array, zero if null array input */ @@ -195,9 +196,9 @@ public class ArrayUtils { /** *

Compares two arrays, using equals(), handling multi-dimensional arrays * correctly.

- * + * *

Multi-dimensional primitive arrays are also handled correctly by this method.

- * + * * @param array1 the left hand array to compare, may be null * @param array2 the right hand array to compare, may be null * @return true if the arrays are equal @@ -222,7 +223,7 @@ public class ArrayUtils { * {"GREEN", "#00FF00"}, * {"BLUE", "#0000FF"}}); * - * + * *

This method returns null for a null input array.

* * @param array an array whose elements are either a {@link java.util.Map.Entry} or @@ -268,13 +269,13 @@ public class ArrayUtils { * *

The objects in the array are not cloned, thus there is no special * handling for multi-dimensional arrays.

- * + * *

This method returns null for a null input array.

- * + * * @param array the array to shallow clone, may be null * @return the cloned array, null if null input */ - public static Object[] clone(Object[] array) { + public static T[] clone(T[] array) { if (array == null) { return null; } @@ -286,7 +287,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -302,7 +303,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -318,7 +319,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -334,7 +335,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -350,7 +351,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -366,7 +367,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -382,7 +383,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -398,7 +399,7 @@ public class ArrayUtils { * null.

* *

This method returns null for a null input array.

- * + * * @param array the array to clone, may be null * @return the cloned array, null if null input */ @@ -438,7 +439,8 @@ public class ArrayUtils { * the start and end indices. * @since 2.1 */ - public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) { + @SuppressWarnings("unchecked") + public static T[] subarray(T[] array, int startIndexInclusive, int endIndexExclusive) { if (array == null) { return null; } @@ -451,9 +453,9 @@ public class ArrayUtils { int newSize = endIndexExclusive - startIndexInclusive; Class type = array.getClass().getComponentType(); if (newSize <= 0) { - return (Object[]) Array.newInstance(type, 0); + return (T[]) Array.newInstance(type, 0); } - Object[] subarray = (Object[]) Array.newInstance(type, newSize); + T[] subarray = (T[]) Array.newInstance(type, newSize); System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); return subarray; } @@ -777,12 +779,12 @@ public class ArrayUtils { * null arrays as length 0. * *

Any multi-dimensional aspects of the arrays are ignored.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating * null as an empty array - */ + */ public static boolean isSameLength(Object[] array1, Object[] array2) { if ((array1 == null && array2 != null && array2.length > 0) || (array2 == null && array1 != null && array1.length > 0) || @@ -795,7 +797,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -813,7 +815,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -831,7 +833,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -849,7 +851,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -867,7 +869,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -885,7 +887,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -903,7 +905,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -921,7 +923,7 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same length, treating * null arrays as length 0.

- * + * * @param array1 the first array, may be null * @param array2 the second array, may be null * @return true if length of arrays matches, treating @@ -967,12 +969,12 @@ public class ArrayUtils { /** *

Checks whether two arrays are the same type taking into account * multi-dimensional arrays.

- * + * * @param array1 the first array, must not be null * @param array2 the second array, must not be null * @return true if type of arrays matches * @throws IllegalArgumentException if either array is null - */ + */ public static boolean isSameType(Object array1, Object array2) { if (array1 == null || array2 == null) { throw new IllegalArgumentException("The Array must not be null"); @@ -982,13 +984,13 @@ public class ArrayUtils { // Reverse //----------------------------------------------------------------------- - /** + /** *

Reverses the order of the given array.

* *

There is no special handling for multi-dimensional arrays.

* *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(Object[] array) { @@ -1009,9 +1011,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(long[] array) { @@ -1032,9 +1034,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(int[] array) { @@ -1055,9 +1057,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(short[] array) { @@ -1078,9 +1080,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(char[] array) { @@ -1101,9 +1103,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(byte[] array) { @@ -1124,9 +1126,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(double[] array) { @@ -1147,9 +1149,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(float[] array) { @@ -1170,9 +1172,9 @@ public class ArrayUtils { /** *

Reverses the order of the given array.

- * + * *

This method does nothing for a null input array.

- * + * * @param array the array to reverse, may be null */ public static void reverse(boolean[] array) { @@ -1193,17 +1195,17 @@ public class ArrayUtils { // IndexOf search // ---------------------------------------------------------------------- - + // Object IndexOf //----------------------------------------------------------------------- /** *

Finds the index of the given object in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param objectToFind the object to find, may be null - * @return the index of the object within the array, + * @return the index of the object within the array, * {@link #INDEX_NOT_FOUND} (-1) if not found or null array input */ public static int indexOf(Object[] array, Object objectToFind) { @@ -1217,7 +1219,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param objectToFind the object to find, may be null * @param startIndex the index to start searching at @@ -1237,7 +1239,7 @@ public class ArrayUtils { return i; } } - } else { + } else {//TODO add quick type compatibility check for (int i = startIndex; i < array.length; i++) { if (objectToFind.equals(array[i])) { return i; @@ -1251,7 +1253,7 @@ public class ArrayUtils { *

Finds the last index of the given object within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param objectToFind the object to find, may be null * @return the last index of the object within the array, @@ -1268,7 +1270,7 @@ public class ArrayUtils { * *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than * the array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param objectToFind the object to find, may be null * @param startIndex the start index to travers backwards from @@ -1290,7 +1292,7 @@ public class ArrayUtils { return i; } } - } else { + } else {//TODO as above for (int i = startIndex; i >= 0; i--) { if (objectToFind.equals(array[i])) { return i; @@ -1304,7 +1306,7 @@ public class ArrayUtils { *

Checks if the object is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param objectToFind the object to find * @return true if the array contains the object @@ -1319,7 +1321,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -1336,7 +1338,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1362,7 +1364,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -1379,7 +1381,7 @@ public class ArrayUtils { * *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -1407,7 +1409,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -1422,7 +1424,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -1439,7 +1441,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1465,7 +1467,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -1482,7 +1484,7 @@ public class ArrayUtils { * *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -1510,7 +1512,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -1525,7 +1527,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -1542,7 +1544,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1568,7 +1570,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -1583,9 +1585,9 @@ public class ArrayUtils { * *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

* - *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the + *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -1613,7 +1615,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -1628,7 +1630,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -1646,7 +1648,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1673,7 +1675,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -1691,7 +1693,7 @@ public class ArrayUtils { * *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -1720,7 +1722,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -1736,7 +1738,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -1753,7 +1755,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1779,7 +1781,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -1794,9 +1796,9 @@ public class ArrayUtils { * *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

* - *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the + *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -1824,7 +1826,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -1839,7 +1841,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -1855,7 +1857,7 @@ public class ArrayUtils { * defined by valueToFind - tolerance and valueToFind + tolerance.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param tolerance tolerance of the search @@ -1873,7 +1875,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1904,7 +1906,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -1933,7 +1935,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -1949,7 +1951,7 @@ public class ArrayUtils { * defined by valueToFind - tolerance and valueToFind + tolerance.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param tolerance tolerance of the search @@ -1965,9 +1967,9 @@ public class ArrayUtils { * *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

* - *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the + *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -1998,9 +2000,9 @@ public class ArrayUtils { * *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

* - *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the + *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -2031,7 +2033,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -2042,7 +2044,7 @@ public class ArrayUtils { /** *

Checks if a value falling within the given tolerance is in the - * given array. If the array contains a value within the inclusive range + * given array. If the array contains a value within the inclusive range * defined by (value - tolerance) to (value + tolerance).

* *

The method returns false if a null array @@ -2063,7 +2065,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -2080,7 +2082,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -2106,7 +2108,7 @@ public class ArrayUtils { *

Finds the last index of the given value within the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -2121,9 +2123,9 @@ public class ArrayUtils { * *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

* - *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the + *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than the * array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -2151,7 +2153,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -2166,7 +2168,7 @@ public class ArrayUtils { *

Finds the index of the given value in the array.

* *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @return the index of the value within the array, @@ -2183,7 +2185,7 @@ public class ArrayUtils { * *

A negative startIndex is treated as zero. A startIndex larger than the array * length will return {@link #INDEX_NOT_FOUND} (-1).

- * + * * @param array the array to search through for the object, may be null * @param valueToFind the value to find * @param startIndex the index to start searching at @@ -2209,9 +2211,9 @@ public class ArrayUtils { /** *

Finds the last index of the given value within the array.

* - *

This method returns {@link #INDEX_NOT_FOUND} (-1) if + *

This method returns {@link #INDEX_NOT_FOUND} (-1) if * null array input.

- * + * * @param array the array to travers backwords looking for the object, may be null * @param valueToFind the object to find * @return the last index of the value within the array, @@ -2226,9 +2228,9 @@ public class ArrayUtils { * *

This method returns {@link #INDEX_NOT_FOUND} (-1) for a null input array.

* - *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than + *

A negative startIndex will return {@link #INDEX_NOT_FOUND} (-1). A startIndex larger than * the array length will search from the end of the array.

- * + * * @param array the array to traverse for looking for the object, may be null * @param valueToFind the value to find * @param startIndex the start index to travers backwards from @@ -2256,7 +2258,7 @@ public class ArrayUtils { *

Checks if the value is in the given array.

* *

The method returns false if a null array is passed in.

- * + * * @param array the array to search through * @param valueToFind the value to find * @return true if the array contains the object @@ -2274,7 +2276,7 @@ public class ArrayUtils { *

Converts an array of object Characters to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Character array, may be null * @return a char array, null if null array input * @throws NullPointerException if array content is null @@ -2294,9 +2296,9 @@ public class ArrayUtils { /** *

Converts an array of object Character to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Character array, may be null * @param valueForNull the value to insert if null found * @return a char array, null if null array input @@ -2314,12 +2316,12 @@ public class ArrayUtils { } return result; } - + /** *

Converts an array of primitive chars to objects.

* *

This method returns null for a null input array.

- * + * * @param array a char array * @return a Character array, null if null array input */ @@ -2331,18 +2333,18 @@ public class ArrayUtils { } final Character[] result = new Character[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = new Character(array[i]); + result[i] = Character.valueOf(array[i]); } return result; - } - + } + // Long array converters // ---------------------------------------------------------------------- /** *

Converts an array of object Longs to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Long array, may be null * @return a long array, null if null array input * @throws NullPointerException if array content is null @@ -2359,12 +2361,12 @@ public class ArrayUtils { } return result; } - + /** *

Converts an array of object Long to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Long array, may be null * @param valueForNull the value to insert if null found * @return a long array, null if null array input @@ -2382,12 +2384,12 @@ public class ArrayUtils { } return result; } - + /** *

Converts an array of primitive longs to objects.

* *

This method returns null for a null input array.

- * + * * @param array a long array * @return a Long array, null if null array input */ @@ -2399,7 +2401,7 @@ public class ArrayUtils { } final Long[] result = new Long[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = new Long(array[i]); + result[i] = Long.valueOf(array[i]); } return result; } @@ -2410,7 +2412,7 @@ public class ArrayUtils { *

Converts an array of object Integers to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Integer array, may be null * @return an int array, null if null array input * @throws NullPointerException if array content is null @@ -2430,9 +2432,9 @@ public class ArrayUtils { /** *

Converts an array of object Integer to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Integer array, may be null * @param valueForNull the value to insert if null found * @return an int array, null if null array input @@ -2455,7 +2457,7 @@ public class ArrayUtils { *

Converts an array of primitive ints to objects.

* *

This method returns null for a null input array.

- * + * * @param array an int array * @return an Integer array, null if null array input */ @@ -2467,18 +2469,18 @@ public class ArrayUtils { } final Integer[] result = new Integer[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = new Integer(array[i]); + result[i] = Integer.valueOf(array[i]); } return result; } - + // Short array converters // ---------------------------------------------------------------------- /** *

Converts an array of object Shorts to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Short array, may be null * @return a byte array, null if null array input * @throws NullPointerException if array content is null @@ -2498,9 +2500,9 @@ public class ArrayUtils { /** *

Converts an array of object Short to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Short array, may be null * @param valueForNull the value to insert if null found * @return a byte array, null if null array input @@ -2523,7 +2525,7 @@ public class ArrayUtils { *

Converts an array of primitive shorts to objects.

* *

This method returns null for a null input array.

- * + * * @param array a short array * @return a Short array, null if null array input */ @@ -2535,10 +2537,10 @@ public class ArrayUtils { } final Short[] result = new Short[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = new Short(array[i]); + result[i] = Short.valueOf(array[i]); } return result; - } + } // Byte array converters // ---------------------------------------------------------------------- @@ -2546,7 +2548,7 @@ public class ArrayUtils { *

Converts an array of object Bytes to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Byte array, may be null * @return a byte array, null if null array input * @throws NullPointerException if array content is null @@ -2566,9 +2568,9 @@ public class ArrayUtils { /** *

Converts an array of object Bytes to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Byte array, may be null * @param valueForNull the value to insert if null found * @return a byte array, null if null array input @@ -2591,7 +2593,7 @@ public class ArrayUtils { *

Converts an array of primitive bytes to objects.

* *

This method returns null for a null input array.

- * + * * @param array a byte array * @return a Byte array, null if null array input */ @@ -2606,15 +2608,15 @@ public class ArrayUtils { result[i] = Byte.valueOf(array[i]); } return result; - } - + } + // Double array converters // ---------------------------------------------------------------------- /** *

Converts an array of object Doubles to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Double array, may be null * @return a double array, null if null array input * @throws NullPointerException if array content is null @@ -2634,9 +2636,9 @@ public class ArrayUtils { /** *

Converts an array of object Doubles to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Double array, may be null * @param valueForNull the value to insert if null found * @return a double array, null if null array input @@ -2659,7 +2661,7 @@ public class ArrayUtils { *

Converts an array of primitive doubles to objects.

* *

This method returns null for a null input array.

- * + * * @param array a double array * @return a Double array, null if null array input */ @@ -2671,7 +2673,7 @@ public class ArrayUtils { } final Double[] result = new Double[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = new Double(array[i]); + result[i] = Double.valueOf(array[i]); } return result; } @@ -2682,7 +2684,7 @@ public class ArrayUtils { *

Converts an array of object Floats to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Float array, may be null * @return a float array, null if null array input * @throws NullPointerException if array content is null @@ -2702,9 +2704,9 @@ public class ArrayUtils { /** *

Converts an array of object Floats to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Float array, may be null * @param valueForNull the value to insert if null found * @return a float array, null if null array input @@ -2727,7 +2729,7 @@ public class ArrayUtils { *

Converts an array of primitive floats to objects.

* *

This method returns null for a null input array.

- * + * * @param array a float array * @return a Float array, null if null array input */ @@ -2739,7 +2741,7 @@ public class ArrayUtils { } final Float[] result = new Float[array.length]; for (int i = 0; i < array.length; i++) { - result[i] = new Float(array[i]); + result[i] = Float.valueOf(array[i]); } return result; } @@ -2750,7 +2752,7 @@ public class ArrayUtils { *

Converts an array of object Booleans to primitives.

* *

This method returns null for a null input array.

- * + * * @param array a Boolean array, may be null * @return a boolean array, null if null array input * @throws NullPointerException if array content is null @@ -2770,9 +2772,9 @@ public class ArrayUtils { /** *

Converts an array of object Booleans to primitives handling null.

- * + * *

This method returns null for a null input array.

- * + * * @param array a Boolean array, may be null * @param valueForNull the value to insert if null found * @return a boolean array, null if null array input @@ -2795,7 +2797,7 @@ public class ArrayUtils { *

Converts an array of primitive booleans to objects.

* *

This method returns null for a null input array.

- * + * * @param array a boolean array * @return a Boolean array, null if null array input */ @@ -2956,11 +2958,15 @@ public class ArrayUtils { * * @param array1 the first array whose elements are added to the new array, may be null * @param array2 the second array whose elements are added to the new array, may be null - * @return The new array, null if null array inputs. + * @return The new array, null if null array inputs. * The type of the new array is the type of the first array. * @since 2.1 */ public static Object[] addAll(Object[] array1, Object[] array2) { + /* this method could be generic as T[] addAll(T[], U[]), but if array1 == null, + * clone array2, then caller tries to add T objects to the U clone, could be a problem. + * Could add another version of the method where the first array cannot be null... :/ + */ if (array1 == null) { return clone(array2); } else if (array2 == null) { @@ -3209,12 +3215,12 @@ public class ArrayUtils { *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, null)      = [null]
      * ArrayUtils.add(null, "a")       = ["a"]
@@ -3222,35 +3228,36 @@ public class ArrayUtils {
      * ArrayUtils.add(["a"], "b")      = ["a", "b"]
      * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
      * 
- * + * * @param array the array to "add" the element to, may be null * @param element the object to add * @return A new array containing the existing elements plus the new element * @since 2.1 */ - public static Object[] add(Object[] array, Object element) { + @SuppressWarnings("unchecked") + public static T[] add(T[] array, T element) { Class type = array != null ? array.getClass() : (element != null ? element.getClass() : Object.class); - Object[] newArray = (Object[]) copyArrayGrow1(array, type); + T[] newArray = (T[]) copyArrayGrow1(array, type); newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, true)          = [true]
      * ArrayUtils.add([true], false)       = [true, false]
      * ArrayUtils.add([true, false], true) = [true, false, true]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3261,23 +3268,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0)   = [0]
      * ArrayUtils.add([1], 0)    = [1, 0]
      * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3288,23 +3295,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, '0')       = ['0']
      * ArrayUtils.add(['1'], '0')      = ['1', '0']
      * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3315,23 +3322,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0)   = [0]
      * ArrayUtils.add([1], 0)    = [1, 0]
      * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3342,23 +3349,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0)   = [0]
      * ArrayUtils.add([1], 0)    = [1, 0]
      * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3369,23 +3376,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0)   = [0]
      * ArrayUtils.add([1], 0)    = [1, 0]
      * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3396,23 +3403,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0)   = [0]
      * ArrayUtils.add([1], 0)    = [1, 0]
      * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3423,23 +3430,23 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** *

Copies the given array and adds the given element at the end of the new array.

* *

The new array contains the same elements of the input - * array plus the given element in the last position. The component type of + * array plus the given element in the last position. The component type of * the new array is the same as that of the input array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0)   = [0]
      * ArrayUtils.add([1], 0)    = [1, 0]
      * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
      * 
- * + * * @param array the array to copy and add the element to, may be null * @param element the object to add at the last index of the new array * @return A new array containing the existing elements plus the new element @@ -3450,16 +3457,16 @@ public class ArrayUtils { newArray[newArray.length - 1] = element; return newArray; } - + /** - * Returns a copy of the given array of size 1 greater than the argument. + * Returns a copy of the given array of size 1 greater than the argument. * The last value of the array is left to the default value. - * + * * @param array The array to copy, must not be null. - * @param newArrayComponentType If array is null, create a + * @param newArrayComponentType If array is null, create a * size 1 array of this type. * @return A new copy of the array of size 1 greater than the input. - */ + */ private static Object copyArrayGrow1(Object array, Class newArrayComponentType) { if (array != null) { int arrayLength = Array.getLength(array); @@ -3469,20 +3476,20 @@ public class ArrayUtils { } return Array.newInstance(newArrayComponentType, 1); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0, null)      = [null]
      * ArrayUtils.add(null, 0, "a")       = ["a"]
@@ -3490,70 +3497,71 @@ public class ArrayUtils {
      * ArrayUtils.add(["a"], 1, "b")      = ["a", "b"]
      * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ - public static Object[] add(Object[] array, int index, Object element) { + @SuppressWarnings("unchecked") + public static T[] add(T[] array, int index, T element) { Class clss = null; if (array != null) { clss = array.getClass().getComponentType(); } else if (element != null) { clss = element.getClass(); } else { - return new Object[]{null}; + return (T[]) new Object[] { null }; } - return (Object[]) add(array, index, element, clss); + return (T[]) add(array, index, element, clss); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0, true)          = [true]
      * ArrayUtils.add([true], 0, false)       = [false, true]
      * ArrayUtils.add([false], 1, true)       = [false, true]
      * ArrayUtils.add([true, false], 1, true) = [true, true, false]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static boolean[] add(boolean[] array, int index, boolean element) { return (boolean[]) add(array, index, Boolean.valueOf(element), Boolean.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add(null, 0, 'a')            = ['a']
      * ArrayUtils.add(['a'], 0, 'b')           = ['b', 'a']
@@ -3561,207 +3569,207 @@ public class ArrayUtils {
      * ArrayUtils.add(['a', 'b'], 1, 'k')      = ['a', 'k', 'b']
      * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static char[] add(char[] array, int index, char element) { - return (char[]) add(array, index, new Character(element), Character.TYPE); + return (char[]) add(array, index, Character.valueOf(element), Character.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add([1], 0, 2)         = [2, 1]
      * ArrayUtils.add([2, 6], 2, 3)      = [2, 6, 3]
      * ArrayUtils.add([2, 6], 0, 1)      = [1, 2, 6]
      * ArrayUtils.add([2, 6, 3], 2, 1)   = [2, 6, 1, 3]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static byte[] add(byte[] array, int index, byte element) { return (byte[]) add(array, index, Byte.valueOf(element), Byte.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add([1], 0, 2)         = [2, 1]
      * ArrayUtils.add([2, 6], 2, 10)     = [2, 6, 10]
      * ArrayUtils.add([2, 6], 0, -4)     = [-4, 2, 6]
      * ArrayUtils.add([2, 6, 3], 2, 1)   = [2, 6, 1, 3]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static short[] add(short[] array, int index, short element) { - return (short[]) add(array, index, new Short(element), Short.TYPE); + return (short[]) add(array, index, Short.valueOf(element), Short.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add([1], 0, 2)         = [2, 1]
      * ArrayUtils.add([2, 6], 2, 10)     = [2, 6, 10]
      * ArrayUtils.add([2, 6], 0, -4)     = [-4, 2, 6]
      * ArrayUtils.add([2, 6, 3], 2, 1)   = [2, 6, 1, 3]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static int[] add(int[] array, int index, int element) { - return (int[]) add(array, index, new Integer(element), Integer.TYPE); + return (int[]) add(array, index, Integer.valueOf(element), Integer.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add([1L], 0, 2L)           = [2L, 1L]
      * ArrayUtils.add([2L, 6L], 2, 10L)      = [2L, 6L, 10L]
      * ArrayUtils.add([2L, 6L], 0, -4L)      = [-4L, 2L, 6L]
      * ArrayUtils.add([2L, 6L, 3L], 2, 1L)   = [2L, 6L, 1L, 3L]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static long[] add(long[] array, int index, long element) { - return (long[]) add(array, index, new Long(element), Long.TYPE); + return (long[]) add(array, index, Long.valueOf(element), Long.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add([1.1f], 0, 2.2f)               = [2.2f, 1.1f]
      * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f)        = [2.3f, 6.4f, 10.5f]
      * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f)        = [-4.8f, 2.6f, 6.7f]
      * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f)   = [2.9f, 6.0f, 1.0f, 0.3f]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static float[] add(float[] array, int index, float element) { - return (float[]) add(array, index, new Float(element), Float.TYPE); + return (float[]) add(array, index, Float.valueOf(element), Float.TYPE); } - + /** - *

Inserts the specified element at the specified position in the array. + *

Inserts the specified element at the specified position in the array. * Shifts the element currently at that position (if any) and any subsequent * elements to the right (adds one to their indices).

* *

This method returns a new array with the same elements of the input - * array plus the given element on the specified position. The component - * type of the returned array is always the same as that of the input + * array plus the given element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, a new one element array is returned * whose component type is the same as the element.

- * + * *
      * ArrayUtils.add([1.1], 0, 2.2)              = [2.2, 1.1]
      * ArrayUtils.add([2.3, 6.4], 2, 10.5)        = [2.3, 6.4, 10.5]
      * ArrayUtils.add([2.6, 6.7], 0, -4.8)        = [-4.8, 2.6, 6.7]
      * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0)    = [2.9, 6.0, 1.0, 0.3]
      * 
- * + * * @param array the array to add the element to, may be null * @param index the position of the new object * @param element the object to add * @return A new array containing the existing elements and the new element - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > array.length). */ public static double[] add(double[] array, int index, double element) { - return (double[]) add(array, index, new Double(element), Double.TYPE); + return (double[]) add(array, index, Double.valueOf(element), Double.TYPE); } - + /** - * Underlying implementation of add(array, index, element) methods. - * The last parameter is the class, which may not equal element.getClass + * Underlying implementation of add(array, index, element) methods. + * The last parameter is the class, which may not equal element.getClass * for primitives. * * @param array the array to add the element to, may be null @@ -3791,15 +3799,15 @@ public class ArrayUtils { } return result; } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -3811,28 +3819,29 @@ public class ArrayUtils { * ArrayUtils.remove(["a", "b"], 1) = ["a"] * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ - public static Object[] remove(Object[] array, int index) { - return (Object[]) remove((Object) array, index); + @SuppressWarnings("unchecked") + public static T[] remove(T[] array, int index) { + return (T[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -3842,29 +3851,29 @@ public class ArrayUtils {
      * ArrayUtils.removeElement(["a", "b"], "a")      = ["b"]
      * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first * occurrence of the specified element. * @since 2.1 */ - public static Object[] removeElement(Object[] array, Object element) { + public static T[] removeElement(T[] array, Object element) { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -3876,28 +3885,28 @@ public class ArrayUtils { * ArrayUtils.remove([true, false], 1) = [true] * ArrayUtils.remove([true, true, false], 1) = [true, false] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static boolean[] remove(boolean[] array, int index) { return (boolean[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -3907,7 +3916,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([true, false], false)      = [true]
      * ArrayUtils.removeElement([true, false, true], true) = [false, true]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -3918,18 +3927,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -3941,28 +3950,28 @@ public class ArrayUtils { * ArrayUtils.remove([1, 0], 1) = [1] * ArrayUtils.remove([1, 0, 1], 1) = [1, 1] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static byte[] remove(byte[] array, int index) { return (byte[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -3972,7 +3981,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([1, 0], 0)      = [1]
      * ArrayUtils.removeElement([1, 0, 1], 1)   = [0, 1]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -3983,18 +3992,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -4006,28 +4015,28 @@ public class ArrayUtils { * ArrayUtils.remove(['a', 'b'], 1) = ['a'] * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c'] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static char[] remove(char[] array, int index) { return (char[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -4037,7 +4046,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement(['a', 'b'], 'a')      = ['b']
      * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -4048,18 +4057,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -4071,28 +4080,28 @@ public class ArrayUtils { * ArrayUtils.remove([2.5, 6.0], 1) = [2.5] * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static double[] remove(double[] array, int index) { return (double[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -4102,7 +4111,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([1.1, 2.3], 1.1)      = [2.3]
      * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -4113,18 +4122,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -4136,28 +4145,28 @@ public class ArrayUtils { * ArrayUtils.remove([2.5, 6.0], 1) = [2.5] * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static float[] remove(float[] array, int index) { return (float[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -4167,7 +4176,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([1.1, 2.3], 1.1)      = [2.3]
      * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -4178,18 +4187,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -4201,28 +4210,28 @@ public class ArrayUtils { * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static int[] remove(int[] array, int index) { return (int[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -4232,7 +4241,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([1, 3], 1)    = [3]
      * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -4243,18 +4252,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -4266,28 +4275,28 @@ public class ArrayUtils { * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static long[] remove(long[] array, int index) { return (long[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -4297,7 +4306,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([1, 3], 1)    = [3]
      * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -4308,18 +4317,18 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException @@ -4331,28 +4340,28 @@ public class ArrayUtils { * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] * - * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ public static short[] remove(short[] array, int index) { return (short[]) remove((Object) array, index); } - + /** *

Removes the first occurrence of the specified element from the - * specified array. All subsequent elements are shifted to the left + * specified array. All subsequent elements are shifted to the left * (substracts one from their indices). If the array doesn't contains * such an element, no elements are removed from the array.

* *

This method returns a new array with the same elements of the input - * array except the first occurrence of the specified element. The component - * type of the returned array is always the same as that of the input + * array except the first occurrence of the specified element. The component + * type of the returned array is always the same as that of the input * array.

* *
@@ -4362,7 +4371,7 @@ public class ArrayUtils {
      * ArrayUtils.removeElement([1, 3], 1)    = [3]
      * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
      * 
- * + * * @param array the array to remove the element from, may be null * @param element the element to be removed * @return A new array containing the existing elements except the first @@ -4373,28 +4382,28 @@ public class ArrayUtils { int index = indexOf(array, element); if (index == INDEX_NOT_FOUND) { return clone(array); - } + } return remove(array, index); } - + /** *

Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).

* *

This method returns a new array with the same elements of the input - * array except the element on the specified position. The component - * type of the returned array is always the same as that of the input + * array except the element on the specified position. The component + * type of the returned array is always the same as that of the input * array.

* *

If the input array is null, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.

- * + * * @param array the array to remove the element from, may not be null * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. - * @throws IndexOutOfBoundsException if the index is out of range + * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is null. * @since 2.1 */ @@ -4403,13 +4412,13 @@ public class ArrayUtils { if (index < 0 || index >= length) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } - + Object result = Array.newInstance(array.getClass().getComponentType(), length - 1); System.arraycopy(array, 0, result, 0, index); if (index < length - 1) { System.arraycopy(array, index + 1, result, index, length - index - 1); } - + return result; } diff --git a/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java b/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java index e469147ea..23ab9ce7b 100644 --- a/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java +++ b/src/test/org/apache/commons/lang/ArrayUtilsAddTest.java @@ -5,9 +5,9 @@ * 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. @@ -26,7 +26,7 @@ import junit.textui.TestRunner; /** * Tests ArrayUtils add methods. - * + * * @author Gary D. Gregory * @version $Id$ */ @@ -54,7 +54,7 @@ public class ArrayUtilsAddTest extends TestCase { assertTrue(Arrays.equals(new boolean[]{true, false, true, false}, newArray)); assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayByte() { byte[] newArray; newArray = ArrayUtils.add((byte[])null, (byte)0); @@ -69,9 +69,9 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, (byte)4); assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, newArray)); - assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); + assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayChar() { char[] newArray; newArray = ArrayUtils.add((char[])null, (char)0); @@ -86,9 +86,9 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Character.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, (char)4); assertTrue(Arrays.equals(new char[]{1, 2, 3, 4}, newArray)); - assertEquals(Character.TYPE, newArray.getClass().getComponentType()); + assertEquals(Character.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayDouble() { double[] newArray; newArray = ArrayUtils.add((double[])null, 0); @@ -103,9 +103,9 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Double.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, 4); assertTrue(Arrays.equals(new double[]{1, 2, 3, 4}, newArray)); - assertEquals(Double.TYPE, newArray.getClass().getComponentType()); + assertEquals(Double.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayFloat() { float[] newArray; newArray = ArrayUtils.add((float[])null, 0); @@ -120,9 +120,9 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Float.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, 4); assertTrue(Arrays.equals(new float[]{1, 2, 3, 4}, newArray)); - assertEquals(Float.TYPE, newArray.getClass().getComponentType()); + assertEquals(Float.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayInt() { int[] newArray; newArray = ArrayUtils.add((int[])null, 0); @@ -137,9 +137,9 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, 4); assertTrue(Arrays.equals(new int[]{1, 2, 3, 4}, newArray)); - assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); + assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayLong() { long[] newArray; newArray = ArrayUtils.add((long[])null, 0); @@ -154,9 +154,9 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Long.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, 4); assertTrue(Arrays.equals(new long[]{1, 2, 3, 4}, newArray)); - assertEquals(Long.TYPE, newArray.getClass().getComponentType()); + assertEquals(Long.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayShort() { short[] newArray; newArray = ArrayUtils.add((short[])null, (short)0); @@ -171,45 +171,56 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(Short.TYPE, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(array1, (short)4); assertTrue(Arrays.equals(new short[]{1, 2, 3, 4}, newArray)); - assertEquals(Short.TYPE, newArray.getClass().getComponentType()); + assertEquals(Short.TYPE, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayObject() { Object[] newArray; newArray = ArrayUtils.add((Object[])null, null); assertTrue(Arrays.equals((new Object[]{null}), newArray)); assertEquals(Object.class, newArray.getClass().getComponentType()); - + + //show that not casting is okay + newArray = ArrayUtils.add(null, null); + assertTrue(Arrays.equals((new Object[]{null}), newArray)); + assertEquals(Object.class, newArray.getClass().getComponentType()); + newArray = ArrayUtils.add((Object[])null, "a"); assertTrue(Arrays.equals((new String[]{"a"}), newArray)); assertTrue(Arrays.equals((new Object[]{"a"}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); - + + //show that not casting to Object[] is okay and will assume String based on "a" + String[] newStringArray = ArrayUtils.add(null, "a"); + assertTrue(Arrays.equals((new String[]{"a"}), newStringArray)); + assertTrue(Arrays.equals((new Object[]{"a"}), newStringArray)); + assertEquals(String.class, newStringArray.getClass().getComponentType()); + String[] stringArray1 = new String[]{"a", "b", "c"}; newArray = ArrayUtils.add(stringArray1, null); assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); - + newArray = ArrayUtils.add(stringArray1, "d"); assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); - + Number[] numberArray1 = new Number[]{new Integer(1), new Double(2)}; newArray = ArrayUtils.add(numberArray1, new Float(3)); assertTrue(Arrays.equals((new Number[]{new Integer(1), new Double(2), new Float(3)}), newArray)); assertEquals(Number.class, newArray.getClass().getComponentType()); - + numberArray1 = null; newArray = ArrayUtils.add(numberArray1, new Float(3)); assertTrue(Arrays.equals((new Float[]{new Float(3)}), newArray)); assertEquals(Float.class, newArray.getClass().getComponentType()); - + numberArray1 = null; newArray = ArrayUtils.add(numberArray1, null); assertTrue(Arrays.equals((new Object[]{null}), newArray)); assertEquals(Object.class, newArray.getClass().getComponentType()); } - + public void testAddObjectArrayToObjectArray() { assertNull(ArrayUtils.addAll((Object[]) null, (Object[]) null)); Object[] newArray; @@ -241,92 +252,92 @@ public class ArrayUtilsAddTest extends TestCase { assertTrue(Arrays.equals((new String[]{}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); String[] stringArrayNull = new String []{null}; - newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull); + newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull); assertTrue(Arrays.equals((new String[]{null, null}), newArray)); assertEquals(String.class, newArray.getClass().getComponentType()); // boolean - assertTrue( Arrays.equals( new boolean[] { true, false, false, true }, + assertTrue( Arrays.equals( new boolean[] { true, false, false, true }, ArrayUtils.addAll( new boolean[] { true, false }, new boolean[] { false, true } ) ) ); - assertTrue( Arrays.equals( new boolean[] { false, true }, + assertTrue( Arrays.equals( new boolean[] { false, true }, ArrayUtils.addAll( null, new boolean[] { false, true } ) ) ); - assertTrue( Arrays.equals( new boolean[] { true, false }, + assertTrue( Arrays.equals( new boolean[] { true, false }, ArrayUtils.addAll( new boolean[] { true, false }, null ) ) ); // char - assertTrue( Arrays.equals( new char[] { 'a', 'b', 'c', 'd' }, + assertTrue( Arrays.equals( new char[] { 'a', 'b', 'c', 'd' }, ArrayUtils.addAll( new char[] { 'a', 'b' }, new char[] { 'c', 'd' } ) ) ); - assertTrue( Arrays.equals( new char[] { 'c', 'd' }, + assertTrue( Arrays.equals( new char[] { 'c', 'd' }, ArrayUtils.addAll( null, new char[] { 'c', 'd' } ) ) ); - assertTrue( Arrays.equals( new char[] { 'a', 'b' }, + assertTrue( Arrays.equals( new char[] { 'a', 'b' }, ArrayUtils.addAll( new char[] { 'a', 'b' }, null ) ) ); // byte - assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3 }, + assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3 }, ArrayUtils.addAll( new byte[] { (byte) 0, (byte) 1 }, new byte[] { (byte) 2, (byte) 3 } ) ) ); - assertTrue( Arrays.equals( new byte[] { (byte) 2, (byte) 3 }, + assertTrue( Arrays.equals( new byte[] { (byte) 2, (byte) 3 }, ArrayUtils.addAll( null, new byte[] { (byte) 2, (byte) 3 } ) ) ); - assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1 }, + assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1 }, ArrayUtils.addAll( new byte[] { (byte) 0, (byte) 1 }, null ) ) ); // short - assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20, (short) 30, (short) 40 }, + assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20, (short) 30, (short) 40 }, ArrayUtils.addAll( new short[] { (short) 10, (short) 20 }, new short[] { (short) 30, (short) 40 } ) ) ); - assertTrue( Arrays.equals( new short[] { (short) 30, (short) 40 }, + assertTrue( Arrays.equals( new short[] { (short) 30, (short) 40 }, ArrayUtils.addAll( null, new short[] { (short) 30, (short) 40 } ) ) ); - assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20 }, + assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20 }, ArrayUtils.addAll( new short[] { (short) 10, (short) 20 }, null ) ) ); // int - assertTrue( Arrays.equals( new int[] { 1, 1000, -1000, -1 }, + assertTrue( Arrays.equals( new int[] { 1, 1000, -1000, -1 }, ArrayUtils.addAll( new int[] { 1, 1000 }, new int[] { -1000, -1 } ) ) ); - assertTrue( Arrays.equals( new int[] { -1000, -1 }, + assertTrue( Arrays.equals( new int[] { -1000, -1 }, ArrayUtils.addAll( null, new int[] { -1000, -1 } ) ) ); - assertTrue( Arrays.equals( new int[] { 1, 1000 }, + assertTrue( Arrays.equals( new int[] { 1, 1000 }, ArrayUtils.addAll( new int[] { 1, 1000 }, null ) ) ); // long - assertTrue( Arrays.equals( new long[] { 1L, -1L, 1000L, -1000L }, + assertTrue( Arrays.equals( new long[] { 1L, -1L, 1000L, -1000L }, ArrayUtils.addAll( new long[] { 1L, -1L }, new long[] { 1000L, -1000L } ) ) ); - assertTrue( Arrays.equals( new long[] { 1000L, -1000L }, + assertTrue( Arrays.equals( new long[] { 1000L, -1000L }, ArrayUtils.addAll( null, new long[] { 1000L, -1000L } ) ) ); - - assertTrue( Arrays.equals( new long[] { 1L, -1L }, + + assertTrue( Arrays.equals( new long[] { 1L, -1L }, ArrayUtils.addAll( new long[] { 1L, -1L }, null ) ) ); // float - assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f, 1.6f, 0.01f }, + assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f, 1.6f, 0.01f }, ArrayUtils.addAll( new float[] { 10.5f, 10.1f }, new float[] { 1.6f, 0.01f } ) ) ); - assertTrue( Arrays.equals( new float[] { 1.6f, 0.01f }, + assertTrue( Arrays.equals( new float[] { 1.6f, 0.01f }, ArrayUtils.addAll( null, new float[] { 1.6f, 0.01f } ) ) ); - assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f }, + assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f }, ArrayUtils.addAll( new float[] { 10.5f, 10.1f }, null ) ) ); // double - assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI, 0, 9.99 }, + assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI, 0, 9.99 }, ArrayUtils.addAll( new double[] { Math.PI, -Math.PI }, new double[] { 0, 9.99 } ) ) ); - assertTrue( Arrays.equals( new double[] { 0, 9.99 }, + assertTrue( Arrays.equals( new double[] { 0, 9.99 }, ArrayUtils.addAll( null, new double[] { 0, 9.99 } ) ) ); - assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI }, + assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI }, ArrayUtils.addAll( new double[] { Math.PI, -Math.PI }, null ) ) ); - } - + } + public void testAddObjectAtIndex() { Object[] newArray; newArray = ArrayUtils.add((Object[])null, 0, null); @@ -348,13 +359,13 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals(String.class, newArray.getClass().getComponentType()); newArray = ArrayUtils.add(stringArray1, 3, "d"); assertTrue(Arrays.equals((new String[]{"a", "b", "c", "d"}), newArray)); - assertEquals(String.class, newArray.getClass().getComponentType()); + assertEquals(String.class, newArray.getClass().getComponentType()); assertEquals(String.class, newArray.getClass().getComponentType()); Object[] o = new Object[] {"1", "2", "4"}; Object[] result = ArrayUtils.add(o, 2, "3"); Object[] result2 = ArrayUtils.add(o, 3, "5"); - + assertNotNull(result); assertEquals(4, result.length); assertEquals("1", result[0]); @@ -570,5 +581,5 @@ public class ArrayUtilsAddTest extends TestCase { assertEquals("Index: -1, Length: 2", e.getMessage()); } } - + }