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:
parent
68a906b0a6
commit
3b408710f4
1 changed files with 74 additions and 44 deletions
|
@ -69,7 +69,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.
|
||||||
* Appends object using the current length of the array.
|
* 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
|
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
||||||
|
@ -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,16 +346,14 @@ 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)
|
||||||
|
@ -365,7 +362,8 @@ public abstract class ArrayUtil {
|
||||||
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)
|
||||||
|
@ -374,7 +372,40 @@ public abstract class ArrayUtil {
|
||||||
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;
|
||||||
|
@ -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) {
|
||||||
|
@ -451,7 +482,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -489,7 +520,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue