Package org.axonframework.common
Class CollectionUtils
java.lang.Object
org.axonframework.common.CollectionUtils
Utility methods for operations on collections.
- Since:
- 0.7
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionstatic <R> Collection<R> asCollection(Object potentialCollection) Returns a Collection instance that contains the elements of the givenpotentialCollection.static <T,C extends Collection<T>>
Cintersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) Returns a collection containing the elements that are in both given collectionscollection1andcollection2, using the givencollectionBuilderto create an instance for the new collection.static <K,V> Map <K, V> Returns a new Map with the entries from givenexistingMapas well as an entry with givennewKeyand givennewValue.static <S,T extends Collection<S>>
TMerge two collections into a new collection instance.
-
Method Details
-
merge
public static <S,T extends Collection<S>> T merge(T collection1, T collection2, Supplier<T> factoryMethod) Merge two collections into a new collection instance. The new collection is created using the givenfactoryMethod.If any of the two inputs collections is
nullor empty the other input collection is returned (even if that one isnullas well).- Type Parameters:
S- the type of elements in the collectionsT- the type of collection- Parameters:
collection1- the first collectioncollection2- the second collectionfactoryMethod- function to initialize the new collection- Returns:
- a collection that combines both collections
-
asCollection
Returns a Collection instance that contains the elements of the givenpotentialCollection. If not a Collection-like structure, it will return a Collection with the givenpotentialCollectionas the sole element.The following structures are recognized and will have their elements placed inside a Collection:
StreamCollection- Array
SpliteratorIterable
- Type Parameters:
R- The type of instance contained in the resulting Collection- Parameters:
potentialCollection- The instance to collect all elements from- Returns:
- A Collection of the elements contained in the given
potentialCollection
-
intersect
public static <T,C extends Collection<T>> C intersect(Collection<? extends T> collection1, Collection<? extends T> collection2, Supplier<C> collectionBuilder) Returns a collection containing the elements that are in both given collectionscollection1andcollection2, using the givencollectionBuilderto create an instance for the new collection. The items are added to the resulting collection in the order as found in collection2.- Type Parameters:
T- The type of element contained in the resulting collectionC- The type of collection to return- Parameters:
collection1- The first collectioncollection2- The second collectioncollectionBuilder- The factory for the returned collection instance- Returns:
- a collection containing the elements that were found in both given collections
-
mapWith
Returns a new Map with the entries from givenexistingMapas well as an entry with givennewKeyand givennewValue.- Type Parameters:
K- The type of keyV- The type of value- Parameters:
existingMap- A map containing the base elements to copy.newKey- The key of the new element to add.newValue- The value of the new element to add.- Returns:
- a new Map instance containing the elements in the base map and the new key/value pair
-