Make ObjectUtils.addObjectToArray() generic

This commit is contained in:
Chris Beams
2011-02-08 13:01:29 +00:00
parent fb57316afa
commit b04987ccc3
4 changed files with 16 additions and 15 deletions

View File

@@ -122,14 +122,14 @@ public abstract class ObjectUtils {
}
/**
* Append the given Object to the given array, returning a new array
* consisting of the input array contents plus the given Object.
* Append the given object to the given array, returning a new array
* consisting of the input array contents plus the given object.
* @param array the array to append to (can be <code>null</code>)
* @param obj the Object to append
* @param obj the object to append
* @return the new array (of the same component type; never <code>null</code>)
*/
public static Object[] addObjectToArray(Object[] array, Object obj) {
Class compType = Object.class;
public static <A,O extends A> A[] addObjectToArray(A[] array, O obj) {
Class<?> compType = Object.class;
if (array != null) {
compType = array.getClass().getComponentType();
}
@@ -137,7 +137,8 @@ public abstract class ObjectUtils {
compType = obj.getClass();
}
int newArrLength = (array != null ? array.length + 1 : 1);
Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
@SuppressWarnings("unchecked")
A[] newArr = (A[]) Array.newInstance(compType, newArrLength);
if (array != null) {
System.arraycopy(array, 0, newArr, 0, array.length);
}