Class Validate

java.lang.Object
software.amazon.awssdk.utils.Validate

public final class Validate extends Object

This class assists in validating arguments. The validation methods are based along the following principles:

All exceptions messages are format strings as defined by the Java platform. For example:

 Validate.isTrue(i > 0, "The value must be greater than zero: %d", i);
 Validate.notNull(surname, "The surname must not be %s", null);
 

This class's source was modified from the Apache commons-lang library: https://github.com/apache/commons-lang/

#ThreadSafe#

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    exclusiveBetween(double start, double end, double value, String message)
    Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
    static long
    exclusiveBetween(long start, long end, long value, String message)
    Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
    static <T extends Comparable<U>, U>
    T
    exclusiveBetween(U start, U end, T value, String message, Object... values)
    Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception with the specified message.
    static <T> T
    getOrDefault(T param, Supplier<T> defaultValue)
    Returns the param if non null, otherwise gets a default value from the provided Supplier.
    static double
    inclusiveBetween(double start, double end, double value, String message)
    Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
    static long
    inclusiveBetween(long start, long end, long value, String message)
    Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
    static <T extends Comparable<U>, U>
    T
    inclusiveBetween(U start, U end, T value, String message, Object... values)
    Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception with the specified message.
    static <T> Class<? extends T>
    isAssignableFrom(Class<T> superType, Class<?> type, String message, Object... values)
    Validates that the argument can be converted to the specified class, if not throws an exception.
    static void
    isFalse(boolean expression, String message, Object... values)
    Validate that the argument condition is false; otherwise throwing an exception with the specified message.
    static <T, U> U
    isInstanceOf(Class<U> type, T obj, String message, Object... values)
    Validate that the argument is an instance of the specified class; otherwise throwing an exception with the specified message.
    static int
    isNotNegative(int num, String fieldName)
     
    static long
    isNotNegative(long num, String fieldName)
     
    static Duration
    isNotNegative(Duration duration, String fieldName)
    Asserts that the given duration is positive, including zero.
    static Long
    isNotNegativeOrNull(Long num, String fieldName)
     
    static <T> void
    isNull(T object, String message, Object... values)
    Validate that the specified argument is null; otherwise throwing an exception with the specified message.
    static double
    isPositive(double num, String fieldName)
     
    static int
    isPositive(int num, String fieldName)
    Asserts that the given number is positive (non-negative and non-zero).
    static long
    isPositive(long num, String fieldName)
    Asserts that the given number is positive (non-negative and non-zero).
    static Duration
    isPositive(Duration duration, String fieldName)
    Asserts that the given duration is positive (non-negative and non-zero).
    static Double
    isPositiveOrNull(Double num, String fieldName)
    Asserts that the given boxed double is positive (non-negative and non-zero) or null.
    static Integer
    isPositiveOrNull(Integer num, String fieldName)
    Asserts that the given boxed integer is positive (non-negative and non-zero) or null.
    static Long
    isPositiveOrNull(Long num, String fieldName)
    Asserts that the given boxed long is positive (non-negative and non-zero) or null.
    static Duration
    isPositiveOrNull(Duration duration, String fieldName)
    Asserts that the given duration is positive (non-negative and non-zero) or null.
    static void
    isTrue(boolean expression, String message, Object... values)
    Validate that the argument condition is true; otherwise throwing an exception with the specified message.
    static void
    mutuallyExclusive(String message, Object... objs)
    Verify that only one of the objects is non null.
    static <T> T[]
    noNullElements(T[] array, String message, Object... values)
    Validate that the specified argument array is neither null nor contains any elements that are null; otherwise throwing an exception with the specified message.
    static <T extends Iterable<?>>
    T
    noNullElements(T iterable, String message, Object... values)
    Validate that the specified argument iterable is neither null nor contains any elements that are null; otherwise throwing an exception with the specified message.
    static <T extends CharSequence>
    T
    notBlank(T chars, String message, Object... values)
    Validate that the specified argument character sequence is neither null, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.
    static <T> T[]
    notEmpty(T[] array, String message, Object... values)
    Validate that the specified argument array is neither null nor a length of zero (no elements); otherwise throwing an exception with the specified message.
    static <T extends Collection<?>>
    T
    notEmpty(T collection, String message, Object... values)
    Validate that the specified argument collection is neither null nor a size of zero (no elements); otherwise throwing an exception with the specified message.
    static <T> T
    notNull(T object, String message, Object... values)
    Validate that the specified argument is not null; otherwise throwing an exception with the specified message.
    static <T extends CharSequence>
    T
    paramNotBlank(T chars, String paramName)
    Validate that the specified char sequence is neither null, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.
    static <T> T
    paramNotNull(T object, String paramName)
    Validate that the specified field/param is not null; otherwise throwing an exception with a precanned message that includes the parameter name.
    static void
    validState(boolean expression, String message, Object... values)
    Validate that the stateful condition is true; otherwise throwing an exception with the specified message.
    static <T> T
    validState(T object, Predicate<T> test, String message, Object... values)
    Validate the stateful predicate is true for the given object and return the object; otherwise throw an exception with the specified message.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isTrue

      public static void isTrue(boolean expression, String message, Object... values)

      Validate that the argument condition is true; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.

       Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max);
       Validate.isTrue(myObject.isOk(), "The object is not okay");
      Parameters:
      expression - the boolean expression to check
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalArgumentException - if expression is false
    • isFalse

      public static void isFalse(boolean expression, String message, Object... values)

      Validate that the argument condition is false; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.

       Validate.isFalse(myObject.permitsSomething(), "The object is not allowed to permit something");
      Parameters:
      expression - the boolean expression to check
      message - the String.format(String, Object...) exception message if not false, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalArgumentException - if expression is true
    • notNull

      public static <T> T notNull(T object, String message, Object... values)

      Validate that the specified argument is not null; otherwise throwing an exception with the specified message.

      Validate.notNull(myObject, "The object must not be null");
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to check
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message
      Returns:
      the validated object (never null for method chaining)
      Throws:
      NullPointerException - if the object is null
    • isNull

      public static <T> void isNull(T object, String message, Object... values)

      Validate that the specified argument is null; otherwise throwing an exception with the specified message.

      Validate.isNull(myObject, "The object must be null");
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to check
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message
      Throws:
      IllegalArgumentException - if the object is not null
    • paramNotNull

      public static <T> T paramNotNull(T object, String paramName)

      Validate that the specified field/param is not null; otherwise throwing an exception with a precanned message that includes the parameter name.

      Validate.paramNotNull(myObject, "myObject");
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to check
      paramName - The name of the param or field being checked.
      Returns:
      the validated object (never null for method chaining)
      Throws:
      NullPointerException - if the object is null
    • paramNotBlank

      public static <T extends CharSequence> T paramNotBlank(T chars, String paramName)

      Validate that the specified char sequence is neither null, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.

      Validate.paramNotBlank(myCharSequence, "myCharSequence");
      Type Parameters:
      T - the char sequence type
      Parameters:
      chars - the character sequence to check
      paramName - The name of the param or field being checked.
      Returns:
      the validated char sequence (never null for method chaining)
      Throws:
      NullPointerException - if the char sequence is null
    • validState

      public static <T> T validState(T object, Predicate<T> test, String message, Object... values)

      Validate the stateful predicate is true for the given object and return the object; otherwise throw an exception with the specified message.

      String value = Validate.validState(someString, s -> s.length() == 0, "must be blank got: %s", someString);
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to check
      test - the predicate to apply, will return true if the object is valid
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message
      Returns:
      the validated object
      Throws:
      NullPointerException - if the object is null
    • notEmpty

      public static <T> T[] notEmpty(T[] array, String message, Object... values)

      Validate that the specified argument array is neither null nor a length of zero (no elements); otherwise throwing an exception with the specified message.

      Validate.notEmpty(myArray, "The array must not be empty");
      Type Parameters:
      T - the array type
      Parameters:
      array - the array to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Returns:
      the validated array (never null method for chaining)
      Throws:
      NullPointerException - if the array is null
      IllegalArgumentException - if the array is empty
    • notEmpty

      public static <T extends Collection<?>> T notEmpty(T collection, String message, Object... values)

      Validate that the specified argument collection is neither null nor a size of zero (no elements); otherwise throwing an exception with the specified message.

      Validate.notEmpty(myCollection, "The collection must not be empty");
      Type Parameters:
      T - the collection type
      Parameters:
      collection - the collection to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Returns:
      the validated collection (never null method for chaining)
      Throws:
      NullPointerException - if the collection is null
      IllegalArgumentException - if the collection is empty
    • notEmpty

      public static <T extends Map<?, ?>> T notEmpty(T map, String message, Object... values)

      Validate that the specified argument map is neither null nor a size of zero (no elements); otherwise throwing an exception with the specified message.

      Validate.notEmpty(myMap, "The map must not be empty");
      Type Parameters:
      T - the map type
      Parameters:
      map - the map to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Returns:
      the validated map (never null method for chaining)
      Throws:
      NullPointerException - if the map is null
      IllegalArgumentException - if the map is empty
    • notEmpty

      public static <T extends CharSequence> T notEmpty(T chars, String message, Object... values)

      Validate that the specified argument character sequence is neither null nor a length of zero (no characters); otherwise throwing an exception with the specified message.

      Validate.notEmpty(myString, "The string must not be empty");
      Type Parameters:
      T - the character sequence type
      Parameters:
      chars - the character sequence to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Returns:
      the validated character sequence (never null method for chaining)
      Throws:
      NullPointerException - if the character sequence is null
      IllegalArgumentException - if the character sequence is empty
    • notBlank

      public static <T extends CharSequence> T notBlank(T chars, String message, Object... values)

      Validate that the specified argument character sequence is neither null, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.

      Validate.notBlank(myString, "The string must not be blank");
      Type Parameters:
      T - the character sequence type
      Parameters:
      chars - the character sequence to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Returns:
      the validated character sequence (never null method for chaining)
      Throws:
      NullPointerException - if the character sequence is null
      IllegalArgumentException - if the character sequence is blank
    • noNullElements

      public static <T> T[] noNullElements(T[] array, String message, Object... values)

      Validate that the specified argument array is neither null nor contains any elements that are null; otherwise throwing an exception with the specified message.

      Validate.noNullElements(myArray, "The array is null or contains null.");
      Type Parameters:
      T - the array type
      Parameters:
      array - the array to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message
      Returns:
      the validated array (never null method for chaining)
      Throws:
      NullPointerException - if the array is null
      IllegalArgumentException - if an element is null
    • noNullElements

      public static <T extends Iterable<?>> T noNullElements(T iterable, String message, Object... values)

      Validate that the specified argument iterable is neither null nor contains any elements that are null; otherwise throwing an exception with the specified message.

      Validate.noNullElements(myCollection, "The collection is null or contains null.");
      Type Parameters:
      T - the iterable type
      Parameters:
      iterable - the iterable to check, validated not null by this method
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message.
      Returns:
      the validated iterable (never null method for chaining)
      Throws:
      NullPointerException - if the array is null
      IllegalArgumentException - if an element is null
    • validState

      public static void validState(boolean expression, String message, Object... values)

      Validate that the stateful condition is true; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.

      Validate.validState(this.isOk(), "The state is not OK: %s", myObject);
      Parameters:
      expression - the boolean expression to check
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalStateException - if expression is false
    • inclusiveBetween

      public static <T extends Comparable<U>, U> T inclusiveBetween(U start, U end, T value, String message, Object... values)

      Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception with the specified message.

      Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");
      Type Parameters:
      T - the type of the argument object
      Parameters:
      start - the inclusive start value, not null
      end - the inclusive end value, not null
      value - the object to validate, not null
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalArgumentException - if the value falls outside the boundaries
    • inclusiveBetween

      public static long inclusiveBetween(long start, long end, long value, String message)
      Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
      Validate.inclusiveBetween(0, 2, 1, "Not in range");
      Parameters:
      start - the inclusive start value
      end - the inclusive end value
      value - the value to validate
      message - the exception message if invalid, not null
      Throws:
      IllegalArgumentException - if the value falls outside the boundaries
    • inclusiveBetween

      public static double inclusiveBetween(double start, double end, double value, String message)
      Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
      Validate.inclusiveBetween(0.1, 2.1, 1.1, "Not in range");
      Parameters:
      start - the inclusive start value
      end - the inclusive end value
      value - the value to validate
      message - the exception message if invalid, not null
      Throws:
      IllegalArgumentException - if the value falls outside the boundaries
    • exclusiveBetween

      public static <T extends Comparable<U>, U> T exclusiveBetween(U start, U end, T value, String message, Object... values)

      Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception with the specified message.

      Validate.exclusiveBetween(0, 2, 1, "Not in boundaries");
      Type Parameters:
      T - the type of the argument object
      Parameters:
      start - the exclusive start value, not null
      end - the exclusive end value, not null
      value - the object to validate, not null
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalArgumentException - if the value falls outside the boundaries
    • exclusiveBetween

      public static long exclusiveBetween(long start, long end, long value, String message)
      Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
      Validate.exclusiveBetween(0, 2, 1, "Not in range");
      Parameters:
      start - the exclusive start value
      end - the exclusive end value
      value - the value to validate
      message - the exception message if invalid, not null
      Throws:
      IllegalArgumentException - if the value falls outside the boundaries
    • exclusiveBetween

      public static double exclusiveBetween(double start, double end, double value, String message)
      Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
      Validate.exclusiveBetween(0.1, 2.1, 1.1, "Not in range");
      Parameters:
      start - the exclusive start value
      end - the exclusive end value
      value - the value to validate
      message - the exception message if invalid, not null
      Throws:
      IllegalArgumentException - if the value falls outside the boundaries
    • isInstanceOf

      public static <T, U> U isInstanceOf(Class<U> type, T obj, String message, Object... values)

      Validate that the argument is an instance of the specified class; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary class

      Validate.isInstanceOf(OkClass.class, object, "Wrong class, object is of class %s",
         object.getClass().getName());
      Parameters:
      type - the class the object must be validated against, not null
      obj - the object to check, null throws an exception
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalArgumentException - if argument is not of specified class
    • isAssignableFrom

      public static <T> Class<? extends T> isAssignableFrom(Class<T> superType, Class<?> type, String message, Object... values)
      Validates that the argument can be converted to the specified class, if not throws an exception.

      This method is useful when validating if there will be no casting errors.

      Validate.isAssignableFrom(SuperClass.class, object.getClass());

      The message of the exception is "The validated object can not be converted to the" followed by the name of the class and "class"

      Parameters:
      superType - the class the class must be validated against, not null
      type - the class to check, not null
      message - the String.format(String, Object...) exception message if invalid, not null
      values - the optional values for the formatted exception message, null array not recommended
      Throws:
      IllegalArgumentException - if argument can not be converted to the specified class
    • isPositive

      public static int isPositive(int num, String fieldName)
      Asserts that the given number is positive (non-negative and non-zero).
      Parameters:
      num - Number to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Number if positive.
    • isPositive

      public static long isPositive(long num, String fieldName)
      Asserts that the given number is positive (non-negative and non-zero).
      Parameters:
      num - Number to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Number if positive.
    • isPositive

      public static double isPositive(double num, String fieldName)
    • isNotNegative

      public static int isNotNegative(int num, String fieldName)
    • isNotNegativeOrNull

      public static Long isNotNegativeOrNull(Long num, String fieldName)
    • isNotNegative

      public static long isNotNegative(long num, String fieldName)
    • isPositive

      public static Duration isPositive(Duration duration, String fieldName)
      Asserts that the given duration is positive (non-negative and non-zero).
      Parameters:
      duration - Number to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Duration if positive.
    • isPositiveOrNull

      public static Duration isPositiveOrNull(Duration duration, String fieldName)
      Asserts that the given duration is positive (non-negative and non-zero) or null.
      Parameters:
      duration - Number to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Duration if positive or null.
    • isPositiveOrNull

      public static Integer isPositiveOrNull(Integer num, String fieldName)
      Asserts that the given boxed integer is positive (non-negative and non-zero) or null.
      Parameters:
      num - Boxed integer to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Duration if positive or null.
    • isPositiveOrNull

      public static Double isPositiveOrNull(Double num, String fieldName)
      Asserts that the given boxed double is positive (non-negative and non-zero) or null.
      Parameters:
      num - Boxed double to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Duration if double or null.
    • isPositiveOrNull

      public static Long isPositiveOrNull(Long num, String fieldName)
      Asserts that the given boxed long is positive (non-negative and non-zero) or null.
      Parameters:
      num - Boxed long to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Duration if positive or null.
    • isNotNegative

      public static Duration isNotNegative(Duration duration, String fieldName)
      Asserts that the given duration is positive, including zero.
      Parameters:
      duration - Number to validate
      fieldName - Field name to display in exception message if not positive.
      Returns:
      Duration if positive or zero.
    • getOrDefault

      public static <T> T getOrDefault(T param, Supplier<T> defaultValue)
      Returns the param if non null, otherwise gets a default value from the provided Supplier.
      Type Parameters:
      T - Type of value.
      Parameters:
      param - Param to return if non null.
      defaultValue - Supplier of default value.
      Returns:
      Value of param or default value if param was null.
    • mutuallyExclusive

      public static void mutuallyExclusive(String message, Object... objs)
      Verify that only one of the objects is non null. If all objects are null this method does not throw.
      Parameters:
      message - Error message if more than one object is non-null.
      objs - Objects to validate.
      Throws:
      IllegalArgumentException - if more than one of the objects was non-null.