Package org.eclipse.cdt.core.parser.util
Class ArrayUtil
- java.lang.Object
-
- org.eclipse.cdt.core.parser.util.ArrayUtil
-
public abstract class ArrayUtil extends Object
- Restriction:
- This class is not intended to be subclassed by clients.
-
-
Constructor Summary
Constructors Constructor Description ArrayUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> T[]
addAll(Class<T> c, T[] dest, Object[] source)
Takes contents of the two arrays up to the firstnull
element and concatenates them.static <T> void
addAll(Collection<? super T> collection, T... elements)
Adds all elements of an array to a collection.static <T> T[]
addAll(T[] dest, Object[] source)
Takes contents of the two arrays up to the firstnull
element and concatenates them.static Object[]
append(Class<?> c, Object[] array, int currentLength, Object obj)
Deprecated.UseappendAt(Class, Object[], int, Object)
instead.static <T> T[]
append(Class<T> c, T[] array, T obj)
Assumes that array containsnull
s at the end, only.static <T> T[]
append(T[] array, T obj)
Assumes that array containsnull
s at the end, only.static <T> T[]
appendAt(Class<T> c, T[] array, int currentLength, T obj)
Assumes that array containsnull
s at the end, only.static <T> T[]
appendAt(T[] array, int currentLength, T obj)
Assumes that array containsnull
s at the end, only.static void
compact(Object[] array)
Moves allnull
elements to the end of the array.static <T> boolean
contains(T[] array, T obj)
Returns whether the specified array contains the specified object.static <T> boolean
containsEqual(T[] array, T obj)
Assumes that array containsnull
s at the end, only.static <S,T>
T[]convert(Class<T> target, S[] source)
Stores the specified array contents in a new array of specified runtime type.static <T> T[]
filter(T[] array, IUnaryPredicate<T> predicate)
Filter the elements of an array down to just the ones that match the given predicate.static <T> int
indexOf(T[] array, T obj)
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 isnull
.static <T> int
indexOfEqual(T[] array, T obj)
Assumes that array containsnull
s at the end, only.static <T> T[]
prepend(Class<T> c, T[] array, T obj)
Inserts theobj
at the beginning of the array, shifting the whole thing one index Assumes that array containsnull
s at the end, only.static <T> T[]
prepend(T[] array, T obj)
Inserts theobj
at the beginning of the array, shifting the whole thing one index Assumes that array containsnull
s at the end, only.static <T> void
remove(T[] array, T element)
Removes first occurrence of element in array and moves objects behind up front.static <T> T[]
removeDuplicates(T[] array)
Returns a new array that contains elements of the given array except duplicates andnull
s.static <T> T[]
removeFirst(T[] array)
Returns a new array that contains all of the elements of the given array except the first one.static <T> T[]
removeNulls(Class<T> c, T[] array)
Removes all of thenull
s from the array and returns a new array that contains all of the non-null
elements.static <T> T[]
removeNulls(T[] array)
Removes all of thenull
s from the array and returns a new array that contains all of the non-null
elements.static Object[]
removeNullsAfter(Class<?> c, Object[] array, int index)
Deprecated.Usetrim(Object[], int)
ortrimAt(Class, Object[], int)
insteadstatic void
reverse(Object[] array)
Reverses order of elements in an array.static void
reverse(Object[] array, int fromIndex, int toIndex)
Reverses order of elements in a subsection of an array.static int[]
setInt(int[] array, int idx, int val)
static <T> T[]
trim(Class<T> c, T[] array)
static <T> T[]
trim(Class<T> c, T[] array, boolean forceNew)
Trims the given array and returns a new array with nonull
entries.static <T> T[]
trim(T[] array)
Trims the given array and returns a new array with nonull
entries.static <T> T[]
trim(T[] array, boolean forceNew)
Trims the given array and returns a new array with nonull
entries.static <T> T[]
trim(T[] array, int newLength)
Trims the given array and returns a new array with nonull
entries.static <T> T[]
trimAt(Class<T> c, T[] array, int index)
To improve performance, this method should be used instead ofremoveNulls(Class, Object[])
when all of the non-null
elements in the array are grouped together at the beginning of the array and all of the s are at the end of the array.
-
-
-
Method Detail
-
append
public static <T> T[] append(Class<T> c, T[] array, T obj)
Assumes that array containsnull
s at the end, only. Appends element after the last non-null
element. If the array isnull
or not large enough, a larger one is allocated, using the given class object.
-
append
public static <T> T[] append(T[] array, T obj)
Assumes that array containsnull
s at the end, only. Appends element after the last non-null
element. If the array is not large enough, a larger one is allocated. Nullarray
is supported for backward compatibility only and only when T is Object.
-
append
@Deprecated public static Object[] append(Class<?> c, Object[] array, int currentLength, Object obj)
Deprecated.UseappendAt(Class, Object[], int, Object)
instead.- Since:
- 4.0
- Restriction:
- This method is not intended to be referenced by clients.
-
appendAt
public static <T> T[] appendAt(Class<T> c, T[] array, int currentLength, T obj)
Assumes that array containsnull
s at the end, only. Appends object using the current length of the array.- Since:
- 5.1
-
appendAt
public static <T> T[] appendAt(T[] array, int currentLength, T obj)
Assumes that array containsnull
s at the end, only. Appends object using the current length of the array.- Parameters:
array
- The array to append to. Notnull
currentLength
- The number of non-null
elements in the arrayobj
- The object to append. Notnull
- Returns:
- The modified array, which may be the same as the first parameter.
- Since:
- 5.4
-
trim
public static <T> T[] trim(Class<T> c, T[] array, boolean forceNew)
Trims the given array and returns a new array with nonull
entries. Assumes thatnull
s can be found at the end, only. ifarray
isnull
, a new array of length 0 is returned ifforceNew
istrue
, a new array will always be created. ifforceNew
isfalse
, a new array will only be created if the original array containednull
entries.- Parameters:
c
- the type of the new arrayarray
- the array to be trimmedforceNew
-
-
trim
public static <T> T[] trim(Class<T> c, T[] array)
-
trim
public static <T> T[] trim(T[] array, boolean forceNew)
Trims the given array and returns a new array with nonull
entries. Assumes thatnull
s can be found at the end, only. ifforceNew
istrue
, a new array will always be created. ifforceNew
isfalse
, a new array will only be created if the original array containednull
entries.- Parameters:
array
- the array to be trimmedforceNew
-- Since:
- 5.2
-
trim
public static <T> T[] trim(T[] array)
Trims the given array and returns a new array with nonull
entries. Assumes thatnull
s can be found at the end, only.- Parameters:
array
- the array to be trimmed- Since:
- 5.2
-
trim
public static <T> T[] trim(T[] array, int newLength)
Trims the given array and returns a new array with nonull
entries. Assumes thatnull
s can be found at the end, only. Similar totrimAt(Class, Object[], int)
, but uses the new length instead of index.- Parameters:
array
- the array to be trimmednewLength
- the new length of the array, has to be less or equal than the current length.- Returns:
- the modified array, which may be the same as the first parameter.
- Since:
- 5.4
-
addAll
public static <T> T[] addAll(Class<T> c, T[] dest, Object[] source)
Takes contents of the two arrays up to the firstnull
element and concatenates them.- Parameters:
c
- The type of the element of the returned array if there was not enough free space in the destination array.dest
- The destination array. The elements of the source array are added to this array if there is enough free space in it. May benull
.source
- The source array. May not benull
.- Returns:
- The concatenated array, which may be the same as the first parameter.
-
addAll
public static <T> T[] addAll(T[] dest, Object[] source)
Takes contents of the two arrays up to the firstnull
element and concatenates them.- Parameters:
dest
- The destination array. The elements of the source array are added to this array if there is enough free space in it. May benull
.source
- The source array. May not benull
.- Returns:
- The concatenated array, which may be the same as the first parameter.
- Since:
- 5.2
-
addAll
@SafeVarargs public static <T> void addAll(Collection<? super T> collection, T... elements)
Adds all elements of an array to a collection. For anArrayList
this method is slightly more efficient thanjava.util.Collections#addAll(Collection, T...)
.- Since:
- 5.4
-
contains
public static <T> boolean contains(T[] array, T obj)
Returns whether the specified array contains the specified object. The comparison is by object identity.- Parameters:
array
- the array to searchobj
- the object to search for- Returns:
true
if the specified array contains the specified object, or the specified array isnull
-
indexOf
public static <T> int indexOf(T[] array, T obj)
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 isnull
. Comparison is by object identity.- Parameters:
array
- the array to searchobj
- the object to search for- 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
-
containsEqual
public static <T> boolean containsEqual(T[] array, T obj)
Assumes that array containsnull
s at the end, only. Returns whether the specified array contains the specified object. Comparison is by object identity.- Parameters:
array
- the array to searchobj
- the object to search for- Returns:
- true if the specified array contains the specified object, or the specified array is
null
-
indexOfEqual
public static <T> int indexOfEqual(T[] array, T obj)
Assumes that array containsnull
s at the end, only. 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 isnull
. Comparison is by equals().- Parameters:
array
- the array to searchobj
- the object to search for- Returns:
- the index into the specified array of the specified object, or -1 if the array does
not contain an equal object, or if the array is
null
-
compact
public static void compact(Object[] array)
Moves allnull
elements to the end of the array. The order of non-null
elements is preserved.- Since:
- 5.4
-
removeNulls
public static <T> T[] removeNulls(Class<T> c, T[] array)
Removes all of thenull
s from the array and returns a new array that contains all of the non-null
elements. If there are nonull
s in the original array then the original array is returned. Note that this method should only be used when the placement ofnull
s within the array is unknown (due to performance efficiency).
-
removeNulls
public static <T> T[] removeNulls(T[] array)
Removes all of thenull
s from the array and returns a new array that contains all of the non-null
elements.If there are no
null
s in the original array then the original array is returned.Note that this method should only be used when the placement of
null
s within the array is unknown (due to performance efficiency).- Since:
- 5.2
-
removeNullsAfter
@Deprecated public static Object[] removeNullsAfter(Class<?> c, Object[] array, int index)
Deprecated.Usetrim(Object[], int)
ortrimAt(Class, Object[], int)
instead- Restriction:
- This method is not intended to be referenced by clients.
-
trimAt
public static <T> T[] trimAt(Class<T> c, T[] array, int index)
To improve performance, this method should be used instead ofremoveNulls(Class, Object[])
when all of the non-null
elements in the array are grouped together at the beginning of the array and all of the s are at the end of the array. The position of the last non-null
element in the array must also be known.If you don't intend to pass
null
array, consider usingtrim(Object[], int)
instead.- Since:
- 5.1
-
prepend
public static <T> T[] prepend(Class<T> c, T[] array, T obj)
Inserts theobj
at the beginning of the array, shifting the whole thing one index Assumes that array containsnull
s at the end, only.
-
prepend
public static <T> T[] prepend(T[] array, T obj)
Inserts theobj
at the beginning of the array, shifting the whole thing one index Assumes that array containsnull
s at the end, only. array must not benull
.- Since:
- 5.2
-
remove
public static <T> void remove(T[] array, T element)
Removes first occurrence of element in array and moves objects behind up front.- Since:
- 4.0
-
convert
public static <S,T> T[] convert(Class<T> target, S[] source)
Stores the specified array contents in a new array of specified runtime type.- Parameters:
target
- the runtime type of the new arraysource
- the source array- Returns:
- the current array stored in a new array with the specified runtime type,
or
null
if source isnull
.
-
reverse
public static void reverse(Object[] array)
Reverses order of elements in an array.- Parameters:
array
- the array- Since:
- 5.4
-
reverse
public static void reverse(Object[] array, int fromIndex, int toIndex)
Reverses order of elements in a subsection of an array.- Parameters:
array
- the arrayfromIndex
- the index of the first affected element (inclusive)toIndex
- the index of the last affected element (exclusive)- Since:
- 5.4
-
removeFirst
public static <T> T[] removeFirst(T[] array)
Returns a new array that contains all of the elements of the given array except the first one.- Throws:
NullPointerException
- ifarray
isnull
IllegalArgumentException
- ifarray
is empty- Since:
- 5.1
-
removeDuplicates
public static <T> T[] removeDuplicates(T[] array)
Returns a new array that contains elements of the given array except duplicates andnull
s. Duplicates are determined byObject.equals(Object)
method.- Throws:
NullPointerException
- ifarray
isnull
- Since:
- 5.5
-
setInt
public static int[] setInt(int[] array, int idx, int val)
-
filter
public static <T> T[] filter(T[] array, IUnaryPredicate<T> predicate)
Filter the elements of an array down to just the ones that match the given predicate.- Since:
- 5.6
-
-