Package org.identityconnectors.common
Class CollectionUtil
- java.lang.Object
-
- org.identityconnectors.common.CollectionUtil
-
public final class CollectionUtil extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> List<T>
asReadOnlyList(List<T> list)
Returns a read-only list.static <T,K>
Map<T,K>asReadOnlyMap(Map<T,K> map)
static boolean
equals(Object o1, Object o2)
Equality function that properly handles arrays, lists, maps, lists of arrays, and maps of arrays.static <T> int
forceCompare(Object o1, Object o2)
Forces the compare of two comparable objects and removes any warnings generated by the compiler.static int
hashCode(Object o)
hashCode function that properly handles arrays, collections, maps, collections of arrays, and maps of arrays.static <T> Set<T>
intersection(Collection<T> c1, Collection<T> c2)
Returns the intersection of twoCollection
s as an unmodifiable set.static boolean
isCaseInsensitiveMap(Map<?,?> map)
Returns true if the given map is a case-insensitive map.static boolean
isCaseInsensitiveSet(Set<?> set)
Returns true if the given set is a case-insensitive set.static <T> boolean
isEmpty(Collection<T> c)
Determine ifCollection
is empty or not, protects against null being passed in.static <T> SortedMap<String,T>
newCaseInsensitiveMap()
Creates a case-insensitive map.static SortedSet<String>
newCaseInsensitiveSet()
Creates a case-insensitive set.static <T> List<T>
newList(Collection<? extends T> c)
Create a modifiable list from theCollection
provided.static <T> List<T>
newList(T... arr)
Create a modifiable list from the arguments.static Map<String,String>
newMap(Properties properties)
static <T,K>
Map<T,K>newMap(T[] k, K[] v)
Builds a map from two arrays.static <T,K>
Map<T,K>newMap(T k0, K v0)
static <T,K>
Map<T,K>newMap(T k0, K v0, T k1, K v1)
static <T,K>
Map<T,K>newMap(T k0, K v0, T k1, K v1, T k2, K v2)
static <T,K>
Map<T,K>newMap(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3)
static <T,K>
Map<T,K>newMap(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4)
static <T,K>
Map<T,K>newMap(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4, T k5, K v5)
static <T,K>
Map<T,K>newMapFromLists(List<T> keys, List<K> values)
Converts twoList
to a map.static <T> List<T>
newReadOnlyList(Collection<? extends T> c)
Creates an unmodifiableList
from a collection.static <T> List<T>
newReadOnlyList(List<? extends T> list)
static <T> List<T>
newReadOnlyList(T... obj)
Creates an unmodifiableList
from a variable number arguments.static <T,K>
Map<T,K>newReadOnlyMap(Map<T,K> map)
static <T> Map<T,T>
newReadOnlyMap(T[][] kv)
static <T> Set<T>
newReadOnlySet(Collection<T> c)
Creates an unmodifiable set from aCollection
.static <T> Set<T>
newReadOnlySet(T... arr)
Creates an unmodifiable set from a variable number arguments.static <T> Set<T>
newSet(Collection<T> c)
Creates a set that can be modified from theCollection
provided.static <T> Set<T>
newSet(T... arr)
Creates a set that can be modified from the arguments.static <T extends Object & Comparable<? super T>>
List<T>newSortedList(Collection<? extends T> col)
Create a modifiable sortedList
based on theCollection
provided.static <T> Collection<T>
nullAsEmpty(Collection<T> c)
Protects fromnull
and returns a new instance ofHashSet
.static <T> List<T>
nullAsEmpty(List<T> list)
static <T,K>
Map<T,K>nullAsEmpty(Map<T,K> map)
static <T> Set<T>
nullAsEmpty(Set<T> set)
static <T> Set<T>
union(Collection<T> c1, Collection<T> c2)
Returns the union of twoCollection
s as an unmodifiable set.static <T> Collection<T>
unique(Collection<T> c)
UseHashSet
to create a uniqueCollection
based on the one passed in.
-
-
-
Method Detail
-
newCaseInsensitiveSet
public static SortedSet<String> newCaseInsensitiveSet()
Creates a case-insensitive set.- Returns:
- An empty case-insensitive set
-
isCaseInsensitiveSet
public static boolean isCaseInsensitiveSet(Set<?> set)
Returns true if the given set is a case-insensitive set.- Parameters:
set
- The set. May be null.- Returns:
- true if the given set is a case-insensitive set
-
newCaseInsensitiveMap
public static <T> SortedMap<String,T> newCaseInsensitiveMap()
Creates a case-insensitive map.- Type Parameters:
T
- The object type of the map- Returns:
- An empty case-insensitive map
-
isCaseInsensitiveMap
public static boolean isCaseInsensitiveMap(Map<?,?> map)
Returns true if the given map is a case-insensitive map.- Parameters:
map
- The map. May be null.- Returns:
- true if the given map is a case-insensitive map
-
nullAsEmpty
public static <T> Collection<T> nullAsEmpty(Collection<T> c)
Protects fromnull
and returns a new instance ofHashSet
. if the parameter c is null.- Type Parameters:
T
- the type of the collection- Parameters:
c
- collection to check- Returns:
- if null new
HashSet
otherwise the parameter that was passed in or
-
nullAsEmpty
public static <T,K> Map<T,K> nullAsEmpty(Map<T,K> map)
Protects fromnull
and returns a new instance ofHashMap
if the parametermap
isnull
. Otherwise return the parameter that was passed in.
-
nullAsEmpty
public static <T> Set<T> nullAsEmpty(Set<T> set)
Protects fromnull
and returns a new instance ofHashSet
if the parameterset
isnull
. Otherwise return the parameter that was passed in.
-
nullAsEmpty
public static <T> List<T> nullAsEmpty(List<T> list)
Protects fromnull
and returns a new instance ofArrayList
if the parameterlist
isnull
. Otherwise return the parameter that was passed in.
-
isEmpty
public static <T> boolean isEmpty(Collection<T> c)
Determine ifCollection
is empty or not, protects against null being passed in.
-
unique
public static <T> Collection<T> unique(Collection<T> c)
UseHashSet
to create a uniqueCollection
based on the one passed in. The method protects against null. The returnedCollection
is unmodifiable.
-
newReadOnlyMap
public static <T> Map<T,T> newReadOnlyMap(T[][] kv)
-
newMapFromLists
public static <T,K> Map<T,K> newMapFromLists(List<T> keys, List<K> values)
Converts twoList
to a map. The order is important here because each key will map to one value.
-
newMap
public static Map<String,String> newMap(Properties properties)
-
newMap
public static <T,K> Map<T,K> newMap(T k0, K v0)
-
newMap
public static <T,K> Map<T,K> newMap(T k0, K v0, T k1, K v1)
-
newMap
public static <T,K> Map<T,K> newMap(T k0, K v0, T k1, K v1, T k2, K v2)
-
newMap
public static <T,K> Map<T,K> newMap(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3)
-
newMap
public static <T,K> Map<T,K> newMap(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4)
-
newMap
public static <T,K> Map<T,K> newMap(T k0, K v0, T k1, K v1, T k2, K v2, T k3, K v3, T k4, K v4, T k5, K v5)
-
newMap
public static <T,K> Map<T,K> newMap(T[] k, K[] v)
Builds a map from two arrays.- Parameters:
k
- Array of keys.v
- Array of values.- Returns:
- a map based on the two arrays.
-
newSet
public static <T> Set<T> newSet(Collection<T> c)
Creates a set that can be modified from theCollection
provided.
-
newSet
@SafeVarargs public static <T> Set<T> newSet(T... arr)
Creates a set that can be modified from the arguments.
-
newReadOnlySet
@SafeVarargs public static <T> Set<T> newReadOnlySet(T... arr)
Creates an unmodifiable set from a variable number arguments.
-
newReadOnlySet
public static <T> Set<T> newReadOnlySet(Collection<T> c)
Creates an unmodifiable set from aCollection
.
-
union
public static <T> Set<T> union(Collection<T> c1, Collection<T> c2)
Returns the union of twoCollection
s as an unmodifiable set.
-
intersection
public static <T> Set<T> intersection(Collection<T> c1, Collection<T> c2)
Returns the intersection of twoCollection
s as an unmodifiable set.
-
newSortedList
public static <T extends Object & Comparable<? super T>> List<T> newSortedList(Collection<? extends T> col)
Create a modifiable sortedList
based on theCollection
provided.
-
newList
public static <T> List<T> newList(Collection<? extends T> c)
Create a modifiable list from theCollection
provided. The return value is backed by anArrayList
.
-
newList
@SafeVarargs public static <T> List<T> newList(T... arr)
Create a modifiable list from the arguments. The return value is backed by anArrayList
.
-
newReadOnlyList
@SafeVarargs public static <T> List<T> newReadOnlyList(T... obj)
Creates an unmodifiableList
from a variable number arguments.
-
newReadOnlyList
public static <T> List<T> newReadOnlyList(Collection<? extends T> c)
Creates an unmodifiableList
from a collection.
-
asReadOnlyList
public static <T> List<T> asReadOnlyList(List<T> list)
Returns a read-only list. The list is backed by the original so no copy is made.- Type Parameters:
T
- The type of the list- Parameters:
list
- The list or null.- Returns:
- A read-only proxy on the original list.
-
forceCompare
public static <T> int forceCompare(Object o1, Object o2)
Forces the compare of two comparable objects and removes any warnings generated by the compiler.- Returns:
Comparable
the integer value of o1.compareTo(o2).
-
hashCode
public static int hashCode(Object o)
hashCode function that properly handles arrays, collections, maps, collections of arrays, and maps of arrays.- Parameters:
o
- The object. May be null.- Returns:
- the hashCode
-
equals
public static boolean equals(Object o1, Object o2)
Equality function that properly handles arrays, lists, maps, lists of arrays, and maps of arrays.NOTE: For Sets, this relies on the equals method of the Set to do the right thing. This is a reasonable assumption since, in order for Sets to behave properly as Sets, their values must already have a proper implementation of equals. (Or they must be specialized Sets that define a custom comparator that knows how to do the right thing). The same holds true for Map keys. Map values, on the other hand, are compared (so Map values can be arrays).
- Parameters:
o1
- The first object. May be null.o2
- The second object. May be null.- Returns:
- true if the two objects are equal.
-
-