Package org.eclipse.handly.util
Class ArrayUtil
java.lang.Object
org.eclipse.handly.util.ArrayUtil
Provides static utility methods for manipulating arrays. These utilities
supplement those found in
Arrays
.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T,
C extends Collection<? super T>>
CcollectMatching
(T[] a, Predicate<? super T> p, C c) Appends all of the elements in the given array that match the given predicate to the end of the given collection, in the order they follow in the array.static <T> List<T>
concat
(T[] a, Collection<? extends T> b) Returns a list that represents the concatenation of the given collection to the end of the given array.static boolean
Returns whether the given array contains the given element.static <T> boolean
containsMatching
(T[] a, Predicate<? super T> p) Returns whether the given array contains an element matching the given predicate.static <T> List<T>
elementsOfType
(Object[] a, Class<? extends T>... types) Returns a list of all of the elements in the given array that have any of the given types, in the order the elements follow in the given array.static boolean
hasElementsNotOfType
(Object[] a, Class<?>... types) Returns whether the given array contains an element that has none of the given types.static boolean
hasElementsOfType
(Object[] a, Class<?>... types) Returns whether the given array contains an element that has any of the given types.static boolean
hasOnlyElementsOfType
(Object[] a, Class<?>... types) Returns whether all of the elements in the given array have any of the given types.static int
Returns the index of the first occurrence of the given element in the given array, or -1 if the array does not contain the element.static <T> int
indexOfMatching
(T[] a, Predicate<? super T> p) Returns the index of the first occurrence of an element matching the given predicate in the given array, or -1 if the array does not contain an element matching the predicate.static int
lastIndexOf
(Object[] a, Object o) Returns the index of the last occurrence of the given element in the given array, or -1 if the array does not contain the element.static <T> int
lastIndexOfMatching
(T[] a, Predicate<? super T> p) Returns the index of the last occurrence of an element matching the given predicate in the given array, or -1 if the array does not contain an element matching the predicate.static <T> Set<T>
setMinus
(T[] a, Collection<?> b) Returns a set of elements that are present in the given array but are absent in the given collection.static <T> Set<T>
union
(T[] a, Collection<? extends T> b) Returns a set of elements that are present in either the given array or the given collection.
-
Method Details
-
contains
Returns whether the given array contains the given element. More formally, returnstrue
iff the array contains at least one elemente
such thatObjects.equals(e, o)
.- Parameters:
a
- the array (notnull
)o
- the element whose presence in the given array is to be tested (may benull
)- Returns:
true
if the given array contains the given element, andfalse
otherwise
-
indexOf
Returns the index of the first occurrence of the given element in the given array, or -1 if the array does not contain the element. More formally, returns the lowest indexi
such thatObjects.equals(o, a[i])
, or -1 if there is no such index.- Parameters:
a
- the array (notnull
)o
- the element to search for in the given array (may benull
)- Returns:
- the index of the first occurrence of the given element in the given array, or -1 if the array does not contain the element
-
lastIndexOf
Returns the index of the last occurrence of the given element in the given array, or -1 if the array does not contain the element. More formally, returns the highest indexi
such thatObjects.equals(o, a[i])
, or -1 if there is no such index.- Parameters:
a
- the array (notnull
)o
- the element to search for in the given array (may benull
)- Returns:
- the index of the last occurrence of the given element in the given array, or -1 if the array does not contain the element
-
containsMatching
Returns whether the given array contains an element matching the given predicate. More formally, returnstrue
iff the array contains at least one elemente
such thatp.test(e)
.- Parameters:
a
- the array (notnull
)p
- the predicate to match (notnull
)- Returns:
true
if the given array contains an element matching the given predicate, andfalse
otherwise
-
indexOfMatching
Returns the index of the first occurrence of an element matching the given predicate in the given array, or -1 if the array does not contain an element matching the predicate. More formally, returns the lowest indexi
such thatp.test(a[i])
, or -1 if there is no such index.- Parameters:
a
- the array (notnull
)p
- the predicate to match (notnull
)- Returns:
- the index of the first occurrence of an element matching the given predicate in the given array, or -1 if the array does not contain an element matching the predicate
-
lastIndexOfMatching
Returns the index of the last occurrence of an element matching the given predicate in the given array, or -1 if the array does not contain an element matching the predicate. More formally, returns the highest indexi
such thatp.test(a[i])
, or -1 if there is no such index.- Parameters:
a
- the array (notnull
)p
- the predicate to match (notnull
)- Returns:
- the index of the last occurrence of an element matching the given predicate in the given array, or -1 if the array does not contain an element matching the predicate
-
collectMatching
public static <T,C extends Collection<? super T>> C collectMatching(T[] a, Predicate<? super T> p, C c) Appends all of the elements in the given array that match the given predicate to the end of the given collection, in the order they follow in the array.- Parameters:
a
- the array (notnull
)p
- the predicate to match (notnull
)c
- the collection to add matching elements to (notnull
)- Returns:
- the given collection instance,
c
-
elementsOfType
Returns a list of all of the elements in the given array that have any of the given types, in the order the elements follow in the given array. Clients are free to modify the returned list.- Parameters:
a
- the array (notnull
)types
- at least one type (each type notnull
)- Returns:
- a list of all of the elements in the given array that have
any of the given types (never
null
)
-
hasElementsOfType
Returns whether the given array contains an element that has any of the given types.- Parameters:
a
- the array (notnull
)types
- at least one type (each type notnull
)- Returns:
true
if the given array contains an element that has any of the given types, andfalse
otherwise
-
hasElementsNotOfType
Returns whether the given array contains an element that has none of the given types.- Parameters:
a
- the array (notnull
)types
- at least one type (each type notnull
)- Returns:
true
if the given array contains an element that has none of the given types, andfalse
otherwise
-
hasOnlyElementsOfType
Returns whether all of the elements in the given array have any of the given types.- Parameters:
a
- the array (notnull
)types
- at least one type (each type notnull
)- Returns:
true
if all of the elements in the given array have any of the given types, andfalse
otherwise
-
concat
Returns a list that represents the concatenation of the given collection to the end of the given array. Clients are free to modify the returned list.- Parameters:
a
- the array (notnull
)b
- the collection that is concatenated to the end of the given array (notnull
)- Returns:
- a list that represents the concatenation (never
null
)
-
union
Returns a set of elements that are present in either the given array or the given collection. Effectively, models the mathematical set-theoretic union operation. The returned set has predictable iteration order determined by those of the given array and the given collection. Clients are free to modify the returned set.- Parameters:
a
- the array to "add" elements to (notnull
)b
- the collection of elements to "add" (notnull
)- Returns:
- a set of elements that are present in either the given array
or the given collection (never
null
)
-
setMinus
Returns a set of elements that are present in the given array but are absent in the given collection. Effectively, models the mathematical set-theoretic difference operation. The returned set has predictable iteration order determined by that of the given array. Clients are free to modify the returned set.- Parameters:
a
- the array to "subtract" elements from (notnull
)b
- the collection of elements to "subtract" (notnull
)- Returns:
- a set of elements that are present in the given array but are
absent in the given collection (never
null
)
-