ArrayUtils generics

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@813971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Jason Benson 2009-09-11 18:27:18 +00:00
parent ab66a1d87c
commit 3d69a1f2cd
2 changed files with 467 additions and 447 deletions

View File

@ -173,6 +173,7 @@ public class ArrayUtils {
* @param stringIfNull the String to return if the array is <code>null</code> * @param stringIfNull the String to return if the array is <code>null</code>
* @return a String representation of the array * @return a String representation of the array
*/ */
@SuppressWarnings("unchecked")
public static String toString(Object array, String stringIfNull) { public static String toString(Object array, String stringIfNull) {
if (array == null) { if (array == null) {
return stringIfNull; return stringIfNull;
@ -274,7 +275,7 @@ public class ArrayUtils {
* @param array the array to shallow clone, may be <code>null</code> * @param array the array to shallow clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input * @return the cloned array, <code>null</code> if <code>null</code> input
*/ */
public static Object[] clone(Object[] array) { public static <T> T[] clone(T[] array) {
if (array == null) { if (array == null) {
return null; return null;
} }
@ -438,7 +439,8 @@ public class ArrayUtils {
* the start and end indices. * the start and end indices.
* @since 2.1 * @since 2.1
*/ */
public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) { @SuppressWarnings("unchecked")
public static <T> T[] subarray(T[] array, int startIndexInclusive, int endIndexExclusive) {
if (array == null) { if (array == null) {
return null; return null;
} }
@ -451,9 +453,9 @@ public class ArrayUtils {
int newSize = endIndexExclusive - startIndexInclusive; int newSize = endIndexExclusive - startIndexInclusive;
Class<?> type = array.getClass().getComponentType(); Class<?> type = array.getClass().getComponentType();
if (newSize <= 0) { 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); System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
return subarray; return subarray;
} }
@ -1237,7 +1239,7 @@ public class ArrayUtils {
return i; return i;
} }
} }
} else { } else {//TODO add quick type compatibility check
for (int i = startIndex; i < array.length; i++) { for (int i = startIndex; i < array.length; i++) {
if (objectToFind.equals(array[i])) { if (objectToFind.equals(array[i])) {
return i; return i;
@ -1290,7 +1292,7 @@ public class ArrayUtils {
return i; return i;
} }
} }
} else { } else {//TODO as above
for (int i = startIndex; i >= 0; i--) { for (int i = startIndex; i >= 0; i--) {
if (objectToFind.equals(array[i])) { if (objectToFind.equals(array[i])) {
return i; return i;
@ -2331,7 +2333,7 @@ public class ArrayUtils {
} }
final Character[] result = new Character[array.length]; final Character[] result = new Character[array.length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
result[i] = new Character(array[i]); result[i] = Character.valueOf(array[i]);
} }
return result; return result;
} }
@ -2399,7 +2401,7 @@ public class ArrayUtils {
} }
final Long[] result = new Long[array.length]; final Long[] result = new Long[array.length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
result[i] = new Long(array[i]); result[i] = Long.valueOf(array[i]);
} }
return result; return result;
} }
@ -2467,7 +2469,7 @@ public class ArrayUtils {
} }
final Integer[] result = new Integer[array.length]; final Integer[] result = new Integer[array.length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
result[i] = new Integer(array[i]); result[i] = Integer.valueOf(array[i]);
} }
return result; return result;
} }
@ -2535,7 +2537,7 @@ public class ArrayUtils {
} }
final Short[] result = new Short[array.length]; final Short[] result = new Short[array.length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
result[i] = new Short(array[i]); result[i] = Short.valueOf(array[i]);
} }
return result; return result;
} }
@ -2671,7 +2673,7 @@ public class ArrayUtils {
} }
final Double[] result = new Double[array.length]; final Double[] result = new Double[array.length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
result[i] = new Double(array[i]); result[i] = Double.valueOf(array[i]);
} }
return result; return result;
} }
@ -2739,7 +2741,7 @@ public class ArrayUtils {
} }
final Float[] result = new Float[array.length]; final Float[] result = new Float[array.length];
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
result[i] = new Float(array[i]); result[i] = Float.valueOf(array[i]);
} }
return result; return result;
} }
@ -2961,6 +2963,10 @@ public class ArrayUtils {
* @since 2.1 * @since 2.1
*/ */
public static Object[] addAll(Object[] array1, Object[] array2) { public static Object[] addAll(Object[] array1, Object[] array2) {
/* this method could be generic as <T, U extends T> 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) { if (array1 == null) {
return clone(array2); return clone(array2);
} else if (array2 == null) { } else if (array2 == null) {
@ -3228,9 +3234,10 @@ public class ArrayUtils {
* @return A new array containing the existing elements plus the new element * @return A new array containing the existing elements plus the new element
* @since 2.1 * @since 2.1
*/ */
public static Object[] add(Object[] array, Object element) { @SuppressWarnings("unchecked")
public static <T> T[] add(T[] array, T element) {
Class<?> type = array != null ? array.getClass() : (element != null ? element.getClass() : Object.class); 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; newArray[newArray.length - 1] = element;
return newArray; return newArray;
} }
@ -3498,16 +3505,17 @@ public class ArrayUtils {
* @throws IndexOutOfBoundsException if the index is out of range * @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static Object[] add(Object[] array, int index, Object element) { @SuppressWarnings("unchecked")
public static <T> T[] add(T[] array, int index, T element) {
Class<?> clss = null; Class<?> clss = null;
if (array != null) { if (array != null) {
clss = array.getClass().getComponentType(); clss = array.getClass().getComponentType();
} else if (element != null) { } else if (element != null) {
clss = element.getClass(); clss = element.getClass();
} else { } else {
return new Object[]{null}; return (T[]) new Object[] { null };
} }
return (Object[]) add(array, index, element, clss); return (T[]) add(array, index, element, clss);
} }
/** /**
@ -3570,7 +3578,7 @@ public class ArrayUtils {
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static char[] add(char[] array, int index, char element) { 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);
} }
/** /**
@ -3632,7 +3640,7 @@ public class ArrayUtils {
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static short[] add(short[] array, int index, short element) { 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);
} }
/** /**
@ -3663,7 +3671,7 @@ public class ArrayUtils {
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static int[] add(int[] array, int index, int element) { 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);
} }
/** /**
@ -3694,7 +3702,7 @@ public class ArrayUtils {
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static long[] add(long[] array, int index, long element) { 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);
} }
/** /**
@ -3725,7 +3733,7 @@ public class ArrayUtils {
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static float[] add(float[] array, int index, float element) { 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);
} }
/** /**
@ -3756,7 +3764,7 @@ public class ArrayUtils {
* (index < 0 || index > array.length). * (index < 0 || index > array.length).
*/ */
public static double[] add(double[] array, int index, double element) { 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);
} }
/** /**
@ -3820,8 +3828,9 @@ public class ArrayUtils {
* (index < 0 || index >= array.length), or if the array is <code>null</code>. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
* @since 2.1 * @since 2.1
*/ */
public static Object[] remove(Object[] array, int index) { @SuppressWarnings("unchecked")
return (Object[]) remove((Object) array, index); public static <T> T[] remove(T[] array, int index) {
return (T[]) remove((Object) array, index);
} }
/** /**
@ -3849,7 +3858,7 @@ public class ArrayUtils {
* occurrence of the specified element. * occurrence of the specified element.
* @since 2.1 * @since 2.1
*/ */
public static Object[] removeElement(Object[] array, Object element) { public static <T> T[] removeElement(T[] array, Object element) {
int index = indexOf(array, element); int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) { if (index == INDEX_NOT_FOUND) {
return clone(array); return clone(array);

View File

@ -180,11 +180,22 @@ public class ArrayUtilsAddTest extends TestCase {
assertTrue(Arrays.equals((new Object[]{null}), newArray)); assertTrue(Arrays.equals((new Object[]{null}), newArray));
assertEquals(Object.class, newArray.getClass().getComponentType()); 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"); newArray = ArrayUtils.add((Object[])null, "a");
assertTrue(Arrays.equals((new String[]{"a"}), newArray)); assertTrue(Arrays.equals((new String[]{"a"}), newArray));
assertTrue(Arrays.equals((new Object[]{"a"}), newArray)); assertTrue(Arrays.equals((new Object[]{"a"}), newArray));
assertEquals(String.class, newArray.getClass().getComponentType()); 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"}; String[] stringArray1 = new String[]{"a", "b", "c"};
newArray = ArrayUtils.add(stringArray1, null); newArray = ArrayUtils.add(stringArray1, null);
assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray)); assertTrue(Arrays.equals((new String[]{"a", "b", "c", null}), newArray));