Class NullUtil


  • public final class NullUtil
    extends Object
    Utility class for checking invariants on null-values.
    Author:
    Emil Forslund
    • Method Detail

      • requireNonNullElements

        public static <T> T[] requireNonNullElements​(T[] array)
        Checks if this array is non null and also that all members are non-null. If a null is detected a NullPointerException is thrown.
        Type Parameters:
        T - array type
        Parameters:
        array - to check
        Returns:
        the same array instance as provided
        Throws:
        NullPointerException - if a null is found in the array or if the array itself is null
      • requireNonNullElements

        public static <E,​T extends Collection<E>> T requireNonNullElements​(T collection)
        Checks if this collection is non null and also that all members are non-null. If a null is detected a NullPointerException is thrown.
        Type Parameters:
        E - the inner type of the collection
        T - collection type
        Parameters:
        collection - to check
        Returns:
        the same collection instance as provided
        Throws:
        NullPointerException - if a null is found in the collection or if the array itself is null
      • requireNonNullElements

        public static <T> T[] requireNonNullElements​(T[] array,
                                                     String msg)
        Checks if this array is non null and also that all members are non-null. If a null is detected a NullPointerException is thrown.
        Type Parameters:
        T - array type
        Parameters:
        array - to check
        msg - to show if a null is detected
        Returns:
        the same array instance as provided
        Throws:
        NullPointerException - if a null is found in the array or if the array itself is null
      • requireNonNulls

        public static void requireNonNulls​(Object o0)
        Checks if this element is non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        Throws:
        NullPointerException - if the element is null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        o1 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1,
                                           Object o2)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        o1 - object to check
        o2 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1,
                                           Object o2,
                                           Object o3)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        o1 - object to check
        o2 - object to check
        o3 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1,
                                           Object o2,
                                           Object o3,
                                           Object o4)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        o1 - object to check
        o2 - object to check
        o3 - object to check
        o4 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1,
                                           Object o2,
                                           Object o3,
                                           Object o4,
                                           Object o5)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown. s
        Parameters:
        o0 - object to check
        o1 - object to check
        o2 - object to check
        o3 - object to check
        o4 - object to check
        o5 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1,
                                           Object o2,
                                           Object o3,
                                           Object o4,
                                           Object o5,
                                           Object o6)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        o1 - object to check
        o2 - object to check
        o3 - object to check
        o4 - object to check
        o5 - object to check
        o6 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireNonNulls

        public static void requireNonNulls​(Object o0,
                                           Object o1,
                                           Object o2,
                                           Object o3,
                                           Object o4,
                                           Object o5,
                                           Object o6,
                                           Object o7)
        Checks if the provided elements all are non null. If a null is detected a NullPointerException is thrown.
        Parameters:
        o0 - object to check
        o1 - object to check
        o2 - object to check
        o3 - object to check
        o4 - object to check
        o5 - object to check
        o6 - object to check
        o7 - object to check
        Throws:
        NullPointerException - if at least one of the elements are null
      • requireKeys

        @SafeVarargs
        public static <K,​V> Map<K,​V> requireKeys​(Map<K,​V> map,
                                                             K... requiredKeys)
        Checks if the provided map contains all the provided required non-null key(s).
        Type Parameters:
        K - key type
        V - value type
        Parameters:
        map - to check
        requiredKeys - to check for existence
        Returns:
        the same instance of the provided map
        Throws:
        NoSuchElementException - if at least one required key is not a key in the provided Map
        NullPointerException - if the provided Map is null or if at least one of the required key are null
      • requireKeys

        public static <K,​V> Map<K,​V> requireKeys​(Map<K,​V> map,
                                                             K requiredKey)
        Checks if the provided map contains all the provided required non-null key(s).
        Type Parameters:
        K - key type
        V - value type
        Parameters:
        map - to check
        requiredKey - to check for existence
        Returns:
        the same instance of the provided map
        Throws:
        NoSuchElementException - if the required key is not a key in the provided Map
        NullPointerException - if the provided Map is null or if the required key is null
      • requireKeys

        public static <K,​V> Map<K,​V> requireKeys​(Map<K,​V> map,
                                                             K requiredKeyA,
                                                             K requiredKeyB)
        Checks if the provided map contains all the provided required key(s).
        Type Parameters:
        K - key type
        V - value type
        Parameters:
        map - to check
        requiredKeyA - to check for existence
        requiredKeyB - to check for existence
        Returns:
        the same instance of the provided map
        Throws:
        NoSuchElementException - if the required keys are not keys in the provided Map
        NullPointerException - if the provided Map is null
      • requireKeys

        public static <K,​V> Map<K,​V> requireKeys​(Map<K,​V> map,
                                                             K requiredKeyA,
                                                             K requiredKeyB,
                                                             K requiredKeyC)
        Checks if the provided map contains all the provided required key(s).
        Type Parameters:
        K - key type
        V - value type
        Parameters:
        map - to check
        requiredKeyA - to check for existence
        requiredKeyB - to check for existence
        requiredKeyC - to check for existence
        Returns:
        the same instance of the provided map
        Throws:
        NoSuchElementException - if the required keys are not keys in the provided Map
        NullPointerException - if the provided Map is null