Class Validate


  • public class Validate
    extends java.lang.Object

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

    • An invalid null argument causes a NullPointerException.
    • A non-null argument causes an IllegalArgumentException.
    • An invalid index into an array/collection/map/string causes an IndexOutOfBoundsException.

    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);
     

    #ThreadSafe#

    Since:
    2.0
    See Also:
    String.format(String, Object...)
    • Constructor Summary

      Constructors 
      Constructor Description
      Validate()
      Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void exclusiveBetween​(double start, double end, double value)
      Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception.
      static void exclusiveBetween​(double start, double end, double value, java.lang.String message)
      Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
      static void exclusiveBetween​(long start, long end, long value)
      Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception.
      static void exclusiveBetween​(long start, long end, long value, java.lang.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> void exclusiveBetween​(T start, T end, java.lang.Comparable<T> value)
      Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception.
      static <T> void exclusiveBetween​(T start, T end, java.lang.Comparable<T> value, java.lang.String message, java.lang.Object... values)
      Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception with the specified message.
      static void finite​(double value)
      Validates that the specified argument is not infinite or NaN; otherwise throwing an exception.
      static void finite​(double value, java.lang.String message, java.lang.Object... values)
      Validates that the specified argument is not infinite or NaN; otherwise throwing an exception with the specified message.
      static void inclusiveBetween​(double start, double end, double value)
      Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception.
      static void inclusiveBetween​(double start, double end, double value, java.lang.String message)
      Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
      static void inclusiveBetween​(long start, long end, long value)
      Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception.
      static void inclusiveBetween​(long start, long end, long value, java.lang.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> void inclusiveBetween​(T start, T end, java.lang.Comparable<T> value)
      Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception.
      static <T> void inclusiveBetween​(T start, T end, java.lang.Comparable<T> value, java.lang.String message, java.lang.Object... values)
      Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception with the specified message.
      static void isAssignableFrom​(java.lang.Class<?> superType, java.lang.Class<?> type)
      Validates that the argument can be converted to the specified class, if not, throws an exception.
      static void isAssignableFrom​(java.lang.Class<?> superType, java.lang.Class<?> type, java.lang.String message, java.lang.Object... values)
      Validates that the argument can be converted to the specified class, if not throws an exception.
      static void isInstanceOf​(java.lang.Class<?> type, java.lang.Object obj)
      Validates that the argument is an instance of the specified class, if not throws an exception.
      static void isInstanceOf​(java.lang.Class<?> type, java.lang.Object obj, java.lang.String message, java.lang.Object... values)
      Validate that the argument is an instance of the specified class; otherwise throwing an exception with the specified message.
      static void isTrue​(boolean expression)
      Validate that the argument condition is true; otherwise throwing an exception.
      static void isTrue​(boolean expression, java.lang.String message, double value)
      Validate that the argument condition is true; otherwise throwing an exception with the specified message.
      static void isTrue​(boolean expression, java.lang.String message, long value)
      Validate that the argument condition is true; otherwise throwing an exception with the specified message.
      static void isTrue​(boolean expression, java.lang.String message, java.lang.Object... values)
      Validate that the argument condition is true; otherwise throwing an exception with the specified message.
      static void matchesPattern​(java.lang.CharSequence input, java.lang.String pattern)
      Validate that the specified argument character sequence matches the specified regular expression pattern; otherwise throwing an exception.
      static void matchesPattern​(java.lang.CharSequence input, java.lang.String pattern, java.lang.String message, java.lang.Object... values)
      Validate that the specified argument character sequence matches the specified regular expression pattern; otherwise throwing an exception with the specified message.
      static <T extends java.lang.Iterable<?>>
      T
      noNullElements​(T iterable)
      Validate that the specified argument iterable is neither null nor contains any elements that are null; otherwise throwing an exception.
      static <T> T[] noNullElements​(T[] array)
      Validate that the specified argument array is neither null nor contains any elements that are null; otherwise throwing an exception.
      static <T> T[] noNullElements​(T[] array, java.lang.String message, java.lang.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 java.lang.Iterable<?>>
      T
      noNullElements​(T iterable, java.lang.String message, java.lang.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 java.lang.CharSequence>
      T
      notBlank​(T chars)
      Validate that the specified argument character sequence is neither null, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception.
      static <T extends java.lang.CharSequence>
      T
      notBlank​(T chars, java.lang.String message, java.lang.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 extends java.util.Collection<?>>
      T
      notEmpty​(T collection)
      Validate that the specified argument collection is neither null nor a size of zero (no elements); otherwise throwing an exception.
      static <T> T[] notEmpty​(T[] array)
      Validate that the specified argument array is neither null nor a length of zero (no elements); otherwise throwing an exception.
      static <T> T[] notEmpty​(T[] array, java.lang.String message, java.lang.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 java.util.Collection<?>>
      T
      notEmpty​(T collection, java.lang.String message, java.lang.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 void notNaN​(double value)
      Validates that the specified argument is not NaN; otherwise throwing an exception.
      static void notNaN​(double value, java.lang.String message, java.lang.Object... values)
      Validates that the specified argument is not NaN; otherwise throwing an exception with the specified message.
      static <T> T notNull​(T object)
      Validate that the specified argument is not null; otherwise throwing an exception.
      static <T> T notNull​(T object, java.lang.String message, java.lang.Object... values)
      Validate that the specified argument is not null; otherwise throwing an exception with the specified message.
      static <T> T[] validIndex​(T[] array, int index)
      Validates that the index is within the bounds of the argument array; otherwise throwing an exception.
      static <T> T[] validIndex​(T[] array, int index, java.lang.String message, java.lang.Object... values)
      Validates that the index is within the bounds of the argument array; otherwise throwing an exception with the specified message.
      static <T extends java.util.Collection<?>>
      T
      validIndex​(T collection, int index)
      Validates that the index is within the bounds of the argument collection; otherwise throwing an exception.
      static <T extends java.util.Collection<?>>
      T
      validIndex​(T collection, int index, java.lang.String message, java.lang.Object... values)
      Validates that the index is within the bounds of the argument collection; otherwise throwing an exception with the specified message.
      static void validState​(boolean expression)
      Validate that the stateful condition is true; otherwise throwing an exception.
      static void validState​(boolean expression, java.lang.String message, java.lang.Object... values)
      Validate that the stateful condition is true; otherwise throwing an exception with the specified message.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Validate

        public Validate()
        Constructor. This class should not normally be instantiated.
    • Method Detail

      • isTrue

        public static void isTrue​(boolean expression,
                                  java.lang.String message,
                                  long value)

        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 > 0.0, "The value must be greater than zero: %d", i);

        For performance reasons, the long value is passed as a separate parameter and appended to the exception message only in the case of an error.

        Parameters:
        expression - the boolean expression to check
        message - the String.format(String, Object...) exception message if invalid, not null
        value - the value to append to the message when invalid
        Throws:
        java.lang.IllegalArgumentException - if expression is false
        See Also:
        isTrue(boolean), isTrue(boolean, String, double), isTrue(boolean, String, Object...)
      • isTrue

        public static void isTrue​(boolean expression,
                                  java.lang.String message,
                                  double value)

        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(d > 0.0, "The value must be greater than zero: %s", d);

        For performance reasons, the double value is passed as a separate parameter and appended to the exception message only in the case of an error.

        Parameters:
        expression - the boolean expression to check
        message - the String.format(String, Object...) exception message if invalid, not null
        value - the value to append to the message when invalid
        Throws:
        java.lang.IllegalArgumentException - if expression is false
        See Also:
        isTrue(boolean), isTrue(boolean, String, long), isTrue(boolean, String, Object...)
      • isTrue

        public static void isTrue​(boolean expression,
                                  java.lang.String message,
                                  java.lang.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:
        java.lang.IllegalArgumentException - if expression is false
        See Also:
        isTrue(boolean), isTrue(boolean, String, long), isTrue(boolean, String, double)
      • isTrue

        public static void isTrue​(boolean expression)

        Validate that the argument condition is true; otherwise throwing an exception. 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 > 0);
         Validate.isTrue(myObject.isOk());

        The message of the exception is "The validated expression is false".

        Parameters:
        expression - the boolean expression to check
        Throws:
        java.lang.IllegalArgumentException - if expression is false
        See Also:
        isTrue(boolean, String, long), isTrue(boolean, String, double), isTrue(boolean, String, Object...)
      • notNull

        public static <T> T notNull​(T object)

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

        Validate.notNull(myObject, "The object must not be null");

        The message of the exception is "The validated object is null".

        Type Parameters:
        T - the object type
        Parameters:
        object - the object to check
        Returns:
        the validated object (never null for method chaining)
        Throws:
        java.lang.NullPointerException - if the object is null
        See Also:
        notNull(Object, String, Object...)
      • notNull

        public static <T> T notNull​(T object,
                                    java.lang.String message,
                                    java.lang.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:
        java.lang.NullPointerException - if the object is null
        See Also:
        notNull(Object)
      • notEmpty

        public static <T> T[] notEmpty​(T[] array,
                                       java.lang.String message,
                                       java.lang.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:
        java.lang.NullPointerException - if the array is null
        java.lang.IllegalArgumentException - if the array is empty
        See Also:
        notEmpty(Object[])
      • notEmpty

        public static <T> T[] notEmpty​(T[] array)

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

        Validate.notEmpty(myArray);

        The message in the exception is "The validated array is empty".

        Type Parameters:
        T - the array type
        Parameters:
        array - the array to check, validated not null by this method
        Returns:
        the validated array (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the array is null
        java.lang.IllegalArgumentException - if the array is empty
        See Also:
        notEmpty(Object[], String, Object...)
      • notEmpty

        public static <T extends java.util.Collection<?>> T notEmpty​(T collection,
                                                                     java.lang.String message,
                                                                     java.lang.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:
        java.lang.NullPointerException - if the collection is null
        java.lang.IllegalArgumentException - if the collection is empty
        See Also:
        notEmpty(Object[])
      • notEmpty

        public static <T extends java.util.Collection<?>> T notEmpty​(T collection)

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

        Validate.notEmpty(myCollection);

        The message in the exception is "The validated collection is empty".

        Type Parameters:
        T - the collection type
        Parameters:
        collection - the collection to check, validated not null by this method
        Returns:
        the validated collection (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the collection is null
        java.lang.IllegalArgumentException - if the collection is empty
        See Also:
        notEmpty(Collection, String, Object...)
      • notEmpty

        public static <T extends java.util.Map<?,​?>> T notEmpty​(T map,
                                                                      java.lang.String message,
                                                                      java.lang.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:
        java.lang.NullPointerException - if the map is null
        java.lang.IllegalArgumentException - if the map is empty
        See Also:
        notEmpty(Object[])
      • notEmpty

        public static <T extends java.util.Map<?,​?>> T notEmpty​(T map)

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

        Validate.notEmpty(myMap);

        The message in the exception is "The validated map is empty".

        Type Parameters:
        T - the map type
        Parameters:
        map - the map to check, validated not null by this method
        Returns:
        the validated map (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the map is null
        java.lang.IllegalArgumentException - if the map is empty
        See Also:
        notEmpty(Map, String, Object...)
      • notEmpty

        public static <T extends java.lang.CharSequence> T notEmpty​(T chars,
                                                                    java.lang.String message,
                                                                    java.lang.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:
        java.lang.NullPointerException - if the character sequence is null
        java.lang.IllegalArgumentException - if the character sequence is empty
        See Also:
        notEmpty(CharSequence)
      • notEmpty

        public static <T extends java.lang.CharSequence> T notEmpty​(T chars)

        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 message in the exception is "The validated character sequence is empty".

        Type Parameters:
        T - the character sequence type
        Parameters:
        chars - the character sequence to check, validated not null by this method
        Returns:
        the validated character sequence (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the character sequence is null
        java.lang.IllegalArgumentException - if the character sequence is empty
        See Also:
        notEmpty(CharSequence, String, Object...)
      • notBlank

        public static <T extends java.lang.CharSequence> T notBlank​(T chars,
                                                                    java.lang.String message,
                                                                    java.lang.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:
        java.lang.NullPointerException - if the character sequence is null
        java.lang.IllegalArgumentException - if the character sequence is blank
        Since:
        3.0
        See Also:
        notBlank(CharSequence)
      • notBlank

        public static <T extends java.lang.CharSequence> T notBlank​(T chars)

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

        Validate.notBlank(myString);

        The message in the exception is "The validated character sequence is blank".

        Type Parameters:
        T - the character sequence type
        Parameters:
        chars - the character sequence to check, validated not null by this method
        Returns:
        the validated character sequence (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the character sequence is null
        java.lang.IllegalArgumentException - if the character sequence is blank
        Since:
        3.0
        See Also:
        notBlank(CharSequence, String, Object...)
      • noNullElements

        public static <T> T[] noNullElements​(T[] array,
                                             java.lang.String message,
                                             java.lang.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 contain null at position %d");

        If the array is null, then the message in the exception is "The validated object is null".

        If the array has a null element, then the iteration index of the invalid element is appended to the values argument.

        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:
        java.lang.NullPointerException - if the array is null
        java.lang.IllegalArgumentException - if an element is null
        See Also:
        noNullElements(Object[])
      • noNullElements

        public static <T> T[] noNullElements​(T[] array)

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

        Validate.noNullElements(myArray);

        If the array is null, then the message in the exception is "The validated object is null".

        If the array has a null element, then the message in the exception is "The validated array contains null element at index: " followed by the index.

        Type Parameters:
        T - the array type
        Parameters:
        array - the array to check, validated not null by this method
        Returns:
        the validated array (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the array is null
        java.lang.IllegalArgumentException - if an element is null
        See Also:
        noNullElements(Object[], String, Object...)
      • noNullElements

        public static <T extends java.lang.Iterable<?>> T noNullElements​(T iterable,
                                                                         java.lang.String message,
                                                                         java.lang.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 contains null at position %d");

        If the iterable is null, then the message in the exception is "The validated object is null".

        If the iterable has a null element, then the iteration index of the invalid element is appended to the values argument.

        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, null array not recommended
        Returns:
        the validated iterable (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the array is null
        java.lang.IllegalArgumentException - if an element is null
        See Also:
        noNullElements(Iterable)
      • noNullElements

        public static <T extends java.lang.Iterable<?>> T noNullElements​(T iterable)

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

        Validate.noNullElements(myCollection);

        If the iterable is null, then the message in the exception is "The validated object is null".

        If the array has a null element, then the message in the exception is "The validated iterable contains null element at index: " followed by the index.

        Type Parameters:
        T - the iterable type
        Parameters:
        iterable - the iterable to check, validated not null by this method
        Returns:
        the validated iterable (never null method for chaining)
        Throws:
        java.lang.NullPointerException - if the array is null
        java.lang.IllegalArgumentException - if an element is null
        See Also:
        noNullElements(Iterable, String, Object...)
      • validIndex

        public static <T> T[] validIndex​(T[] array,
                                         int index,
                                         java.lang.String message,
                                         java.lang.Object... values)

        Validates that the index is within the bounds of the argument array; otherwise throwing an exception with the specified message.

        Validate.validIndex(myArray, 2, "The array index is invalid: ");

        If the array is null, then the message of the exception is "The validated object is null".

        Type Parameters:
        T - the array type
        Parameters:
        array - the array to check, validated not null by this method
        index - the index 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
        Returns:
        the validated array (never null for method chaining)
        Throws:
        java.lang.NullPointerException - if the array is null
        java.lang.IndexOutOfBoundsException - if the index is invalid
        Since:
        3.0
        See Also:
        validIndex(Object[], int)
      • validIndex

        public static <T> T[] validIndex​(T[] array,
                                         int index)

        Validates that the index is within the bounds of the argument array; otherwise throwing an exception.

        Validate.validIndex(myArray, 2);

        If the array is null, then the message of the exception is "The validated object is null".

        If the index is invalid, then the message of the exception is "The validated array index is invalid: " followed by the index.

        Type Parameters:
        T - the array type
        Parameters:
        array - the array to check, validated not null by this method
        index - the index to check
        Returns:
        the validated array (never null for method chaining)
        Throws:
        java.lang.NullPointerException - if the array is null
        java.lang.IndexOutOfBoundsException - if the index is invalid
        Since:
        3.0
        See Also:
        validIndex(Object[], int, String, Object...)
      • validIndex

        public static <T extends java.util.Collection<?>> T validIndex​(T collection,
                                                                       int index,
                                                                       java.lang.String message,
                                                                       java.lang.Object... values)

        Validates that the index is within the bounds of the argument collection; otherwise throwing an exception with the specified message.

        Validate.validIndex(myCollection, 2, "The collection index is invalid: ");

        If the collection is null, then the message of the exception is "The validated object is null".

        Type Parameters:
        T - the collection type
        Parameters:
        collection - the collection to check, validated not null by this method
        index - the index 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
        Returns:
        the validated collection (never null for chaining)
        Throws:
        java.lang.NullPointerException - if the collection is null
        java.lang.IndexOutOfBoundsException - if the index is invalid
        Since:
        3.0
        See Also:
        validIndex(Collection, int)
      • validIndex

        public static <T extends java.util.Collection<?>> T validIndex​(T collection,
                                                                       int index)

        Validates that the index is within the bounds of the argument collection; otherwise throwing an exception.

        Validate.validIndex(myCollection, 2);

        If the index is invalid, then the message of the exception is "The validated collection index is invalid: " followed by the index.

        Type Parameters:
        T - the collection type
        Parameters:
        collection - the collection to check, validated not null by this method
        index - the index to check
        Returns:
        the validated collection (never null for method chaining)
        Throws:
        java.lang.NullPointerException - if the collection is null
        java.lang.IndexOutOfBoundsException - if the index is invalid
        Since:
        3.0
        See Also:
        validIndex(Collection, int, String, Object...)
      • validIndex

        public static <T extends java.lang.CharSequence> T validIndex​(T chars,
                                                                      int index,
                                                                      java.lang.String message,
                                                                      java.lang.Object... values)

        Validates that the index is within the bounds of the argument character sequence; otherwise throwing an exception with the specified message.

        Validate.validIndex(myStr, 2, "The string index is invalid: ");

        If the character sequence is null, then the message of the exception is "The validated object is null".

        Type Parameters:
        T - the character sequence type
        Parameters:
        chars - the character sequence to check, validated not null by this method
        index - the index 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
        Returns:
        the validated character sequence (never null for method chaining)
        Throws:
        java.lang.NullPointerException - if the character sequence is null
        java.lang.IndexOutOfBoundsException - if the index is invalid
        Since:
        3.0
        See Also:
        validIndex(CharSequence, int)
      • validIndex

        public static <T extends java.lang.CharSequence> T validIndex​(T chars,
                                                                      int index)

        Validates that the index is within the bounds of the argument character sequence; otherwise throwing an exception.

        Validate.validIndex(myStr, 2);

        If the character sequence is null, then the message of the exception is "The validated object is null".

        If the index is invalid, then the message of the exception is "The validated character sequence index is invalid: " followed by the index.

        Type Parameters:
        T - the character sequence type
        Parameters:
        chars - the character sequence to check, validated not null by this method
        index - the index to check
        Returns:
        the validated character sequence (never null for method chaining)
        Throws:
        java.lang.NullPointerException - if the character sequence is null
        java.lang.IndexOutOfBoundsException - if the index is invalid
        Since:
        3.0
        See Also:
        validIndex(CharSequence, int, String, Object...)
      • validState

        public static void validState​(boolean expression)

        Validate that the stateful condition is true; otherwise throwing an exception. 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(field > 0);
         Validate.validState(this.isOk());

        The message of the exception is "The validated state is false".

        Parameters:
        expression - the boolean expression to check
        Throws:
        java.lang.IllegalStateException - if expression is false
        Since:
        3.0
        See Also:
        validState(boolean, String, Object...)
      • validState

        public static void validState​(boolean expression,
                                      java.lang.String message,
                                      java.lang.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:
        java.lang.IllegalStateException - if expression is false
        Since:
        3.0
        See Also:
        validState(boolean)
      • matchesPattern

        public static void matchesPattern​(java.lang.CharSequence input,
                                          java.lang.String pattern)

        Validate that the specified argument character sequence matches the specified regular expression pattern; otherwise throwing an exception.

        Validate.matchesPattern("hi", "[a-z]*");

        The syntax of the pattern is the one used in the Pattern class.

        Parameters:
        input - the character sequence to validate, not null
        pattern - the regular expression pattern, not null
        Throws:
        java.lang.IllegalArgumentException - if the character sequence does not match the pattern
        Since:
        3.0
        See Also:
        matchesPattern(CharSequence, String, String, Object...)
      • matchesPattern

        public static void matchesPattern​(java.lang.CharSequence input,
                                          java.lang.String pattern,
                                          java.lang.String message,
                                          java.lang.Object... values)

        Validate that the specified argument character sequence matches the specified regular expression pattern; otherwise throwing an exception with the specified message.

        Validate.matchesPattern("hi", "[a-z]*", "%s does not match %s", "hi" "[a-z]*");

        The syntax of the pattern is the one used in the Pattern class.

        Parameters:
        input - the character sequence to validate, not null
        pattern - the regular expression pattern, 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:
        java.lang.IllegalArgumentException - if the character sequence does not match the pattern
        Since:
        3.0
        See Also:
        matchesPattern(CharSequence, String)
      • notNaN

        public static void notNaN​(double value)

        Validates that the specified argument is not NaN; otherwise throwing an exception.

        Validate.notNaN(myDouble);

        The message of the exception is "The validated value is not a number".

        Parameters:
        value - the value to validate
        Throws:
        java.lang.IllegalArgumentException - if the value is not a number
        Since:
        3.5
        See Also:
        notNaN(double, java.lang.String, java.lang.Object...)
      • notNaN

        public static void notNaN​(double value,
                                  java.lang.String message,
                                  java.lang.Object... values)

        Validates that the specified argument is not NaN; otherwise throwing an exception with the specified message.

        Validate.notNaN(myDouble, "The value must be a number");
        Parameters:
        value - the value to validate
        message - the String.format(String, Object...) exception message if invalid, not null
        values - the optional values for the formatted exception message
        Throws:
        java.lang.IllegalArgumentException - if the value is not a number
        Since:
        3.5
        See Also:
        notNaN(double)
      • finite

        public static void finite​(double value)

        Validates that the specified argument is not infinite or NaN; otherwise throwing an exception.

        Validate.finite(myDouble);

        The message of the exception is "The value is invalid: %f".

        Parameters:
        value - the value to validate
        Throws:
        java.lang.IllegalArgumentException - if the value is infinite or NaN
        Since:
        3.5
        See Also:
        finite(double, java.lang.String, java.lang.Object...)
      • finite

        public static void finite​(double value,
                                  java.lang.String message,
                                  java.lang.Object... values)

        Validates that the specified argument is not infinite or NaN; otherwise throwing an exception with the specified message.

        Validate.finite(myDouble, "The argument must contain a numeric value");
        Parameters:
        value - the value to validate
        message - the String.format(String, Object...) exception message if invalid, not null
        values - the optional values for the formatted exception message
        Throws:
        java.lang.IllegalArgumentException - if the value is infinite or NaN
        Since:
        3.5
        See Also:
        finite(double)
      • inclusiveBetween

        public static <T> void inclusiveBetween​(T start,
                                                T end,
                                                java.lang.Comparable<T> value)

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

        Validate.inclusiveBetween(0, 2, 1);
        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
        Throws:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.0
        See Also:
        inclusiveBetween(Object, Object, Comparable, String, Object...)
      • inclusiveBetween

        public static <T> void inclusiveBetween​(T start,
                                                T end,
                                                java.lang.Comparable<T> value,
                                                java.lang.String message,
                                                java.lang.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:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.0
        See Also:
        inclusiveBetween(Object, Object, Comparable)
      • inclusiveBetween

        public static void inclusiveBetween​(long start,
                                            long end,
                                            long value)
        Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception.
        Validate.inclusiveBetween(0, 2, 1);
        Parameters:
        start - the inclusive start value
        end - the inclusive end value
        value - the value to validate
        Throws:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries (inclusive)
        Since:
        3.3
      • inclusiveBetween

        public static void inclusiveBetween​(long start,
                                            long end,
                                            long value,
                                            java.lang.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:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.3
      • inclusiveBetween

        public static void inclusiveBetween​(double start,
                                            double end,
                                            double value)
        Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception.
        Validate.inclusiveBetween(0.1, 2.1, 1.1);
        Parameters:
        start - the inclusive start value
        end - the inclusive end value
        value - the value to validate
        Throws:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries (inclusive)
        Since:
        3.3
      • inclusiveBetween

        public static void inclusiveBetween​(double start,
                                            double end,
                                            double value,
                                            java.lang.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:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.3
      • exclusiveBetween

        public static <T> void exclusiveBetween​(T start,
                                                T end,
                                                java.lang.Comparable<T> value)

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

        Validate.exclusiveBetween(0, 2, 1);
        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
        Throws:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.0
        See Also:
        exclusiveBetween(Object, Object, Comparable, String, Object...)
      • exclusiveBetween

        public static <T> void exclusiveBetween​(T start,
                                                T end,
                                                java.lang.Comparable<T> value,
                                                java.lang.String message,
                                                java.lang.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:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.0
        See Also:
        exclusiveBetween(Object, Object, Comparable)
      • exclusiveBetween

        public static void exclusiveBetween​(long start,
                                            long end,
                                            long value)
        Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception.
        Validate.exclusiveBetween(0, 2, 1);
        Parameters:
        start - the exclusive start value
        end - the exclusive end value
        value - the value to validate
        Throws:
        java.lang.IllegalArgumentException - if the value falls out of the boundaries
        Since:
        3.3
      • exclusiveBetween

        public static void exclusiveBetween​(long start,
                                            long end,
                                            long value,
                                            java.lang.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:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.3
      • exclusiveBetween

        public static void exclusiveBetween​(double start,
                                            double end,
                                            double value)
        Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception.
        Validate.exclusiveBetween(0.1, 2.1, 1.1);
        Parameters:
        start - the exclusive start value
        end - the exclusive end value
        value - the value to validate
        Throws:
        java.lang.IllegalArgumentException - if the value falls out of the boundaries
        Since:
        3.3
      • exclusiveBetween

        public static void exclusiveBetween​(double start,
                                            double end,
                                            double value,
                                            java.lang.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:
        java.lang.IllegalArgumentException - if the value falls outside the boundaries
        Since:
        3.3
      • isInstanceOf

        public static void isInstanceOf​(java.lang.Class<?> type,
                                        java.lang.Object obj)
        Validates that the argument is an instance of the specified class, if not throws an exception.

        This method is useful when validating according to an arbitrary class

        Validate.isInstanceOf(OkClass.class, object);

        The message of the exception is "Expected type: {type}, actual: {obj_type}"

        Parameters:
        type - the class the object must be validated against, not null
        obj - the object to check, null throws an exception
        Throws:
        java.lang.IllegalArgumentException - if argument is not of specified class
        Since:
        3.0
        See Also:
        isInstanceOf(Class, Object, String, Object...)
      • isInstanceOf

        public static void isInstanceOf​(java.lang.Class<?> type,
                                        java.lang.Object obj,
                                        java.lang.String message,
                                        java.lang.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:
        java.lang.IllegalArgumentException - if argument is not of specified class
        Since:
        3.0
        See Also:
        isInstanceOf(Class, Object)
      • isAssignableFrom

        public static void isAssignableFrom​(java.lang.Class<?> superType,
                                            java.lang.Class<?> type)
        Validates that the argument can be converted to the specified class, if not, throws an exception.

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

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

        The message format of the exception is "Cannot assign {type} to {superType}"

        Parameters:
        superType - the class the class must be validated against, not null
        type - the class to check, not null
        Throws:
        java.lang.IllegalArgumentException - if type argument is not assignable to the specified superType
        Since:
        3.0
        See Also:
        isAssignableFrom(Class, Class, String, Object...)
      • isAssignableFrom

        public static void isAssignableFrom​(java.lang.Class<?> superType,
                                            java.lang.Class<?> type,
                                            java.lang.String message,
                                            java.lang.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:
        java.lang.IllegalArgumentException - if argument can not be converted to the specified class
        See Also:
        isAssignableFrom(Class, Class)