Module logicng

Class CollectionHelper


  • public final class CollectionHelper
    extends Object
    A class which contains utility methods for Collection objects.
    Since:
    2.0.0
    Version:
    2.3.0
    • Method Detail

      • nullOrEmpty

        public static <T> boolean nullOrEmpty​(Collection<T> collection)
        Tests if the given collection is null or empty.
        Type Parameters:
        T - the type of the elements in the collection, may be arbitrary
        Parameters:
        collection - the collection, may be null
        Returns:
        true if the collection is null or empty, otherwise false
      • nullSafe

        public static <T,​C extends Collection<T>> C nullSafe​(Supplier<C> collectionSupplier,
                                                                   Supplier<C> collectionFactory)
        Null safe wrapper for collections. Returns an (unmodifiable!) empty list if the given collection is null, otherwise returns the given list unchanged.
        Type Parameters:
        T - the type of the elements in the collection, may be arbitrary
        C - the type parameters of the collection
        Parameters:
        collectionSupplier - the collection, may be null
        collectionFactory - the supplier for the collection
        Returns:
        the collection itself, if it is not null, otherwise an unmodifiable empty list
      • nullSafe

        public static <T> Collection<T> nullSafe​(Collection<T> collection)
        Null safe wrapper for collections. Returns an (unmodifiable!) empty list if the given collection is null, otherwise returns the given list unchanged.
        Type Parameters:
        T - the type of the elements in the collection, may be arbitrary
        Parameters:
        collection - the collection, may be null
        Returns:
        the collection itself, if it is not null, otherwise an unmodifiable empty list
      • intersection

        public static <T,​C extends Collection<T>> C intersection​(Collection<? extends Collection<T>> collections,
                                                                       Supplier<C> collectionFactory)
        Computes the intersection of the given collections. Each collection treated in a null-safe manner, i.e. if a collection is null the collection is considered to be an empty collection.
        Type Parameters:
        T - the type parameters of the elements
        C - the type parameters of the collection
        Parameters:
        collections - the collections the intersection should be computed for
        collectionFactory - the supplier for the collection
        Returns:
        the intersection of the collections
      • intersection

        public static <T,​C extends Collection<T>> C intersection​(Collection<T> col1,
                                                                       Collection<T> col2,
                                                                       Supplier<C> collectionFactory)
        Computes the intersection of two collections. Each collection treated in a null-safe manner, i.e. if a collection is null the collection is considered to be an empty collection.
        Type Parameters:
        T - the type parameters of the elements
        C - the type parameters of the collection
        Parameters:
        col1 - the first collection
        col2 - the second collection
        collectionFactory - the supplier for the collection
        Returns:
        the intersection of the two collections
      • union

        public static <T,​C extends Collection<T>> C union​(Collection<? extends Collection<T>> collections,
                                                                Supplier<C> collectionFactory)
        Computes the union of the given collections. Each collection is treated in a null-safe manner, i.e. if a collection is null the collection is considered to be an empty collection
        Type Parameters:
        T - the type parameters of the elements
        C - the type parameters of the collection
        Parameters:
        collections - the collections the union should be computed for
        collectionFactory - the supplier for the collection
        Returns:
        the union of the collections
      • union

        public static <T,​C extends Collection<T>> C union​(Collection<T> col1,
                                                                Collection<T> col2,
                                                                Supplier<C> collectionFactory)
        Computes the union of two collections. Each collection treated in a null-safe manner, i.e. if a collection is null the collection is considered to be an empty collection.
        Type Parameters:
        T - the type parameters of the elements
        C - the type parameters of the collection
        Parameters:
        col1 - the first collection
        col2 - the second collection
        collectionFactory - the supplier for the collection
        Returns:
        the union of the two collections
      • difference

        public static <T,​C extends Collection<T>> C difference​(Collection<T> col1,
                                                                     Collection<T> col2,
                                                                     Supplier<C> collectionFactory)
        Computes the difference of the given two collections. The result contains all elements from the first collection that are not contained within the second collection. Each collection treated in a null-safe manner, i.e. if a collection is null the collection is considered to be an empty collection.
        Type Parameters:
        T - the type parameters of the elements
        C - the type parameters of the collection
        Parameters:
        col1 - the first collection
        col2 - the second collection
        collectionFactory - the supplier for the collection
        Returns:
        the union of the two collections
      • mkString

        public static <T> String mkString​(Collection<T> collection,
                                          String delimiter)
        Builds a string representation of a collection using a delimiter.
        Type Parameters:
        T - the type parameter of the elements
        Parameters:
        collection - the collection
        delimiter - the delimiter
        Returns:
        the string representation of the collection
      • mkString

        public static <T> String mkString​(Collection<T> collection,
                                          String prefix,
                                          String delimiter,
                                          String suffix)
        Builds a string representation of a collection using a prefix, delimiter and suffix.
        Type Parameters:
        T - the type parameter of the elements
        Parameters:
        collection - the collection
        prefix - the prefix
        delimiter - the delimiter
        suffix - the suffix
        Returns:
        the string representation of the collection
      • mkString

        public static <T> String mkString​(T[] array,
                                          String delimiter)
        Builds a string representation of an array using a delimiter.
        Type Parameters:
        T - the type parameter of the elements
        Parameters:
        array - the array
        delimiter - the delimiter
        Returns:
        the string representation of the collection
      • mkString

        public static <T> String mkString​(T[] array,
                                          String prefix,
                                          String delimiter,
                                          String suffix)
        Builds a string representation of an array using a prefix, delimiter and suffix.
        Type Parameters:
        T - the type parameter of the elements
        Parameters:
        array - the array
        prefix - the prefix
        delimiter - the delimiter
        suffix - the suffix
        Returns:
        the string representation of the collection