Class CollectionUtils
- java.lang.Object
-
- com.igormaznitsa.jcp.utils.antpathmatcher.CollectionUtils
-
abstract class CollectionUtils extends java.lang.Object
Miscellaneous collection utility methods. Mainly for internal use within the framework.- Since:
- 1.1.3
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
CollectionUtils.EnumerationIterator<E>
Iterator wrapping an Enumeration.
-
Constructor Summary
Constructors Constructor Description CollectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static boolean
arrayEquals(java.lang.Object o1, java.lang.Object o2)
static java.util.List
arrayToList(java.lang.Object source)
Convert the supplied array into a List.static boolean
contains(java.util.Enumeration<?> enumeration, java.lang.Object element)
Check whether the given Enumeration contains the given element.static boolean
contains(java.util.Iterator<?> iterator, java.lang.Object element)
Check whether the given Iterator contains the given element.static boolean
containsAny(java.util.Collection<?> source, java.util.Collection<?> candidates)
Returntrue
if any element in 'candidates
' is contained in 'source
'; otherwise returnsfalse
.static boolean
containsInstance(java.util.Collection<?> collection, java.lang.Object element)
Check whether the given Collection contains the given element instance.static java.lang.Class<?>
findCommonElementType(java.util.Collection<?> collection)
Find the common element type of the given Collection, if any.static <E> E
findFirstMatch(java.util.Collection<?> source, java.util.Collection<E> candidates)
Return the first element in 'candidates
' that is contained in 'source
'.static java.lang.Object
findValueOfType(java.util.Collection<?> collection, java.lang.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 <T> T
findValueOfType(java.util.Collection<?> collection, java.lang.Class<T> type)
Find a single value of the given type in the given Collection.static boolean
hasUniqueObject(java.util.Collection<?> collection)
Determine whether the given Collection only contains a single unique object.static boolean
isEmpty(java.lang.Object[] array)
static boolean
isEmpty(java.util.Collection<?> collection)
Returntrue
if the supplied Collection isnull
or empty.static boolean
isEmpty(java.util.Map<?,?> map)
Returntrue
if the supplied Map isnull
or empty.static <E> void
mergeArrayIntoCollection(java.lang.Object array, java.util.Collection<E> collection)
Merge the given array into the given Collection.static <K,V>
voidmergePropertiesIntoMap(java.util.Properties props, java.util.Map<K,V> map)
Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.static boolean
nullSafeEquals(java.lang.Object o1, java.lang.Object o2)
static <A,E extends A>
A[]toArray(java.util.Enumeration<E> enumeration, A[] array)
Marshal the elements from the given enumeration into an array of the given type.static <E> java.util.Iterator<E>
toIterator(java.util.Enumeration<E> enumeration)
Adapt an enumeration to an iterator.static java.lang.Object[]
toObjectArray(java.lang.Object source)
-
-
-
Method Detail
-
isEmpty
public static boolean isEmpty(java.util.Collection<?> collection)
Returntrue
if the supplied Collection isnull
or empty. Otherwise, returnfalse
.- Parameters:
collection
- the Collection to check- Returns:
- whether the given Collection is empty
-
isEmpty
public static boolean isEmpty(java.util.Map<?,?> map)
Returntrue
if the supplied Map isnull
or empty. Otherwise, returnfalse
.- Parameters:
map
- the Map to check- Returns:
- whether the given Map is empty
-
arrayToList
public static java.util.List arrayToList(java.lang.Object source)
Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.NOTE: Generally prefer the standard
Arrays.asList(T...)
method. ThisarrayToList
method is just meant to deal with an incoming Object value that might be anObject[]
or a primitive array at runtime.A
null
source value will be converted to an empty List.- Parameters:
source
- the (potentially primitive) array- Returns:
- the converted List result
- See Also:
ObjectUtils.toObjectArray(Object)
,Arrays.asList(Object[])
-
toObjectArray
public static java.lang.Object[] toObjectArray(java.lang.Object source)
-
mergeArrayIntoCollection
public static <E> void mergeArrayIntoCollection(java.lang.Object array, java.util.Collection<E> collection)
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
public static <K,V> void mergePropertiesIntoMap(java.util.Properties props, java.util.Map<K,V> map)
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
public static boolean contains(java.util.Iterator<?> iterator, java.lang.Object element)
Check whether the given Iterator contains the given element.- Parameters:
iterator
- the Iterator to checkelement
- the element to look for- Returns:
true
if found,false
else
-
nullSafeEquals
public static boolean nullSafeEquals(java.lang.Object o1, java.lang.Object o2)
-
arrayEquals
private static boolean arrayEquals(java.lang.Object o1, java.lang.Object o2)
-
contains
public static boolean contains(java.util.Enumeration<?> enumeration, java.lang.Object element)
Check whether the given Enumeration contains the given element.- Parameters:
enumeration
- the Enumeration to checkelement
- the element to look for- Returns:
true
if found,false
else
-
containsInstance
public static boolean containsInstance(java.util.Collection<?> collection, java.lang.Object element)
Check whether the given Collection contains the given element instance.Enforces the given instance to be present, rather than returning
true
for an equal element as well.- Parameters:
collection
- the Collection to checkelement
- the element to look for- Returns:
true
if found,false
else
-
containsAny
public static boolean containsAny(java.util.Collection<?> source, java.util.Collection<?> candidates)
Returntrue
if 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
public static <E> E findFirstMatch(java.util.Collection<?> source, java.util.Collection<E> candidates)
Return the first element in 'candidates
' that is contained in 'source
'. If no element in 'candidates
' is present in 'source
' returnsnull
. Iteration order isCollection
implementation specific.- Parameters:
source
- the source Collectioncandidates
- the candidates to search for- Returns:
- the first present object, or
null
if not found
-
findValueOfType
public static <T> T findValueOfType(java.util.Collection<?> collection, java.lang.Class<T> type)
Find a single value of the given type in the given Collection.- Parameters:
collection
- the Collection to searchtype
- the type to look for- Returns:
- a value of the given type found if there is a clear match, or
null
if none or more than one such value found
-
isEmpty
public static boolean isEmpty(java.lang.Object[] array)
-
findValueOfType
public static java.lang.Object findValueOfType(java.util.Collection<?> collection, java.lang.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.- 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
null
if none or more than one such value found
-
hasUniqueObject
public static boolean hasUniqueObject(java.util.Collection<?> collection)
Determine whether the given Collection only contains a single unique object.- Parameters:
collection
- the Collection to check- Returns:
true
if the collection contains a single reference or multiple references to the same instance,false
else
-
findCommonElementType
public static java.lang.Class<?> findCommonElementType(java.util.Collection<?> collection)
Find the common element type of the given Collection, if any.- Parameters:
collection
- the Collection to check- Returns:
- the common element type, or
null
if no clear common type has been found (or the collection was empty)
-
toArray
public static <A,E extends A> A[] toArray(java.util.Enumeration<E> enumeration, A[] array)
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.
-
toIterator
public static <E> java.util.Iterator<E> toIterator(java.util.Enumeration<E> enumeration)
Adapt an enumeration to an iterator.- Parameters:
enumeration
- the enumeration- Returns:
- the iterator
-
-