Class CollectionUtils

java.lang.Object
org.axonframework.common.CollectionUtils

public final class CollectionUtils extends Object
Utility methods for operations on collections.
Since:
0.7
Author:
Allard Buijze
  • Method Summary

    Modifier and Type
    Method
    Description
    static <R> Collection<R>
    asCollection(Object potentialCollection)
    Returns a Collection instance that contains the elements of the given potentialCollection.
    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 collections collection1 and collection2, using the given collectionBuilder to create an instance for the new collection.
    static <K, V> Map<K,V>
    mapWith(Map<K,V> existingMap, K newKey, V newValue)
    Returns a new Map with the entries from given existingMap as well as an entry with given newKey and given newValue.
    static <S, T extends Collection<S>>
    T
    merge(T collection1, T collection2, Supplier<T> factoryMethod)
    Merge two collections into a new collection instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 given factoryMethod.

      If any of the two inputs collections is null or empty the other input collection is returned (even if that one is null as well).

      Type Parameters:
      S - the type of elements in the collections
      T - the type of collection
      Parameters:
      collection1 - the first collection
      collection2 - the second collection
      factoryMethod - function to initialize the new collection
      Returns:
      a collection that combines both collections
    • asCollection

      public static <R> Collection<R> asCollection(Object potentialCollection)
      Returns a Collection instance that contains the elements of the given potentialCollection. If not a Collection-like structure, it will return a Collection with the given potentialCollection as the sole element.

      The following structures are recognized and will have their elements placed inside a Collection:

      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 collections collection1 and collection2, using the given collectionBuilder to 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 collection
      C - The type of collection to return
      Parameters:
      collection1 - The first collection
      collection2 - The second collection
      collectionBuilder - The factory for the returned collection instance
      Returns:
      a collection containing the elements that were found in both given collections
    • mapWith

      public static <K, V> Map<K,V> mapWith(Map<K,V> existingMap, K newKey, V newValue)
      Returns a new Map with the entries from given existingMap as well as an entry with given newKey and given newValue.
      Type Parameters:
      K - The type of key
      V - 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