Package com.okta.commons.lang
Class Collections
java.lang.Object
com.okta.commons.lang.Collections
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ListarrayToList(Object source) Convert the supplied array into a List.static booleancontains(Enumeration enumeration, Object element) Check whether the given Enumeration contains the given element.static booleanCheck whether the given Iterator contains the given element.static booleancontainsAny(Collection source, Collection candidates) Returntrueif any element in 'candidates' is contained in 'source'; otherwise returnsfalse.static booleancontainsInstance(Collection collection, Object element) Check whether the given Collection contains the given element instance.static Class<?> findCommonElementType(Collection collection) Find the common element type of the given Collection, if any.static ObjectfindFirstMatch(Collection source, Collection candidates) Return the first element in 'candidates' that is contained in 'source'.static <T> TfindFirstValueOfType(Collection<?> collection, Class<T> type) Find the first single value of the given type in the given Collection ornullif none was foundstatic ObjectfindValueOfType(Collection<?> collection, Class<?>[] types) Find a single value of one of the given types in the given Collection: searching the Collection for a value of the first type, then searching for a value of the second type, etc.static booleanhasUniqueObject(Collection collection) Determine whether the given Collection only contains a single unique object.static booleanisEmpty(Collection collection) Returntrueif the supplied Collection isnullor empty.static booleanReturntrueif the supplied Map isnullor empty.static voidmergeArrayIntoCollection(Object array, Collection collection) Merge the given array into the given Collection.static voidmergePropertiesIntoMap(Properties props, Map map) Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.static intsize(Collection collection) Returns the collection's size or0if the collection isnull.static intReturns the map's size or0if the map isnull.static <T> T[]toArray(Collection<T> c, Class<T> type) static <A,E extends A>
A[]toArray(Enumeration<E> enumeration, A[] array) Marshal the elements from the given enumeration into an array of the given type.static <E> Iterator<E> toIterator(Enumeration<E> enumeration) Adapt an enumeration to an iterator.static <T> List<T> toList(Collection<T> elements) a new List that contains the specified elements or an empty collection if the elements are null or empty.static <T> List<T> toList(T... elements) a new List that contains the specified elements or an empty collection if the elements are null or empty.static <E> Set<E> toSet(E... elements) Returns a newSetthat contains the specified elements or an empty Set if the elements are null or empty.
-
Constructor Details
-
Collections
public Collections()
-
-
Method Details
-
isEmpty
Returntrueif the supplied Collection isnullor empty. Otherwise, returnfalse.- Parameters:
collection- the Collection to check- Returns:
- whether the given Collection is empty
-
size
Returns the collection's size or0if the collection isnull.- Parameters:
collection- the collection to check.- Returns:
- the collection's size or
0if the collection isnull.
-
size
Returns the map's size or0if the map isnull.- Parameters:
map- the map to check- Returns:
- the map's size or
0if the map isnull.
-
isEmpty
Returntrueif the supplied Map isnullor empty. Otherwise, returnfalse.- Parameters:
map- the Map to check- Returns:
- whether the given Map is empty
-
arrayToList
Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.A
nullsource value will be converted to an empty List.- Parameters:
source- the (potentially primitive) array- Returns:
- the converted List result
- See Also:
-
toList
a new List that contains the specified elements or an empty collection if the elements are null or empty.- Type Parameters:
T- the type of elements in the collection- Parameters:
elements- the elements to put in the list.- Returns:
- a new List that contains the specified elements or an empty collection if the elements are null or empty.
-
toList
a new List that contains the specified elements or an empty collection if the elements are null or empty.- Type Parameters:
T- the type of elements in the collection- Parameters:
elements- the elements to put in the list.- Returns:
- a new List that contains the specified elements or an empty collection if the elements are null or empty.
-
toSet
Returns a newSetthat contains the specified elements or an empty Set if the elements are null or empty.- Type Parameters:
E- the type of elements in the set- Parameters:
elements- elements to add to the new set- Returns:
- a new
Setthat contains the specified elements or an empty Set if the elements are null or empty.
-
mergeArrayIntoCollection
Merge the given array into the given Collection.- Parameters:
array- the array to merge (may benull)collection- the target Collection to merge the array into
-
mergePropertiesIntoMap
Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.Uses
Properties.propertyNames()to even catch default properties linked into the original Properties instance.- Parameters:
props- the Properties instance to merge (may benull)map- the target Map to merge the properties into
-
contains
Check whether the given Iterator contains the given element.- Parameters:
iterator- the Iterator to checkelement- the element to look for- Returns:
trueif found,falseelse
-
contains
Check whether the given Enumeration contains the given element.- Parameters:
enumeration- the Enumeration to checkelement- the element to look for- Returns:
trueif found,falseelse
-
containsInstance
Check whether the given Collection contains the given element instance.Enforces the given instance to be present, rather than returning
truefor an equal element as well.- Parameters:
collection- the Collection to checkelement- the element to look for- Returns:
trueif found,falseelse
-
containsAny
Returntrueif any element in 'candidates' is contained in 'source'; otherwise returnsfalse.- Parameters:
source- the source Collectioncandidates- the candidates to search for- Returns:
- whether any of the candidates has been found
-
findFirstMatch
Return the first element in 'candidates' that is contained in 'source'. If no element in 'candidates' is present in 'source' returnsnull. Iteration order isCollectionimplementation specific.- Parameters:
source- the source Collectioncandidates- the candidates to search for- Returns:
- the first present object, or
nullif not found
-
findFirstValueOfType
Find the first single value of the given type in the given Collection ornullif none was found- Type Parameters:
T- type type of object to find- Parameters:
collection- the Collection to searchtype- the type to look for- Returns:
- a value of the given type found or
nullif none found.
-
findValueOfType
Find a single value of one of the given types in the given Collection: searching the Collection for a value of the first type, then searching for a value of the second type, etc.- Parameters:
collection- the collection to searchtypes- the types to look for, in prioritized order- Returns:
- a value of one of the given types found if there is a clear match,
or
nullif none or more than one such value found
-
hasUniqueObject
Determine whether the given Collection only contains a single unique object.- Parameters:
collection- the Collection to check- Returns:
trueif the collection contains a single reference or multiple references to the same instance,falseelse
-
findCommonElementType
Find the common element type of the given Collection, if any.- Parameters:
collection- the Collection to check- Returns:
- the common element type, or
nullif no clear common type has been found (or the collection was empty)
-
toArray
Marshal the elements from the given enumeration into an array of the given type. Enumeration elements must be assignable to the type of the given array. The array returned will be a different instance than the array given.- Type Parameters:
A- type of arrayE- type of enumeration- Parameters:
enumeration- source enumerationarray- the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array with the contents of the enumeration
-
toArray
-
toIterator
Adapt an enumeration to an iterator.- Type Parameters:
E- type of enumeration- Parameters:
enumeration- the enumeration- Returns:
- the iterator
-