org.springframework.shell.support.util
Class CollectionUtils

java.lang.Object
  extended by org.springframework.shell.support.util.CollectionUtils

public final class CollectionUtils
extends Object

Miscellaneous collection utility methods. Mainly for internal use within the framework.

Since:
1.1.3

Method Summary
static
<T> boolean
addAll(Collection<? extends T> newItems, Collection<T> existingItems)
          Adds the given items to the given collection
static List<?> arrayToList(Object source)
          Convert the supplied array into a List.
static boolean contains(Enumeration<?> enumeration, Object element)
          Check whether the given Enumeration contains the given element.
static boolean contains(Iterator<?> iterator, Object element)
          Check whether the given Iterator contains the given element.
static boolean containsAny(Collection<?> source, Collection<?> candidates)
          Return true if any element in 'candidates' is contained in 'source'; otherwise returns false.
static boolean containsInstance(Collection<?> collection, Object element)
          Check whether the given Collection contains the given element instance.
static
<T> List<T>
filter(Iterable<? extends T> unfiltered, Filter<T> filter)
          Filters (removes elements from) the given Iterable using the given filter.
static Object findFirstMatch(Collection<?> source, Collection<?> candidates)
          Return the first element in 'candidates' that is contained in 'source'.
static Object findValueOfType(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
<T> T
findValueOfType(Collection<?> collection, Class<T> type)
          Find a single value of the given type in the given Collection.
static
<T> T
firstElementOf(Collection<? extends T> collection)
          Returns the first element of the given collection
static boolean hasUniqueObject(Collection<?> collection)
          Determine whether the given Collection only contains a single unique object.
static boolean isEmpty(Collection<?> collection)
          Return true if the supplied Collection is null or empty.
static boolean isEmpty(Map<?,?> map)
          Return true if the supplied Map is null or empty.
static void mergeArrayIntoCollection(Object array, Collection<Object> collection)
          Merge the given array into the given Collection.
static void mergePropertiesIntoMap(Properties props, Map<String,String> map)
          Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.
static
<T> Collection<T>
populate(Collection<T> collection, Collection<? extends T> items)
          Populates the given collection by replacing any existing contents with the given elements, in a null-safe way.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isEmpty

public static boolean isEmpty(Collection<?> collection)
Return true if the supplied Collection is null or empty. Otherwise, return false.

Parameters:
collection - the Collection to check
Returns:
whether the given Collection is empty

isEmpty

public static boolean isEmpty(Map<?,?> map)
Return true if the supplied Map is null or empty. Otherwise, return false.

Parameters:
map - the Map to check
Returns:
whether the given Map is empty

arrayToList

public static List<?> arrayToList(Object source)
Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.

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)

mergeArrayIntoCollection

public static void mergeArrayIntoCollection(Object array,
                                            Collection<Object> collection)
Merge the given array into the given Collection.

Parameters:
array - the array to merge (may be null)
collection - the target Collection to merge the array into

mergePropertiesIntoMap

public static void mergePropertiesIntoMap(Properties props,
                                          Map<String,String> 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 be null)
map - the target Map to merge the properties into

contains

public static boolean contains(Iterator<?> iterator,
                               Object element)
Check whether the given Iterator contains the given element.

Parameters:
iterator - the Iterator to check
element - the element to look for
Returns:
true if found, false else

contains

public static boolean contains(Enumeration<?> enumeration,
                               Object element)
Check whether the given Enumeration contains the given element.

Parameters:
enumeration - the Enumeration to check
element - the element to look for
Returns:
true if found, false else

containsInstance

public static boolean containsInstance(Collection<?> collection,
                                       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 check
element - the element to look for
Returns:
true if found, false else

containsAny

public static boolean containsAny(Collection<?> source,
                                  Collection<?> candidates)
Return true if any element in 'candidates' is contained in 'source'; otherwise returns false.

Parameters:
source - the source Collection
candidates - the candidates to search for
Returns:
whether any of the candidates has been found

findFirstMatch

public static Object findFirstMatch(Collection<?> source,
                                    Collection<?> candidates)
Return the first element in 'candidates' that is contained in 'source'. If no element in 'candidates' is present in 'source' returns null. Iteration order is Collection implementation specific.

Parameters:
source - the source Collection
candidates - the candidates to search for
Returns:
the first present object, or null if not found

findValueOfType

public static <T> T findValueOfType(Collection<?> collection,
                                    Class<T> type)
Find a single value of the given type in the given Collection.

Parameters:
collection - the Collection to search
type - 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

findValueOfType

public static Object findValueOfType(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.

Parameters:
collection - the collection to search
types - 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(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

filter

public static <T> List<T> filter(Iterable<? extends T> unfiltered,
                                 Filter<T> filter)
Filters (removes elements from) the given Iterable using the given filter.

Type Parameters:
T - the type of object being filtered
Parameters:
unfiltered - the iterable to filter; can be null
filter - the filter to apply; can be null for none
Returns:
a non-null list

addAll

public static <T> boolean addAll(Collection<? extends T> newItems,
                                 Collection<T> existingItems)
Adds the given items to the given collection

Type Parameters:
T - the type of item in the collection being updated
Parameters:
newItems - the items being added; can be null for none
existingItems - the items being added to; must be modifiable
Returns:
true if the existing collection was modified
Throws:
UnsupportedOperationException - if there are items to add and the existing collection is not modifiable
Since:
1.2.0

populate

public static <T> Collection<T> populate(Collection<T> collection,
                                         Collection<? extends T> items)
Populates the given collection by replacing any existing contents with the given elements, in a null-safe way.

Type Parameters:
T - the type of element in the collection
Parameters:
collection - the collection to populate (can be null)
items - the items with which to populate the collection (can be null or empty for none)
Returns:
the given collection (useful if it was anonymous)

firstElementOf

public static <T> T firstElementOf(Collection<? extends T> collection)
Returns the first element of the given collection

Type Parameters:
T -
Parameters:
collection -
Returns:
null if the first element is null or the collection is null or empty