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

@ -69,7 +69,6 @@ public abstract class ArrayUtil {
return haveNull ? right + 1 : -1;
}
/**
* Assumes that array contains nulls at the end, only.
* Appends object using the current length of the array.
@ -293,7 +292,7 @@ public abstract class ArrayUtil {
* @return true if the specified array contains the specified object, or the specified array is null
*/
public static boolean contains(Object[] array, Object obj) {
return indexOf(array, obj)!=-1;
return indexOf(array, obj) >= 0;
}
/**
@ -306,7 +305,7 @@ public abstract class ArrayUtil {
*/
public static int indexOf(Object[] array, Object obj) {
int result = -1;
if (array!=null) {
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (array[i] == obj)
return i;
@ -338,7 +337,7 @@ public abstract class ArrayUtil {
*/
public static int indexOfEqual(Object[] comments, Object comment) {
int result = -1;
if (comments!=null) {
if (comments != null) {
for (int i= 0; (i < comments.length) && (comments[i] != null); i++) {
if (comments[i].equals(comment))
return i;
@ -347,16 +346,14 @@ public abstract class ArrayUtil {
return result;
}
/**
* Note that this should only be used when the placement of
* nulls within the array is unknown (due to performance efficiency).
* 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.
* 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.
* If there are no nulls in the original array then the original array is returned.
*/
public static Object[] removeNulls(Class<?> c, Object[] array) {
if (array == null)
@ -365,7 +362,8 @@ public abstract class ArrayUtil {
int i;
int validEntries = 0;
for (i = 0; i < array.length; i++) {
if (array[i] != null) validEntries++;
if (array[i] != null)
validEntries++;
}
if (array.length == validEntries)
@ -374,7 +372,40 @@ public abstract class ArrayUtil {
Object[] newArray = (Object[]) Array.newInstance(c, validEntries);
int j = 0;
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;
@ -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.
*/
public static Object[] prepend(Class<?> c, Object[] array, Object obj) {
@ -451,7 +482,7 @@ public abstract class ArrayUtil {
static public int[] setInt(int[] array, int idx, int val) {
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;
return array;
}
@ -478,7 +509,7 @@ public abstract class ArrayUtil {
* specified runtime type, or null if source is null.
*/
@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;
if (source != null) {
result= (T[]) Array.newInstance(target, source.length);
@ -489,7 +520,6 @@ public abstract class ArrayUtil {
return result;
}
/**
* Returns a new array that contains all of the elements of the
* given array except the first one.
@ -501,12 +531,12 @@ public abstract class ArrayUtil {
@SuppressWarnings("unchecked")
public static <T> T[] removeFirst(T[] args) {
int n = args.length;
if(n <= 0)
if (n <= 0)
throw new IllegalArgumentException();
T[] newArgs = (T[]) Array.newInstance(args.getClass().getComponentType(), n-1);
for(int i = 1; i < n; i++) {
newArgs[i-1] = args[i];
for (int i = 1; i < n; i++) {
newArgs[i - 1] = args[i];
}
return newArgs;
}