Class RMCollectionUtils
- java.lang.Object
-
- org.alfresco.module.org_alfresco_module_rm.util.RMCollectionUtils
-
public final class RMCollectionUtils extends Object
Various common helper methods for Collections. This class is probably only appropriate for use with relatively small collections as it has not been optimised for dealing with large collections.- Since:
- 2.4.a
- Author:
- Neil Mc Erlean
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RMCollectionUtils.Difference
This enum represents a change in an entry between 2 collections.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Serializable,LIST extends Serializable & List<T>>
LISTasSerializableList(T... elements)
Returns a Serializable List containing all of the provided elements.static <T> HashSet<T>
asSet(Collection<T> c)
Returns a Set containing all of the elements in the provided collection.static <T> HashSet<T>
asSet(T... elements)
Returns a Set containing all of the provided elements.static <K,V>
RMCollectionUtils.DifferencediffKey(Map<K,V> from, Map<K,V> to, K key)
Determines the change in a Map entry between two Maps.static <T> List<T>
getDuplicateElements(List<T> l)
Gets the list of duplicate elements contained within the specified list, if any.static <T> T
head(List<T> l)
Returns the head (element at index 0) of the provided List.static boolean
isEmpty(Serializable value)
Check if a property is null or an empty collection.static <T> List<T>
tail(List<T> l)
Returns the tail of the provided List i.e. the sublist which contains all elements of the given list except thehead
.static <T> com.google.common.collect.ImmutableSet<T>
toImmutableSet(Collection<T> collection)
Convert a collection to an immutable set.
-
-
-
Method Detail
-
getDuplicateElements
public static <T> List<T> getDuplicateElements(List<T> l)
Gets the list of duplicate elements contained within the specified list, if any.- Type Parameters:
T
- the element type of the list.- Parameters:
l
- the list in which to find duplicates.- Returns:
- a list of duplicate elements. If there are no duplicates, returns an empty list.
-
head
public static <T> T head(List<T> l)
Returns the head (element at index 0) of the provided List.- Type Parameters:
T
- the type of the List.- Parameters:
l
- the list whose head is sought.- Returns:
- the head element or
null
for the empty list. - Throws:
NullPointerException
- if l isnull
-
tail
public static <T> List<T> tail(List<T> l)
Returns the tail of the provided List i.e. the sublist which contains all elements of the given list except thehead
.- Type Parameters:
T
- the type of the List.- Parameters:
l
- the list whose tail is sought.- Returns:
- the tail sublist, which will be an empty list if the provided list had only a single element.
- Throws:
NullPointerException
- if l isnull
UnsupportedOperationException
- if the provided list was empty.
-
asSerializableList
@SafeVarargs public static <T extends Serializable,LIST extends Serializable & List<T>> LIST asSerializableList(T... elements)
Returns a Serializable List containing all of the provided elements.- Type Parameters:
T
- the element type.- Parameters:
elements
- the elements to put in a list.- Returns:
- a Serializable List containing all the provided elements.
-
asSet
@SafeVarargs public static <T> HashSet<T> asSet(T... elements)
Returns a Set containing all of the provided elements. Duplicate elements will be removed as per theSet
contract.- Type Parameters:
T
- the element type.- Parameters:
elements
- the elements to put in a Set.- Returns:
- a Set containing all the provided elements (without duplicates).
-
asSet
public static <T> HashSet<T> asSet(Collection<T> c)
Returns a Set containing all of the elements in the provided collection. Duplicate elements will be removed as per theSet
contract.- Type Parameters:
T
- the element type.- Parameters:
c
- the elements to put in a Set.- Returns:
- a Set containing all the provided elements (without duplicates).
-
diffKey
public static <K,V> RMCollectionUtils.Difference diffKey(Map<K,V> from, Map<K,V> to, K key)
Determines the change in a Map entry between two Maps. Note that both maps must have the same types of key-value pair.- Type Parameters:
K
- the type of the key.V
- the type of the value.- Parameters:
from
- the first collection.to
- the second collection.key
- the key identifying the entry.- Returns:
- the
RMCollectionUtils.Difference
. - Throws:
IllegalArgumentException
- ifkey
isnull
.
-
toImmutableSet
public static <T> com.google.common.collect.ImmutableSet<T> toImmutableSet(Collection<T> collection)
Convert a collection to an immutable set. Any instances of null in the original collection will be removed.- Type Parameters:
T
- The type of the object in the collection.- Parameters:
collection
- The original collection.- Returns:
- The immutable set.
-
isEmpty
public static boolean isEmpty(Serializable value)
Check if a property is null or an empty collection. Note that this is the same as {org.apache.commons.collections.CollectionUtils.isEmpty(Collection)}, except that it takes a Serializable rather than a Collection. This avoids awkward casting exceptions when working with properties.- Parameters:
value
- The (probably Collection) value to check.- Returns:
- true if the supplied value is null or an empty collection.
-
-