1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 02:36:01 +02:00

Type safe version of removeNulls.

This commit is contained in:
Sergey Prigogin 2009-10-04 05:57:22 +00:00
parent 68a906b0a6
commit 3b408710f4

View file

@ -21,7 +21,7 @@ import java.lang.reflect.Array;
*/ */
public abstract class ArrayUtil { public abstract class ArrayUtil {
private static final int DEFAULT_LENGTH = 2; private static final int DEFAULT_LENGTH = 2;
/** /**
* Assumes that array contains nulls at the end, only. * Assumes that array contains nulls at the end, only.
* Appends element after the last non-null element. * Appends element after the last non-null element.
@ -68,7 +68,6 @@ public abstract class ArrayUtil {
} }
return haveNull ? right + 1 : -1; return haveNull ? right + 1 : -1;
} }
/** /**
* Assumes that array contains nulls at the end, only. * Assumes that array contains nulls at the end, only.
@ -150,7 +149,7 @@ public abstract class ArrayUtil {
static public Object[] trim(Class<?> c, Object[] array, boolean forceNew) { static public Object[] trim(Class<?> c, Object[] array, boolean forceNew) {
if (array == null) if (array == null)
return (Object[]) Array.newInstance(c, 0); return (Object[]) Array.newInstance(c, 0);
int i = array.length; int i = array.length;
if (i == 0 || array[i - 1] != null) { if (i == 0 || array[i - 1] != null) {
if (!forceNew) { if (!forceNew) {
@ -215,7 +214,7 @@ public abstract class ArrayUtil {
public static Object[] addAll(Class<?> c, Object[] dest, Object[] source) { public static Object[] addAll(Class<?> c, Object[] dest, Object[] source) {
if (source == null || source.length == 0) if (source == null || source.length == 0)
return dest; return dest;
int numToAdd = findFirstNull(source); int numToAdd = findFirstNull(source);
if (numToAdd <= 0) { if (numToAdd <= 0) {
if (numToAdd == 0) { if (numToAdd == 0) {
@ -223,18 +222,18 @@ public abstract class ArrayUtil {
} }
numToAdd= source.length; numToAdd= source.length;
} }
if (dest == null || dest.length == 0) { if (dest == null || dest.length == 0) {
dest = (Object[]) Array.newInstance(c, numToAdd); dest = (Object[]) Array.newInstance(c, numToAdd);
System.arraycopy(source, 0, dest, 0, numToAdd); System.arraycopy(source, 0, dest, 0, numToAdd);
return dest; return dest;
} }
int firstFree = findFirstNull(dest); int firstFree = findFirstNull(dest);
if (firstFree < 0) { if (firstFree < 0) {
firstFree= dest.length; firstFree= dest.length;
} }
if (firstFree + numToAdd <= dest.length) { if (firstFree + numToAdd <= dest.length) {
System.arraycopy(source, 0, dest, firstFree, numToAdd); System.arraycopy(source, 0, dest, firstFree, numToAdd);
return dest; return dest;
@ -254,7 +253,7 @@ public abstract class ArrayUtil {
public static <T> T[] addAll(T[] dest, T[] source) { public static <T> T[] addAll(T[] dest, T[] source) {
if (source == null || source.length == 0) if (source == null || source.length == 0)
return dest; return dest;
int numToAdd = findFirstNull(source); int numToAdd = findFirstNull(source);
if (numToAdd <= 0) { if (numToAdd <= 0) {
if (numToAdd == 0) { if (numToAdd == 0) {
@ -262,19 +261,19 @@ public abstract class ArrayUtil {
} }
numToAdd= source.length; numToAdd= source.length;
} }
if (dest == null || dest.length == 0) { if (dest == null || dest.length == 0) {
Class c = dest != null ? dest.getClass().getComponentType() : source.getClass().getComponentType(); Class c = dest != null ? dest.getClass().getComponentType() : source.getClass().getComponentType();
dest = (T[]) Array.newInstance(c, numToAdd); dest = (T[]) Array.newInstance(c, numToAdd);
System.arraycopy(source, 0, dest, 0, numToAdd); System.arraycopy(source, 0, dest, 0, numToAdd);
return dest; return dest;
} }
int firstFree = findFirstNull(dest); int firstFree = findFirstNull(dest);
if (firstFree < 0) { if (firstFree < 0) {
firstFree= dest.length; firstFree= dest.length;
} }
if (firstFree + numToAdd <= dest.length) { if (firstFree + numToAdd <= dest.length) {
System.arraycopy(source, 0, dest, firstFree, numToAdd); System.arraycopy(source, 0, dest, firstFree, numToAdd);
return dest; return dest;
@ -284,7 +283,7 @@ public abstract class ArrayUtil {
System.arraycopy(source, 0, temp, firstFree, numToAdd); System.arraycopy(source, 0, temp, firstFree, numToAdd);
return temp; return temp;
} }
/** /**
* Returns whether the specified array contains the specified object. Comparison is by * Returns whether the specified array contains the specified object. Comparison is by
* object identity. * object identity.
@ -293,9 +292,9 @@ public abstract class ArrayUtil {
* @return true if the specified array contains the specified object, or the specified array is null * @return true if the specified array contains the specified object, or the specified array is null
*/ */
public static boolean contains(Object[] array, Object obj) { public static boolean contains(Object[] array, Object obj) {
return indexOf(array, obj)!=-1; return indexOf(array, obj) >= 0;
} }
/** /**
* Returns the index into the specified array of the specified object, or -1 if the array does not * Returns the index into the specified array of the specified object, or -1 if the array does not
* contain the object, or if the array is null. Comparison is by object identity. * contain the object, or if the array is null. Comparison is by object identity.
@ -306,7 +305,7 @@ public abstract class ArrayUtil {
*/ */
public static int indexOf(Object[] array, Object obj) { public static int indexOf(Object[] array, Object obj) {
int result = -1; int result = -1;
if (array!=null) { if (array != null) {
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
if (array[i] == obj) if (array[i] == obj)
return i; return i;
@ -314,7 +313,7 @@ public abstract class ArrayUtil {
} }
return result; return result;
} }
/** /**
* Assumes that array contains nulls at the end, only. * Assumes that array contains nulls at the end, only.
* Returns whether the specified array contains the specified object. Comparison is by * Returns whether the specified array contains the specified object. Comparison is by
@ -338,7 +337,7 @@ public abstract class ArrayUtil {
*/ */
public static int indexOfEqual(Object[] comments, Object comment) { public static int indexOfEqual(Object[] comments, Object comment) {
int result = -1; int result = -1;
if (comments!=null) { if (comments != null) {
for (int i= 0; (i < comments.length) && (comments[i] != null); i++) { for (int i= 0; (i < comments.length) && (comments[i] != null); i++) {
if (comments[i].equals(comment)) if (comments[i].equals(comment))
return i; return i;
@ -347,36 +346,68 @@ public abstract class ArrayUtil {
return result; return result;
} }
/** /**
* Note that this should only be used when the placement of * Note that this should only be used when the placement of nulls within the array
* nulls within the array is unknown (due to performance efficiency). * is unknown (due to performance efficiency).
* *
* Removes all of the nulls from the array and returns a new * Removes all of the nulls from the array and returns a new array that contains all
* array that contains all of the non-null elements. * of the non-null elements.
* *
* If there are no nulls in the original array then the original * If there are no nulls in the original array then the original array is returned.
* array is returned.
*/ */
public static Object[] removeNulls(Class<?> c, Object[] array) { public static Object[] removeNulls(Class<?> c, Object[] array) {
if (array == null) if (array == null)
return (Object[]) Array.newInstance(c, 0); return (Object[]) Array.newInstance(c, 0);
int i; int i;
int validEntries = 0; int validEntries = 0;
for (i = 0; i < array.length; i++) { for (i = 0; i < array.length; i++) {
if (array[i] != null) validEntries++; if (array[i] != null)
validEntries++;
} }
if (array.length == validEntries) if (array.length == validEntries)
return array; return array;
Object[] newArray = (Object[]) Array.newInstance(c, validEntries); Object[] newArray = (Object[]) Array.newInstance(c, validEntries);
int j = 0; int j = 0;
for (i = 0; i < array.length; i++) { for (i = 0; i < array.length; i++) {
if (array[i] != null) newArray[j++] = array[i]; if (array[i] != null)
newArray[j++] = array[i];
} }
return newArray;
}
/**
* Note that this should only be used when the placement of nulls within the array
* is unknown (due to performance efficiency).
*
* Removes all of the nulls from the array and returns a new array that contains all
* of the non-null elements.
*
* If there are no nulls in the original array then the original array is returned.
* @since 5.2
*/
@SuppressWarnings("unchecked")
public static <T> T[] removeNulls(T[] array) {
int i;
int validEntries = 0;
for (i = 0; i < array.length; i++) {
if (array[i] != null)
validEntries++;
}
if (array.length == validEntries)
return array;
T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(), validEntries);
int j = 0;
for (i = 0; i < array.length; i++) {
if (array[i] != null)
newArray[j++] = array[i];
}
return newArray; return newArray;
} }
@ -390,7 +421,7 @@ public abstract class ArrayUtil {
final int newLen= index+1; final int newLen= index+1;
if (array != null && array.length == newLen) if (array != null && array.length == newLen)
return array; return array;
Object[] newArray = (Object[]) Array.newInstance(c, newLen); Object[] newArray = (Object[]) Array.newInstance(c, newLen);
if (array != null && newLen > 0) if (array != null && newLen > 0)
System.arraycopy(array, 0, newArray, 0, newLen); System.arraycopy(array, 0, newArray, 0, newLen);
@ -407,7 +438,7 @@ public abstract class ArrayUtil {
} }
/** /**
* Insert the obj at the beginning of the array, shifting the whole thing one index * Inserts the obj at the beginning of the array, shifting the whole thing one index
* Assumes that array contains nulls at the end, only. * Assumes that array contains nulls at the end, only.
*/ */
public static Object[] prepend(Class<?> c, Object[] array, Object obj) { public static Object[] prepend(Class<?> c, Object[] array, Object obj) {
@ -418,7 +449,7 @@ public abstract class ArrayUtil {
array[0] = obj; array[0] = obj;
return array; return array;
} }
int i = findFirstNull(array); int i = findFirstNull(array);
if (i >= 0) { if (i >= 0) {
System.arraycopy(array, 0, array, 1, i); System.arraycopy(array, 0, array, 1, i);
@ -429,7 +460,7 @@ public abstract class ArrayUtil {
temp[0] = obj; temp[0] = obj;
array = temp; array = temp;
} }
return array; return array;
} }
@ -448,14 +479,14 @@ public abstract class ArrayUtil {
} }
} }
} }
static public int[] setInt(int[] array, int idx, int val) { static public int[] setInt(int[] array, int idx, int val) {
if (array == null) { if (array == null) {
array = new int[ DEFAULT_LENGTH > idx + 1 ? DEFAULT_LENGTH : idx + 1]; array = new int[DEFAULT_LENGTH > idx + 1 ? DEFAULT_LENGTH : idx + 1];
array[idx] = val; array[idx] = val;
return array; return array;
} }
if (array.length <= idx) { if (array.length <= idx) {
int newLen = array.length * 2; int newLen = array.length * 2;
while (newLen <= idx) while (newLen <= idx)
@ -468,7 +499,7 @@ public abstract class ArrayUtil {
array[idx] = val; array[idx] = val;
return array; return array;
} }
/** /**
* Stores the specified array contents in a new array of specified * Stores the specified array contents in a new array of specified
* runtime type. * runtime type.
@ -478,7 +509,7 @@ public abstract class ArrayUtil {
* specified runtime type, or null if source is null. * specified runtime type, or null if source is null.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <S,T> T[] convert(Class<T> target, S[] source) { public static <S, T> T[] convert(Class<T> target, S[] source) {
T[] result= null; T[] result= null;
if (source != null) { if (source != null) {
result= (T[]) Array.newInstance(target, source.length); result= (T[]) Array.newInstance(target, source.length);
@ -488,8 +519,7 @@ public abstract class ArrayUtil {
} }
return result; return result;
} }
/** /**
* Returns a new array that contains all of the elements of the * Returns a new array that contains all of the elements of the
* given array except the first one. * given array except the first one.
@ -501,12 +531,12 @@ public abstract class ArrayUtil {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T[] removeFirst(T[] args) { public static <T> T[] removeFirst(T[] args) {
int n = args.length; int n = args.length;
if(n <= 0) if (n <= 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
T[] newArgs = (T[]) Array.newInstance(args.getClass().getComponentType(), n-1); T[] newArgs = (T[]) Array.newInstance(args.getClass().getComponentType(), n-1);
for(int i = 1; i < n; i++) { for (int i = 1; i < n; i++) {
newArgs[i-1] = args[i]; newArgs[i - 1] = args[i];
} }
return newArgs; return newArgs;
} }