Package org.silverpeas.core.util
Class CollectionUtil
- java.lang.Object
-
- org.silverpeas.core.util.CollectionUtil
-
public class CollectionUtil extends Object
- Author:
- Yohann Chastagnier
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCollectionUtil.RuptureContext<T>Handles the context of a rupture treatment.
It must by initialized by callingCollectionUtil.RuptureContext.newOne(List)after usingfindNextRupture(RuptureContext, Predicate).
Callers can verify if it is no more possible to get an element from the rupture by callingCollectionUtil.RuptureContext.isTerminated().
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> booleanaddAllIgnoreNull(Collection<? super T> c, T... elements)Null elements are not taking into account.static <T> List<T>asList(T... values)static <T> Set<T>asSet(T... values)static <T> Optional<T>findFirst(List<T> list, Predicate<T> predicate)Gets the optional first element in the given List which matches the given predicate.static <T> Optional<T>findFirst(List<T> list, Predicate<T> predicate, int firstIndex)Gets the optional first element in the given List which matches the given predicate and the position is at or after the first given index.static <T> Optional<T>findNextRupture(CollectionUtil.RuptureContext<T> context, Predicate<T> predicate)Gets the optional next element in the given ordered List which matches the givenCollectionUtil.RuptureContextwhich must be intialized by callingCollectionUtil.RuptureContext.newOne(List).static <T> intindexOf(List<T> list, Predicate<T> predicate)Finds the first index in the given List which matches the given predicate.static <T> intindexOf(List<T> list, Predicate<T> predicate, int firstIndex)Finds the first index in the given List which matches the given predicate.static <T> Collection<T>intersection(Collection<T> col1, Collection<T> col2)Makes an intersection between both of the given collections.
The result contains unique values.static <T> Collection<T>intersection(Collection<T> col1, Collection<T> col2, Function<T,Object> discriminator)Makes an intersection between both of the given collections.
The result contains unique values.static <T> List<T>intersection(List<T> list1, List<T> list2)Makes an intersection between both of the given lists.
The result contains unique values.static <T> List<T>intersection(List<T> list1, List<T> list2, Function<T,Object> discriminator)Makes an intersection between both of the given lists.
The result contains unique values.static <T> booleanisEmpty(Collection<T> collection)Checks if the given collection is not instanced or emptystatic <T> booleanisNotEmpty(Collection<T> collection)Checks if the given collection is instanced and not emptystatic <T> List<T>reverse(List<T> list)Reverse the given list and returns it.static <T> Collection<Collection<T>>split(Collection<T> collection)Splits a collection into several collections.static <T> Collection<Collection<T>>split(Collection<T> collection, int collectionSizeMax)Splits a collection into several collections.static <T> List<List<T>>splitList(List<T> collection)Splits a collection into several collections.static <T> Collection<T>union(Collection<T> col1, Collection<T> col2)Makes an union between both of the given collections.
The result contains unique values.static <T> List<T>union(List<T> list1, List<T> list2)Makes an union between both of the given lists.
The result contains unique values.
-
-
-
Method Detail
-
reverse
public static <T> List<T> reverse(List<T> list)
Reverse the given list and returns it.- Type Parameters:
T- the type of the instance contained into the specified list- Parameters:
list- the list to reverse- Returns:
- the specified list that has been reversed (same instance)
-
isEmpty
public static <T> boolean isEmpty(Collection<T> collection)
Checks if the given collection is not instanced or empty- Type Parameters:
T- the type of the instance contained into the specified collection- Parameters:
collection- the collection to verify- Returns:
- true if specified collection is empty, false otherwise
-
isNotEmpty
public static <T> boolean isNotEmpty(Collection<T> collection)
Checks if the given collection is instanced and not empty- Type Parameters:
T- the type of the instance contained into the specified collection- Parameters:
collection- the collection to verify- Returns:
- true if specified collection is not empty, false otherwise
-
splitList
public static <T> List<List<T>> splitList(List<T> collection)
Splits a collection into several collections. (Particularly useful for limitations of database around the "in" clause)- Type Parameters:
T- the concrete type of the items in the list.- Parameters:
collection- the collection to split- Returns:
- the slices of the specified collection
-
split
public static <T> Collection<Collection<T>> split(Collection<T> collection)
Splits a collection into several collections. (Particularly useful for limitations of database around the "in" clause)- Type Parameters:
T- the concrete type of the items in the collection.- Parameters:
collection- the collection to split- Returns:
- the slices of the specified collection
-
split
public static <T> Collection<Collection<T>> split(Collection<T> collection, int collectionSizeMax)
Splits a collection into several collections. (Particularly useful for limitations of database around the "in" clause)- Type Parameters:
T- the concrete type of the items in the collection.- Parameters:
collection- the collection to splitcollectionSizeMax- the maximum elements in slice- Returns:
- the slices of the specified collection
-
asList
@SafeVarargs public static <T> List<T> asList(T... values)
-
asSet
@SafeVarargs public static <T> Set<T> asSet(T... values)
-
addAllIgnoreNull
@SafeVarargs public static <T> boolean addAllIgnoreNull(Collection<? super T> c, T... elements)
Null elements are not taking into account.- Type Parameters:
T- the concrete type of the items.- Parameters:
c- collection in which the items will be addedelements- the items to add- Returns:
- true of the items are added, false otherwise.
- See Also:
Collections.addAll(java.util.Collection, Object[])
-
union
public static <T> List<T> union(List<T> list1, List<T> list2)
Makes an union between both of the given lists.
The result contains unique values.- Type Parameters:
T- the type of the items in the list.- Parameters:
list1- the first list.list2- the second list.- Returns:
- the union between the two lists.
-
union
public static <T> Collection<T> union(Collection<T> col1, Collection<T> col2)
Makes an union between both of the given collections.
The result contains unique values.- Type Parameters:
T- the type of the items in the list- Parameters:
col1- the first collection.col2- the second collection.- Returns:
- the union between the two collections.
-
intersection
public static <T> List<T> intersection(List<T> list1, List<T> list2)
Makes an intersection between both of the given lists.
The result contains unique values.- Type Parameters:
T- the type of the items in the list- Parameters:
list1- the first list.list2- the second list.- Returns:
- the intersection between the two lists.
-
intersection
public static <T> Collection<T> intersection(Collection<T> col1, Collection<T> col2)
Makes an intersection between both of the given collections.
The result contains unique values.- Type Parameters:
T- the type of the items in the list- Parameters:
col1- the first collection.col2- the second collection.- Returns:
- the intersection between the two collections.
-
intersection
public static <T> List<T> intersection(List<T> list1, List<T> list2, Function<T,Object> discriminator)
Makes an intersection between both of the given lists.
The result contains unique values.- Type Parameters:
T- the type of the items in the list- Parameters:
list1- the first list.list2- the second list.discriminator- get the discriminator data.- Returns:
- the intersection between the two lists.
-
intersection
public static <T> Collection<T> intersection(Collection<T> col1, Collection<T> col2, Function<T,Object> discriminator)
Makes an intersection between both of the given collections.
The result contains unique values.- Type Parameters:
T- the type of the items in the list- Parameters:
col1- the first collection.col2- the second collection.discriminator- get the discriminator data.- Returns:
- the intersection between the two collections.
-
indexOf
public static <T> int indexOf(List<T> list, Predicate<T> predicate)
Finds the first index in the given List which matches the given predicate.- Type Parameters:
T- the type of the items in the list- Parameters:
list- the List to search, may not be null.predicate- the predicate to use, may not be null.- Returns:
- the first index of an Object in the List which matches the predicate.
-
indexOf
public static <T> int indexOf(List<T> list, Predicate<T> predicate, int firstIndex)
Finds the first index in the given List which matches the given predicate.- Type Parameters:
T- the type of the items in the list- Parameters:
list- the List to search, may not be null.predicate- the predicate to use, may not be null.firstIndex- an integer representing the first index from which the search starts.- Returns:
- the first index of an Object in the List which matches the predicate.
-
findFirst
public static <T> Optional<T> findFirst(List<T> list, Predicate<T> predicate)
Gets the optional first element in the given List which matches the given predicate.- Type Parameters:
T- the type of the items in the list- Parameters:
list- the List to search, may not be null.predicate- the predicate to use, may not be null.- Returns:
- the optional Object in the List which matches the predicate.
-
findFirst
public static <T> Optional<T> findFirst(List<T> list, Predicate<T> predicate, int firstIndex)
Gets the optional first element in the given List which matches the given predicate and the position is at or after the first given index.- Type Parameters:
T- the type of the items in the list- Parameters:
list- the List to search, may not be null.predicate- the predicate to use, may not be null.firstIndex- an integer representing the first index from which the search starts.- Returns:
- the optional Object in the List which matches the predicate.
-
findNextRupture
public static <T> Optional<T> findNextRupture(CollectionUtil.RuptureContext<T> context, Predicate<T> predicate)
Gets the optional next element in the given ordered List which matches the givenCollectionUtil.RuptureContextwhich must be intialized by callingCollectionUtil.RuptureContext.newOne(List).- Type Parameters:
T- the type of the items in the list- Parameters:
context- the context of the rupture.predicate- the predicate to use, may not be null.- Returns:
- the first index of an Object in the List which matches the predicate.
-
-