Package org.alfresco.util.collections
Class CollectionUtils
- java.lang.Object
-
- org.alfresco.util.collections.CollectionUtils
-
public abstract class CollectionUtils extends java.lang.Object
- Since:
- 4.0
- Author:
- Nick Smith, Neil Mc Erlean
-
-
Field Summary
Fields Modifier and Type Field Description static Function<java.lang.Object,java.lang.String>
TO_STRING_TRANSFORMER
-
Constructor Summary
Constructors Constructor Description CollectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.Set<T>
asSet(java.lang.Class<T> clazz, java.lang.Object... objects)
This utility method converts a vararg of Objects into a Set. static <T> java.util.Set<T>
asSet(T... objects)
This utility method converts a vararg of Objects into a Set. static <T> Filter<T>
containsFilter(java.util.Collection<T> values)
static <T> java.util.List<T>
filter(java.util.Collection<T> values, Function<? super T,java.lang.Boolean> filter)
Returns a filteredList
of values.static <K,V>
java.util.Map<K,V>filterKeys(java.util.Map<K,V> map, Function<? super K,? extends java.lang.Boolean> filter)
This method can be used to filter a Map.static <T> T
findFirst(java.util.Collection<T> values, Function<? super T,java.lang.Boolean> acceptor)
Finds the first value for whichacceptor
returnstrue
.static <T> java.util.List<T>
flatten(java.util.Collection<? extends java.util.Collection<? extends T>> values)
This method flattens the provided collection of collections of values into a singleList
object containing each of the elements from the provided sub-collections.static <T> java.util.List<T>
flatten(java.util.Collection<? extends T>... collections)
static <T> java.util.List<T>
intersect(java.util.List<? extends T> list1, java.util.List<? extends T> list2)
This method returns a new ArrayList which is the intersection of the two List parameters, based onequality
of their elements.static <K,V>
java.util.Map<K,V>intersect(java.util.Map<K,V> map1, java.util.Map<K,V> map2)
This method returns a new HashMap which is the intersection of the two Map parameters, based onequality
of their entries.static <T> java.util.Set<T>
intersect(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2)
This method returns a new HashSet which is the intersection of the two Set parameters, based onequality
of their elements.static boolean
isEmpty(java.util.Collection<?> items)
static boolean
isEmpty(java.util.Map<?,?> map)
static <T> java.util.List<T>
moveLeft(int offset, T element, java.util.List<T> list)
This method returns a new List instance containing the same element objects as the provided list, but with the specified element having been moved left by the specified offset.static <T> java.util.List<T>
moveRight(int offset, T element, java.util.List<T> list)
This method does the same asmoveLeft(int, Object, List)
but it moves the specified element to the right instead of the left.static <T> java.util.List<T>
nullSafeAppend(java.util.List<T> first, java.util.List<T> second)
This method joins two lists returning the a single list consisting of the first followed by the second.static <T> java.util.List<T>
nullSafeAppend(java.util.List<T> first, java.util.List<T> second, boolean emptyResultIsNull)
This method joins two lists returning the a single list consisting of the first followed by the second.static <K,V>
java.util.Map<K,V>nullSafeMerge(java.util.Map<K,V> first, java.util.Map<K,V> second)
This method merges two maps returning the union of both maps.static <K,V>
java.util.Map<K,V>nullSafeMerge(java.util.Map<K,V> first, java.util.Map<K,V> second, boolean emptyResultIsNull)
This method merges two maps returning the union of both maps.static <T> java.util.Set<T>
nullSafeMerge(java.util.Set<T> first, java.util.Set<T> second)
This method merges two sets returning the union of both sets.static <T> java.util.Set<T>
nullSafeMerge(java.util.Set<T> first, java.util.Set<T> second, boolean emptyResultIsNull)
This method merges two sets returning the union of both sets.static <K,V>
java.util.Map<K,V>sortMapByValue(java.util.Map<K,V> map, java.util.Comparator<java.util.Map.Entry<K,V>> valueComparator)
Creates a new sorted map, based on the values from the given map and Comparator.static <K,V>
java.util.Comparator<java.util.Map.Entry<K,V>>toEntryComparator(java.util.Comparator<V> valueComparator)
This method offers convenient conversion from value-based comparators to entry-based comparators for use withsortMapByValue(Map, Comparator)
above.static java.util.List<java.lang.String>
toListOfStrings(java.util.Collection<?> values)
static <F,T>
java.util.List<T>transform(java.util.Collection<F> values, Function<? super F,? extends T> transformer)
Converts aCollection
of values of type F to aSerializable
List
of values of type T.static <FK,FV,TK,TV>
java.util.Map<TK,TV>transform(java.util.Map<FK,FV> map, Function<java.util.Map.Entry<FK,FV>,Pair<TK,TV>> transformer)
static <F,T>
java.util.List<T>transform(Function<? super F,? extends T> transformer, F... values)
Converts aCollection
of values of type F to aSerializable
List
of values of type T.static <F,T>
java.util.List<T>transformFlat(java.util.Collection<F> values, Function<? super F,? extends java.util.Collection<? extends T>> transformer)
static <F,T,V>
java.util.Map<T,V>transformKeys(java.util.Map<F,V> map, Function<? super F,? extends T> transformer)
Converts aMap
having keys of type F to a newMap
instance having keys of type T.static <F,T>
java.util.Map<F,T>transformToMap(java.util.Collection<F> values, Function<F,T> transformer)
static <T> java.util.Set<T>
unmodifiableSet(java.util.Collection<T> values)
Returns an immutable Serializable Set containing the values.static <T> java.util.Set<T>
unmodifiableSet(T... values)
Returns an immutable Serializable Set containing the values.
-
-
-
Field Detail
-
TO_STRING_TRANSFORMER
public static final Function<java.lang.Object,java.lang.String> TO_STRING_TRANSFORMER
-
-
Method Detail
-
isEmpty
public static boolean isEmpty(java.util.Map<?,?> map)
-
isEmpty
public static boolean isEmpty(java.util.Collection<?> items)
-
nullSafeMerge
public static <T> java.util.Set<T> nullSafeMerge(java.util.Set<T> first, java.util.Set<T> second)
This method merges two sets returning the union of both sets.- Parameters:
first
- first set. can be null.second
- second set. can be null.- Returns:
- the union of both sets. will not be null
-
nullSafeMerge
public static <T> java.util.Set<T> nullSafeMerge(java.util.Set<T> first, java.util.Set<T> second, boolean emptyResultIsNull)
This method merges two sets returning the union of both sets.- Parameters:
first
- first set. can be null.second
- second set. can be null.emptyResultIsNull
- if the result is empty, should we return null?- Returns:
- the union of both sets or null.
-
nullSafeMerge
public static <K,V> java.util.Map<K,V> nullSafeMerge(java.util.Map<K,V> first, java.util.Map<K,V> second)
This method merges two maps returning the union of both maps.- Parameters:
first
- first map. can be null.second
- second map. can be null.- Returns:
- the union of both maps. will not be null
-
nullSafeMerge
public static <K,V> java.util.Map<K,V> nullSafeMerge(java.util.Map<K,V> first, java.util.Map<K,V> second, boolean emptyResultIsNull)
This method merges two maps returning the union of both maps.- Parameters:
first
- first map. can be null.second
- second map. can be null.emptyResultIsNull
- if the result is empty, should we return null?- Returns:
- the union of both maps, or null.
-
nullSafeAppend
public static <T> java.util.List<T> nullSafeAppend(java.util.List<T> first, java.util.List<T> second)
This method joins two lists returning the a single list consisting of the first followed by the second.- Parameters:
first
- first list. can be null.second
- second list. can be null.- Returns:
- the concatenation of both lists. will not be null
-
nullSafeAppend
public static <T> java.util.List<T> nullSafeAppend(java.util.List<T> first, java.util.List<T> second, boolean emptyResultIsNull)
This method joins two lists returning the a single list consisting of the first followed by the second.- Parameters:
first
- first list. can be null.second
- second list. can be null.emptyResultIsNull
- if the result is empty, should we return null?- Returns:
- the concatenation of both lists or null
-
transform
public static <F,T> java.util.List<T> transform(java.util.Collection<F> values, Function<? super F,? extends T> transformer)
Converts aCollection
of values of type F to aSerializable
List
of values of type T. Filters out all values converted tonull
.- Type Parameters:
F
- From typeT
- To type- Parameters:
values
- the values to convert.transformer
- Used to convert values.- Returns:
- List
-
transformKeys
public static <F,T,V> java.util.Map<T,V> transformKeys(java.util.Map<F,V> map, Function<? super F,? extends T> transformer)
Converts aMap
having keys of type F to a newMap
instance having keys of type T. The object references in the value set are copied to the transformed map, thus reusing the same objects.- Type Parameters:
F
- From typeT
- To typeV
- The value type of the before and after maps.- Parameters:
map
- the map to convert.transformer
- Used to convert keys.- Returns:
- a new Map instance with transformed keys and unchanged values. These values will be the same object references.
-
transform
public static <F,T> java.util.List<T> transform(Function<? super F,? extends T> transformer, F... values)
Converts aCollection
of values of type F to aSerializable
List
of values of type T. Filters out all values converted tonull
.- Type Parameters:
F
- From typeT
- To type- Parameters:
values
- the values to convert.transformer
- Used to convert values.- Returns:
- List
-
toListOfStrings
public static java.util.List<java.lang.String> toListOfStrings(java.util.Collection<?> values)
-
asSet
public static <T> java.util.Set<T> asSet(T... objects)
This utility method converts a vararg of Objects into a Set. - Parameters:
objects
- the objects to be added to the set- Returns:
- a Set of objects (any equal objects will of course not be duplicated)
- Throws:
java.lang.ClassCastException
- if any of the supplied objects are not of type T.
-
asSet
public static <T> java.util.Set<T> asSet(java.lang.Class<T> clazz, java.lang.Object... objects)
This utility method converts a vararg of Objects into a Set. - Parameters:
clazz
- the Set type to return.objects
- the objects to be added to the set- Returns:
- a Set of objects (any equal objects will of course not be duplicated)
- Throws:
java.lang.ClassCastException
- if any of the supplied objects are not of type T.
-
filter
public static <T> java.util.List<T> filter(java.util.Collection<T> values, Function<? super T,java.lang.Boolean> filter)
Returns a filteredList
of values. Only values for whichfilter.apply(T) returns true
are included in theList
or returned values.- Type Parameters:
T
- The type of theCollection
- Parameters:
values
- theCollection
to be filtered.filter
- theFunction
used to filter theCollection
.- Returns:
- the filtered
List
of values.
-
flatten
public static <T> java.util.List<T> flatten(java.util.Collection<? extends java.util.Collection<? extends T>> values)
This method flattens the provided collection of collections of values into a singleList
object containing each of the elements from the provided sub-collections. For example,flatten( [1, 2], [3], [], [4, 5, 6] )
would produce a List like[1, 2, 3, 4, 5, 6]
. Here, "[]" represents any Java collection.- Type Parameters:
T
- the element type of the collections. Note that this must be the same for all collections.- Parameters:
values
- a collection of collections of elements to be flattened.- Returns:
- a List containing the flattened elements.
-
flatten
@SafeVarargs public static <T> java.util.List<T> flatten(java.util.Collection<? extends T>... collections)
- Parameters:
collections
- a vararg of Collection objects to be flattened into a list.- Returns:
- A flat List containing the elements of the provided collections.
- Since:
- 5.0
-
transformFlat
public static <F,T> java.util.List<T> transformFlat(java.util.Collection<F> values, Function<? super F,? extends java.util.Collection<? extends T>> transformer)
-
findFirst
public static <T> T findFirst(java.util.Collection<T> values, Function<? super T,java.lang.Boolean> acceptor)
Finds the first value for whichacceptor
returnstrue
.- Type Parameters:
T
- T- Parameters:
values
- Collectionacceptor
- Function super T, Boolean>- Returns:
- returns accepted value or
null
.
-
unmodifiableSet
public static <T> java.util.Set<T> unmodifiableSet(T... values)
Returns an immutable Serializable Set containing the values.- Type Parameters:
T
- T- Parameters:
values
- T...- Returns:
- Set
-
unmodifiableSet
public static <T> java.util.Set<T> unmodifiableSet(java.util.Collection<T> values)
Returns an immutable Serializable Set containing the values.- Type Parameters:
T
- T- Parameters:
values
- Collection- Returns:
- Set
-
transformToMap
public static <F,T> java.util.Map<F,T> transformToMap(java.util.Collection<F> values, Function<F,T> transformer)
- Parameters:
values
- Collectiontransformer
- Function- Returns:
- Map
-
filterKeys
public static <K,V> java.util.Map<K,V> filterKeys(java.util.Map<K,V> map, Function<? super K,? extends java.lang.Boolean> filter)
This method can be used to filter a Map. Any keys in the supplied map, for which the suppliedfilter function
returnstrue
, will be included in the resultant Map, else they will not.- Parameters:
map
- the map whose entries are to be filtered.filter
- the filter function which is applied to the key.- Returns:
- a filtered map.
-
transform
public static <FK,FV,TK,TV> java.util.Map<TK,TV> transform(java.util.Map<FK,FV> map, Function<java.util.Map.Entry<FK,FV>,Pair<TK,TV>> transformer)
-
containsFilter
public static <T> Filter<T> containsFilter(java.util.Collection<T> values)
-
intersect
public static <T> java.util.List<T> intersect(java.util.List<? extends T> list1, java.util.List<? extends T> list2)
This method returns a new ArrayList which is the intersection of the two List parameters, based onequality
of their elements. The intersection list will contain elements in the order they have in list1 and any references in the resultant list will be to elements within list1 also.- Returns:
- a new ArrayList whose values represent the intersection of the two Lists.
-
intersect
public static <K,V> java.util.Map<K,V> intersect(java.util.Map<K,V> map1, java.util.Map<K,V> map2)
This method returns a new HashMap which is the intersection of the two Map parameters, based onequality
of their entries. Any references in the resultant map will be to elements within map1.- Returns:
- a new HashMap whose values represent the intersection of the two Maps.
-
intersect
public static <T> java.util.Set<T> intersect(java.util.Set<? extends T> set1, java.util.Set<? extends T> set2)
This method returns a new HashSet which is the intersection of the two Set parameters, based onequality
of their elements. Any references in the resultant set will be to elements within set1.- Returns:
- a new HashSet whose values represent the intersection of the two Sets.
-
sortMapByValue
public static <K,V> java.util.Map<K,V> sortMapByValue(java.util.Map<K,V> map, java.util.Comparator<java.util.Map.Entry<K,V>> valueComparator)
Creates a new sorted map, based on the values from the given map and Comparator.- Parameters:
map
- the map which needs to be sortedvalueComparator
- the Comparator- Returns:
- a new sorted map
-
toEntryComparator
public static <K,V> java.util.Comparator<java.util.Map.Entry<K,V>> toEntryComparator(java.util.Comparator<V> valueComparator)
This method offers convenient conversion from value-based comparators to entry-based comparators for use withsortMapByValue(Map, Comparator)
above. Call it like so:CollectionUtils.<String, Integer>toEntryComparator(valueComparator);
- Parameters:
valueComparator
- a comparator which compares the value types from a Map.- Returns:
- a comparator which takes Map.Entry objects from that Map and compares their values.
-
moveLeft
public static <T> java.util.List<T> moveLeft(int offset, T element, java.util.List<T> list)
This method returns a new List instance containing the same element objects as the provided list, but with the specified element having been moved left by the specified offset. If the offset would mean that the element would move beyond the start or end of the list, it will move only to the end.- Parameters:
offset
- the number of places over which to move the specified element.element
- the element to be moved.list
- the list to be reordered.- Returns:
- a new List instance containing the ordered elements.
- Throws:
java.util.NoSuchElementException
- if the list does not contain an element equal to the one specified.
-
moveRight
public static <T> java.util.List<T> moveRight(int offset, T element, java.util.List<T> list)
This method does the same asmoveLeft(int, Object, List)
but it moves the specified element to the right instead of the left.
-
-