Class MoreCollections


  • public final class MoreCollections
    extends Object

    Overview

    Utility Methods for Collections and some types to be used in the context of Collections.

    isEmpty()

    The overloaded method isEmpty(Collection) checks all kinds of Collections / varargs parameter for not being null and emptiness. In case of Streams it solely checks for being not null in order not to consume it.

    requireNotEmpty()

    The overloaded method requireNotEmpty(Collection) checks all kinds of Collections / varargs parameter for not being null nor empty. In case of being null / empty they will throw an IllegalArgumentException

    Map Difference

    The method difference(Map, Map) creates an MapDifference view on the two given maps in order to check, well whether they are equal or not and if not which elements are differing.

    Map contains key

    Check whether the given Map contains at least one of the given keys (varags)
    Author:
    Oliver Wolff
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean containsKey​(Map<?,​?> map, Object... keys)
      Checks whether the given map contains at least one of the given keys to be checked.
      static <K,​V>
      MapDifference<K,​V>
      difference​(Map<? extends K,​? extends V> left, Map<? extends K,​? extends V> right)
      Computes the difference between two maps.
      static boolean isEmpty​(Iterable<?> elements)
      Simple check method for a null safe check of the emptiness of the given parameter.
      static boolean isEmpty​(Object... elements)
      Simple check method for a null safe check of the emptiness of the given varags-parameter.
      static boolean isEmpty​(Collection<?> elements)
      Simple check method for a null safe check of the emptiness of the given parameter.
      static boolean isEmpty​(Iterator<?> elements)
      Simple check method for a null safe check of the emptiness of the given parameter.
      static boolean isEmpty​(Map<?,​?> map)
      Simple check method for a null safe check of the emptiness of the given parameter.
      static boolean isEmpty​(Stream<?> elements)
      Simple check method for a null safe check of the emptiness of the given parameter.
      static <T> Iterable<T> requireNotEmpty​(Iterable<T> elements)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Iterable<T> requireNotEmpty​(Iterable<T> elements, String message)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Collection<T> requireNotEmpty​(Collection<T> elements)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Collection<T> requireNotEmpty​(Collection<T> elements, String message)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Iterator<T> requireNotEmpty​(Iterator<T> elements)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Iterator<T> requireNotEmpty​(Iterator<T> elements, String message)
      Shorthand for checking whether the given elements are empty or not.
      static <K,​V>
      Map<K,​V>
      requireNotEmpty​(Map<K,​V> elements)
      Shorthand for checking whether the given elements are empty or not.
      static <K,​V>
      Map<K,​V>
      requireNotEmpty​(Map<K,​V> elements, String message)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Stream<T> requireNotEmpty​(Stream<T> elements)
      Shorthand for checking whether the given elements are empty or not.
      static <T> Stream<T> requireNotEmpty​(Stream<T> elements, String message)
      Shorthand for checking whether the given elements are empty or not.
      static <T> T[] requireNotEmpty​(T... elements)
      Shorthand for checking whether the given elements are empty or not.
    • Method Detail

      • isEmpty

        public static boolean isEmpty​(Object... elements)
        Simple check method for a null safe check of the emptiness of the given varags-parameter.
        Parameters:
        elements - to be checked, may be null
        Returns:
        true is the given elements are null or empty
      • isEmpty

        public static boolean isEmpty​(Iterable<?> elements)
        Simple check method for a null safe check of the emptiness of the given parameter.
        Parameters:
        elements - to be checked, may be null
        Returns:
        true is the given elements are null or empty
      • isEmpty

        public static boolean isEmpty​(Collection<?> elements)
        Simple check method for a null safe check of the emptiness of the given parameter.
        Parameters:
        elements - to be checked, may be null
        Returns:
        true is the given elements are null or empty
      • isEmpty

        public static boolean isEmpty​(Map<?,​?> map)
        Simple check method for a null safe check of the emptiness of the given parameter.
        Parameters:
        map - to be checked, may be null
        Returns:
        true is the given elements are null or empty
      • isEmpty

        public static boolean isEmpty​(Iterator<?> elements)
        Simple check method for a null safe check of the emptiness of the given parameter.
        Parameters:
        elements - to be checked, may be null
        Returns:
        true is the given elements are null or empty
      • requireNotEmpty

        @SafeVarargs
        public static <T> T[] requireNotEmpty​(T... elements)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Collection<T> requireNotEmpty​(Collection<T> elements)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Collection<T> requireNotEmpty​(Collection<T> elements,
                                                        String message)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        message - to be set in error-case
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <K,​V> Map<K,​V> requireNotEmpty​(Map<K,​V> elements)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        K - the type for the key
        V - the type for the value
        Parameters:
        elements - to be checked
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <K,​V> Map<K,​V> requireNotEmpty​(Map<K,​V> elements,
                                                                 String message)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        K - the type for the key
        V - the type for the value
        Parameters:
        elements - to be checked
        message - to be set in error-case
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Iterable<T> requireNotEmpty​(Iterable<T> elements)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Iterable<T> requireNotEmpty​(Iterable<T> elements,
                                                      String message)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        message - to be set in error-case
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Iterator<T> requireNotEmpty​(Iterator<T> elements)
        Shorthand for checking whether the given elements are empty or not.
        Parameters:
        elements - to be checked
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Iterator<T> requireNotEmpty​(Iterator<T> elements,
                                                      String message)
        Shorthand for checking whether the given elements are empty or not.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        message - to be set in error-case
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null or empty
      • requireNotEmpty

        public static <T> Stream<T> requireNotEmpty​(Stream<T> elements)
        Shorthand for checking whether the given elements are empty or not. Caution: In order not to consume the stream only a null check will be performed.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null
      • requireNotEmpty

        public static <T> Stream<T> requireNotEmpty​(Stream<T> elements,
                                                    String message)
        Shorthand for checking whether the given elements are empty or not. Caution: In order not to consume the stream only a null check will be performed.
        Type Parameters:
        T - identifying the type to be checked
        Parameters:
        elements - to be checked
        message - to be set in error-case
        Returns:
        the given parameter
        Throws:
        IllegalArgumentException - in case the given elements are null
      • isEmpty

        public static boolean isEmpty​(Stream<?> elements)
        Simple check method for a null safe check of the emptiness of the given parameter. Caution: In order not to consume the stream only a null check will be performed.
        Parameters:
        elements - to be checked, may be null
        Returns:
        true is the given elements are null. The Stream content will be untouched
        Throws:
        IllegalArgumentException - in case the given elements are null
      • containsKey

        public static boolean containsKey​(Map<?,​?> map,
                                          Object... keys)
        Checks whether the given map contains at least one of the given keys to be checked.
        Parameters:
        map - to be checked. If it is null or empty the method will always return false
        keys - to be checked. If it is null or empty the method will always return false
        Returns:
        true if the map contains at lest one of the given keys, false otherwise
      • difference

        public static <K,​V> MapDifference<K,​V> difference​(Map<? extends K,​? extends V> left,
                                                                      Map<? extends K,​? extends V> right)
        Computes the difference between two maps. This difference is an immutable snapshot of the state of the maps at the time this method is called. It will never change, even if the maps change at a later time.

        Since this method uses HashMap instances internally, the keys of the supplied maps must be well-behaved with respect to Object.equals(java.lang.Object) and Object.hashCode().

        Note:If you only need to know whether two maps have the same mappings, call left.equals(right) instead of this method.

        Type Parameters:
        K - the type for the key
        V - the type for the value
        Parameters:
        left - the map to treat as the "left" map for purposes of comparison, must not be null
        right - the map to treat as the "right" map for purposes of comparison , must not be null
        Returns:
        the difference between the two maps