Class ArgChecks

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void arrayLength​(Object array, int minLength, int maxLength, String parameterName)
      Ensures that the given array has the specified minimum and maximum length.
      static void arrayLength​(Object array, int length, String parameterName)
      Ensures that the given array has exactly the specified length.
      static boolean check​(boolean expression, String parameterName, String msg)
      Rejects any expression that evaluates to true.
      static void index​(long index, long length, String parameterName)
      Ensures that the given index lies within the bounds of any list-like structure with the specified length or size.
      static <E> E[] length​(E[] array, int minLength, int maxLength, String parameterName)
      Ensures that the given array has the specified minimum and maximum length.
      static <E> E[] length​(E[] array, int length, String parameterName)
      Ensures that the given array has exactly the specified length.
      static String length​(String string, int minLength, int maxLength, String parameterName)
      Ensures that the given string has the specified minimum and maximum length.
      static String length​(String string, int length, String parameterName)
      Ensures that the given string has exactly the specified length.
      static <N extends Number>
      N
      max​(N number, double max, String parameterName)
      Ensures that the given number is less than or equal to the specified value.
      static <E> E[] maxLength​(E[] array, int maxLength, String parameterName)
      Ensures that the given array has the specified maximum length.
      static String maxLength​(String string, int maxLength, String parameterName)
      Ensures that the given string has the specified maximum length.
      static <E> Collection<E> maxSize​(Collection<E> collection, int maxSize, String parameterName)
      Ensures that the given collection has the specified maximum size.
      static <K,​V>
      Map<K,​V>
      maxSize​(Map<K,​V> map, int maxSize, String parameterName)
      Ensures that the given map has the specified maximum size.
      static <N extends Number>
      N
      min​(N number, double min, String parameterName)
      Ensures that the given number is greater than or equal to the specified value.
      static <E> E[] minLength​(E[] array, int minLength, String parameterName)
      Ensures that the given array has the specified minimum length.
      static String minLength​(String string, int minLength, String parameterName)
      Ensures that the given string has the specified minimum length.
      static <E> Collection<E> minSize​(Collection<E> collection, int minSize, String parameterName)
      Ensures that the given collection has the specified minimum size.
      static <K,​V>
      Map<K,​V>
      minSize​(Map<K,​V> map, int minSize, String parameterName)
      Ensures that the given map has the specified minimum size.
      static String[] noEmptys​(String[] array, String parameterName)
      Ensures that the given array contains no empty strings.
      static <K,​V>
      Map<K,​V>
      noNullKey​(Map<K,​V> map, String parameterName)
      Ensures that the given map contains no null key.
      static <E> E[] noNulls​(E[] array, String parameterName)
      Ensures that the given array contains no null values.
      static <E> Collection<E> noNulls​(Collection<E> collection, String parameterName)
      Ensures that the given collection contains no null values.
      static <K,​V>
      Map<K,​V>
      noNulls​(Map<K,​V> map, String parameterName)
      Ensures that the given map contains neither a null key nor null values.
      static <K,​V>
      Map<K,​V>
      noNullValues​(Map<K,​V> map, String parameterName)
      Ensures that the given map contains no null values.
      static String notBlank​(String string, String parameterName)
      Ensures that the given string is not blank.
      static byte[] notEmpty​(byte[] array, String parameterName)
      Ensures that the given byte array is not empty.
      static <E> E[] notEmpty​(E[] array, String parameterName)
      Ensures that the given array is not empty.
      static String notEmpty​(String string, String parameterName)
      Ensures that the given string is not empty.
      static <E> Collection<E> notEmpty​(Collection<E> collection, String parameterName)
      Ensures that the given collection is not empty.
      static <K,​V>
      Map<K,​V>
      notEmpty​(Map<K,​V> map, String parameterName)
      Ensures that the given map is not empty.
      static <N extends Number>
      N
      notNegative​(N number, String parameterName)
      Ensures that the given number is not negative (i.e.
      static <T> T notNull​(T object, String parameterName)
      Asserts that the parameter is not null.
      static <N extends Number>
      N
      notZero​(N number, String parameterName)
      Ensures that the given number is not zero.
      static String noWhitespace​(String string, String parameterName)
      Ensures that the given string contains no whitespace.
      static String nullOrNotEmpty​(String string, String parameterName)
      Ensures that the given string is either null or not empty.
      static <T> T oneOf​(T object, String parameterName, Object... allowedValues)
      Asserts that the parameter has one of the given values (by testing for equality).
      static <N extends Number>
      N
      positive​(N number, String parameterName)
      Ensures that the given number is positive (i.e.
      static <E> Collection<E> size​(Collection<E> collection, int minSize, int maxSize, String parameterName)
      Ensures that the given collection has the specified minimum and maximum size.
      static <E> Collection<E> size​(Collection<E> collection, int size, String parameterName)
      Ensures that the given collection has exactly the specified size.
      static <K,​V>
      Map<K,​V>
      size​(Map<K,​V> map, int minSize, int maxSize, String parameterName)
      Ensures that the given map has the specified minimum and maximum size.
      static <K,​V>
      Map<K,​V>
      size​(Map<K,​V> map, int size, String parameterName)
      Ensures that the given map has exactly the specified size.
      static <T> T test​(T object, String paramName, Predicate<T> predicate, String predicateName)
      Asserts that the designated value matches the designated predicate.
      static <T> T testNn​(T object, String paramName, Predicate<T> predicate, String predicateName)
      Asserts that the designated value matches the designated predicate.
    • Method Detail

      • check

        public static boolean check​(boolean expression,
                                    String parameterName,
                                    String msg)
        Rejects any expression that evaluates to true. This method is rather generic and can be used if no other method of this class fits the need.
        Parameters:
        expression - true will be rejected
        parameterName - name of the parameter being checked
        msg - the exception message in case the expression does not evaluate to false
        Returns:
        The tested expression.
      • test

        public static <T> T test​(T object,
                                 String paramName,
                                 Predicate<T> predicate,
                                 String predicateName)
        Asserts that the designated value matches the designated predicate. If not, an IllegalArgumentException will be thrown with a message including the designated parameter name and the designated predicate name.
        Type Parameters:
        T - The type of the designated object/parameter value.
        Parameters:
        object - The value of the parameter. This may be null, the designated predicate needs to allow for null.
        paramName - The name of the parameter.
        predicate - The predicate which needs to be matched. This needs to accept null.
        predicateName - The name of the predicate used in the exception message, e. g. "check for null".
        Returns:
        Whether the designated value matches the designated predicate.
      • testNn

        public static <T> T testNn​(T object,
                                   String paramName,
                                   Predicate<T> predicate,
                                   String predicateName)
        Asserts that the designated value matches the designated predicate. If not, an IllegalArgumentException will be thrown with a message including the designated parameter name and the designated predicate name.
        Type Parameters:
        T - The type of the designated object/parameter value.
        Parameters:
        object - The value of the parameter. This must not be null.
        paramName - The name of the parameter.
        predicate - The predicate which needs to be matched. This need not accept null.
        predicateName - The name of the predicate used in the exception message, e. g. "check for null".
        Returns:
        Whether the designated value matches the designated predicate.
      • notNull

        public static <T> T notNull​(T object,
                                    String parameterName)
        Asserts that the parameter is not null.
        Parameters:
        object - The object to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given object.
        Throws:
        NullArgumentException - if the given object is null.
      • oneOf

        public static <T> T oneOf​(T object,
                                  String parameterName,
                                  Object... allowedValues)
        Asserts that the parameter has one of the given values (by testing for equality).
        Parameters:
        object - The object to be tested
        parameterName - The name of the parameter
        allowedValues - The allowed values; may include null
        Returns:
        The given object
        Throws:
        NullArgumentException - if the given object is not one of the allowed values
      • notNegative

        public static <N extends Number> N notNegative​(N number,
                                                       String parameterName)
        Ensures that the given number is not negative (i.e. >= 0).
        Parameters:
        number - The number to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given number.
      • positive

        public static <N extends Number> N positive​(N number,
                                                    String parameterName)
        Ensures that the given number is positive (i.e. > 0).
        Parameters:
        number - The number to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given number.
      • notZero

        public static <N extends Number> N notZero​(N number,
                                                   String parameterName)
        Ensures that the given number is not zero.
        Parameters:
        number - The number to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given number.
      • min

        public static <N extends Number> N min​(N number,
                                               double min,
                                               String parameterName)
        Ensures that the given number is greater than or equal to the specified value.
        Parameters:
        number - The number to be tested.
        min - The smallest acceptable value.
        parameterName - The name of the parameter.
        Returns:
        The given number.
      • max

        public static <N extends Number> N max​(N number,
                                               double max,
                                               String parameterName)
        Ensures that the given number is less than or equal to the specified value.
        Parameters:
        number - The number to be tested.
        max - The largest acceptable value.
        parameterName - The name of the parameter.
        Returns:
        The given number.
      • index

        public static void index​(long index,
                                 long length,
                                 String parameterName)
        Ensures that the given index lies within the bounds of any list-like structure with the specified length or size.
        Parameters:
        index - The index to be tested.
        length - The length or size of the list.
        parameterName - The name of the parameter.
      • notEmpty

        public static String notEmpty​(String string,
                                      String parameterName)
        Ensures that the given string is not empty.
        Parameters:
        string - The string to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • notBlank

        public static String notBlank​(String string,
                                      String parameterName)
        Ensures that the given string is not blank. This method has the same definition of whitespace as String.trim().
        Parameters:
        string - The string to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • nullOrNotEmpty

        public static String nullOrNotEmpty​(String string,
                                            String parameterName)
        Ensures that the given string is either null or not empty.
        Parameters:
        string - The string to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • noWhitespace

        public static String noWhitespace​(String string,
                                          String parameterName)
        Ensures that the given string contains no whitespace. This method has the same definition of whitespace as Character.isWhitespace(char).
        Parameters:
        string - The string to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • minLength

        public static String minLength​(String string,
                                       int minLength,
                                       String parameterName)
        Ensures that the given string has the specified minimum length.
        Parameters:
        string - The string to be tested.
        minLength - The expected minimum length.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • maxLength

        public static String maxLength​(String string,
                                       int maxLength,
                                       String parameterName)
        Ensures that the given string has the specified maximum length.
        Parameters:
        string - The string to be tested.
        maxLength - The expected maximum length.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • length

        public static String length​(String string,
                                    int minLength,
                                    int maxLength,
                                    String parameterName)
        Ensures that the given string has the specified minimum and maximum length.
        Parameters:
        string - The string to be tested.
        minLength - The expected minimum length.
        maxLength - The expected maximum length.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • length

        public static String length​(String string,
                                    int length,
                                    String parameterName)
        Ensures that the given string has exactly the specified length.
        Parameters:
        string - The string to be tested.
        length - The expected length.
        parameterName - The name of the parameter.
        Returns:
        The given string.
      • noNulls

        public static <E> E[] noNulls​(E[] array,
                                      String parameterName)
        Ensures that the given array contains no null values.
        Parameters:
        array - The array to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • notEmpty

        public static byte[] notEmpty​(byte[] array,
                                      String parameterName)
        Ensures that the given byte array is not empty.
        Parameters:
        array - The byte array to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given byte array.
      • notEmpty

        public static <E> E[] notEmpty​(E[] array,
                                       String parameterName)
        Ensures that the given array is not empty.
        Parameters:
        array - The array to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • noEmptys

        public static String[] noEmptys​(String[] array,
                                        String parameterName)
        Ensures that the given array contains no empty strings.
        Parameters:
        array - The strings to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • minLength

        public static <E> E[] minLength​(E[] array,
                                        int minLength,
                                        String parameterName)
        Ensures that the given array has the specified minimum length.
        Parameters:
        array - The array to be tested.
        minLength - The expected minimum length.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • maxLength

        public static <E> E[] maxLength​(E[] array,
                                        int maxLength,
                                        String parameterName)
        Ensures that the given array has the specified maximum length.
        Parameters:
        array - The array to be tested.
        maxLength - The expected maximum length.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • length

        public static <E> E[] length​(E[] array,
                                     int minLength,
                                     int maxLength,
                                     String parameterName)
        Ensures that the given array has the specified minimum and maximum length.
        Parameters:
        array - The array to be tested.
        minLength - The expected minimum length.
        maxLength - The expected maximum length.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • length

        public static <E> E[] length​(E[] array,
                                     int length,
                                     String parameterName)
        Ensures that the given array has exactly the specified length.
        Parameters:
        array - The array to be tested.
        length - The expected length.
        parameterName - The name of the parameter.
        Returns:
        The given array.
      • arrayLength

        public static void arrayLength​(Object array,
                                       int minLength,
                                       int maxLength,
                                       String parameterName)
        Ensures that the given array has the specified minimum and maximum length. This method is intended for the use with arrays of primitive types.
        Parameters:
        array - The array to be tested.
        minLength - The expected minimum length.
        maxLength - The expected maximum length.
        parameterName - The name of the parameter.
      • arrayLength

        public static void arrayLength​(Object array,
                                       int length,
                                       String parameterName)
        Ensures that the given array has exactly the specified length. This method is intended for the use with arrays of primitive types.
        Parameters:
        array - The array to be tested.
        length - The expected length.
        parameterName - The name of the parameter.
      • noNulls

        public static <E> Collection<E> noNulls​(Collection<E> collection,
                                                String parameterName)
        Ensures that the given collection contains no null values.
        Parameters:
        collection - The collection to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given collection.
      • notEmpty

        public static <E> Collection<E> notEmpty​(Collection<E> collection,
                                                 String parameterName)
        Ensures that the given collection is not empty.
        Parameters:
        collection - The collection to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given collection.
      • minSize

        public static <E> Collection<E> minSize​(Collection<E> collection,
                                                int minSize,
                                                String parameterName)
        Ensures that the given collection has the specified minimum size.
        Parameters:
        collection - The collection to be tested.
        minSize - The expected minimum size.
        parameterName - The name of the parameter.
        Returns:
        The given collection.
      • maxSize

        public static <E> Collection<E> maxSize​(Collection<E> collection,
                                                int maxSize,
                                                String parameterName)
        Ensures that the given collection has the specified maximum size.
        Parameters:
        collection - The collection to be tested.
        maxSize - The expected maximum size.
        parameterName - The name of the parameter.
        Returns:
        The given collection.
      • size

        public static <E> Collection<E> size​(Collection<E> collection,
                                             int minSize,
                                             int maxSize,
                                             String parameterName)
        Ensures that the given collection has the specified minimum and maximum size.
        Parameters:
        collection - The collection to be tested.
        minSize - The expected minimum size.
        maxSize - The expected maximum size.
        parameterName - The name of the parameter.
        Returns:
        The given collection.
      • size

        public static <E> Collection<E> size​(Collection<E> collection,
                                             int size,
                                             String parameterName)
        Ensures that the given collection has exactly the specified size.
        Parameters:
        collection - The collection to be tested.
        size - The expected size.
        parameterName - The name of the parameter.
        Returns:
        The given collection.
      • notEmpty

        public static <K,​V> Map<K,​V> notEmpty​(Map<K,​V> map,
                                                          String parameterName)
        Ensures that the given map is not empty.
        Parameters:
        map - The map to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given map.
      • noNullKey

        public static <K,​V> Map<K,​V> noNullKey​(Map<K,​V> map,
                                                           String parameterName)
        Ensures that the given map contains no null key.
        Parameters:
        map - The map to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given map.
      • noNullValues

        public static <K,​V> Map<K,​V> noNullValues​(Map<K,​V> map,
                                                              String parameterName)
        Ensures that the given map contains no null values.
        Parameters:
        map - The map to be tested.
        parameterName - The name of the parameter.
        Returns:
        The given map.
      • minSize

        public static <K,​V> Map<K,​V> minSize​(Map<K,​V> map,
                                                         int minSize,
                                                         String parameterName)
        Ensures that the given map has the specified minimum size.
        Parameters:
        map - The map to be tested.
        minSize - The expected minimum size.
        parameterName - The name of the parameter.
        Returns:
        The given map.
      • maxSize

        public static <K,​V> Map<K,​V> maxSize​(Map<K,​V> map,
                                                         int maxSize,
                                                         String parameterName)
        Ensures that the given map has the specified maximum size.
        Parameters:
        map - The map to be tested.
        maxSize - The expected maximum size.
        parameterName - The name of the parameter.
        Returns:
        The given map.
      • size

        public static <K,​V> Map<K,​V> size​(Map<K,​V> map,
                                                      int minSize,
                                                      int maxSize,
                                                      String parameterName)
        Ensures that the given map has the specified minimum and maximum size.
        Parameters:
        map - The map to be tested.
        minSize - The expected minimum size.
        maxSize - The expected maximum size.
        parameterName - The name of the parameter.
        Returns:
        The given map.
      • size

        public static <K,​V> Map<K,​V> size​(Map<K,​V> map,
                                                      int size,
                                                      String parameterName)
        Ensures that the given map has exactly the specified size.
        Parameters:
        map - The map to be tested.
        size - The expected size.
        parameterName - The name of the parameter.
        Returns:
        The given map.