Package 

Class Validate


  • 
    public class Validate
    
                        

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

    All exceptions messages are format stringsas 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#

    • Constructor Summary

      Constructors 
      Constructor Description
      Validate() Constructor.
    • Method Summary

      Modifier and Type Method Description
      static void isTrue(boolean expression, String message, long value) Validate that the argument condition is {@code true}; otherwisethrowing an exception with the specified message.
      static void isTrue(boolean expression, String message, double value) Validate that the argument condition is {@code true}; otherwisethrowing an exception with the specified message.
      static void isTrue(boolean expression, String message, Array<Object> values) Validate that the argument condition is {@code true}; otherwisethrowing an exception with the specified message.
      static void isTrue(boolean expression) Validate that the argument condition is {@code true}; otherwisethrowing an exception.
      static <T> T notNull(T object) Validate that the specified argument is not {@code null};otherwise throwing an exception.
      static <T> T notNull(T object, String message, Array<Object> values) Validate that the specified argument is not {@code null};otherwise throwing an exception with the specified message.
      static <T> Array<T> notEmpty(Array<T> array, String message, Array<Object> values) Validate that the specified argument array is neither {@code null} nor a length of zero (no elements); otherwise throwing an exceptionwith the specified message.
      static <T> Array<T> notEmpty(Array<T> array) Validate that the specified argument array is neither {@code null} nor a length of zero (no elements); otherwise throwing an exception.
      static <T extends Collection<out Object>> T notEmpty(T collection, String message, Array<Object> values) Validate that the specified argument collection is neither {@code null} nor a size of zero (no elements); otherwise throwing an exceptionwith the specified message.
      static <T extends Collection<out Object>> T notEmpty(T collection) Validate that the specified argument collection is neither {@code null} nor a size of zero (no elements); otherwise throwing an exception.
      static <T extends Map<out Object, out Object>> T notEmpty(T map, String message, Array<Object> values) Validate that the specified argument map is neither {@code null} nor a size of zero (no elements); otherwise throwing an exceptionwith the specified message.
      static <T extends Map<out Object, out Object>> T notEmpty(T map) Validate that the specified argument map is neither {@code null} nor a size of zero (no elements); otherwise throwing an exception.
      static <T extends CharSequence> T notEmpty(T chars, String message, Array<Object> values) Validate that the specified argument character sequence isneither {@code null} nor a length of zero (no characters);otherwise throwing an exception with the specified message.
      static <T extends CharSequence> T notEmpty(T chars) Validate that the specified argument character sequence isneither {@code null} nor a length of zero (no characters);otherwise throwing an exception with the specified message.
      static <T extends CharSequence> T notBlank(T chars, String message, Array<Object> values) Validate that the specified argument character sequence isneither {@code null}, a length of zero (no characters), emptynor whitespace; otherwise throwing an exception with the specifiedmessage.
      static <T extends CharSequence> T notBlank(T chars) Validate that the specified argument character sequence isneither {@code null}, a length of zero (no characters), emptynor whitespace; otherwise throwing an exception.
      static <T> Array<T> noNullElements(Array<T> array, String message, Array<Object> values) Validate that the specified argument array is neither {@code null} nor contains any elements that are {@code null};otherwise throwing an exception with the specified message.
      static <T> Array<T> noNullElements(Array<T> array) Validate that the specified argument array is neither {@code null} nor contains any elements that are {@code null};otherwise throwing an exception.
      static <T extends Iterable<out Object>> T noNullElements(T iterable, String message, Array<Object> values) Validate that the specified argument iterable is neither {@code null} nor contains any elements that are {@code null};otherwise throwing an exception with the specified message.
      static <T extends Iterable<out Object>> T noNullElements(T iterable) Validate that the specified argument iterable is neither {@code null} nor contains any elements that are {@code null};otherwise throwing an exception.
      static <T> Array<T> validIndex(Array<T> array, int index, String message, Array<Object> values) Validates that the index is within the bounds of the argumentarray; otherwise throwing an exception with the specified message.
      static <T> Array<T> validIndex(Array<T> array, int index) Validates that the index is within the bounds of the argumentarray; otherwise throwing an exception.
      static <T extends Collection<out Object>> T validIndex(T collection, int index, String message, Array<Object> values) Validates that the index is within the bounds of the argumentcollection; otherwise throwing an exception with the specified message.
      static <T extends Collection<out Object>> T validIndex(T collection, int index) Validates that the index is within the bounds of the argumentcollection; otherwise throwing an exception.
      static <T extends CharSequence> T validIndex(T chars, int index, String message, Array<Object> values) Validates that the index is within the bounds of the argumentcharacter sequence; otherwise throwing an exception with thespecified message.
      static <T extends CharSequence> T validIndex(T chars, int index) Validates that the index is within the bounds of the argumentcharacter sequence; otherwise throwing an exception.
      static void validState(boolean expression) Validate that the stateful condition is {@code true}; otherwisethrowing an exception.
      static void validState(boolean expression, String message, Array<Object> values) Validate that the stateful condition is {@code true}; otherwisethrowing an exception with the specified message.
      static void matchesPattern(CharSequence input, String pattern) Validate that the specified argument character sequence matches the specified regularexpression pattern; otherwise throwing an exception.
      static void matchesPattern(CharSequence input, String pattern, String message, Array<Object> values) Validate that the specified argument character sequence matches the specified regularexpression pattern; otherwise throwing an exception with the specified message.
      static <T> void inclusiveBetween(T start, T end, Comparable<T> value) Validate that the specified argument object fall between the twoinclusive values specified; otherwise, throws an exception.
      static <T> void inclusiveBetween(T start, T end, Comparable<T> value, String message, Array<Object> values) Validate that the specified argument object fall between the twoinclusive values specified; otherwise, throws an exception with thespecified message.
      static <T> void exclusiveBetween(T start, T end, Comparable<T> value) Validate that the specified argument object fall between the twoexclusive values specified; otherwise, throws an exception.
      static <T> void exclusiveBetween(T start, T end, Comparable<T> value, String message, Array<Object> values) Validate that the specified argument object fall between the twoexclusive values specified; otherwise, throws an exception with thespecified message.
      static void isInstanceOf(Class<out Object> type, Object obj) Validates that the argument is an instance of the specified class, if not throws an exception.
      static void isInstanceOf(Class<out Object> type, Object obj, String message, Array<Object> values) Validate that the argument is an instance of the specified class; otherwisethrowing an exception with the specified message.
      static void isAssignableFrom(Class<out Object> superType, Class<out Object> type) Validates that the argument can be converted to the specified class, if not, throws an exception.
      static void isAssignableFrom(Class<out Object> superType, Class<out Object> type, String message, Array<Object> values) Validates that the argument can be converted to the specified class, if not throws an exception.
      • Methods inherited from class java.lang.Object

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

      • Validate

        Validate()
        Constructor.
    • Method Detail

      • isTrue

         static void isTrue(boolean expression, String message, long value)

        Validate that the argument condition is {@code true}; otherwisethrowing an exception with the specified message. This method is useful whenvalidating according to an arbitrary boolean expression, such as validating aprimitive 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 andappended to the exception message only in the case of an error.

        Parameters:
        expression - the boolean expression to check
        message - the format exception message if invalid, not null
        value - the value to append to the message when invalid
      • isTrue

         static void isTrue(boolean expression, String message, double value)

        Validate that the argument condition is {@code true}; otherwisethrowing an exception with the specified message. This method is useful whenvalidating according to an arbitrary boolean expression, such as validating aprimitive 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 andappended to the exception message only in the case of an error.

        Parameters:
        expression - the boolean expression to check
        message - the format exception message if invalid, not null
        value - the value to append to the message when invalid
      • isTrue

         static void isTrue(boolean expression, String message, Array<Object> values)

        Validate that the argument condition is {@code true}; otherwisethrowing an exception with the specified message. This method is useful whenvalidating according to an arbitrary boolean expression, such as validating aprimitive 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 format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • isTrue

         static void isTrue(boolean expression)

        Validate that the argument condition is {@code true}; otherwisethrowing an exception. This method is useful when validating accordingto an arbitrary boolean expression, such as validating aprimitive 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 isfalse".

        Parameters:
        expression - the boolean expression to check
      • notNull

         static <T> T notNull(T object)

        Validate that the specified argument is not {@code null};otherwise throwing an exception.

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

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

        Parameters:
        object - the object to check
      • notNull

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

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

        Validate.notNull(myObject, "The object must not be null");
        Parameters:
        object - the object to check
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message
      • notEmpty

         static <T> Array<T> notEmpty(Array<T> array, String message, Array<Object> values)

        Validate that the specified argument array is neither {@code null} nor a length of zero (no elements); otherwise throwing an exceptionwith the specified message.

        Validate.notEmpty(myArray, "The array must not be empty");
        Parameters:
        array - the array to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • notEmpty

         static <T> Array<T> notEmpty(Array<T> array)

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

        Validate.notEmpty(myArray);

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

        Parameters:
        array - the array to check, validated not null by this method
      • notEmpty

         static <T extends Collection<out Object>> T notEmpty(T collection, String message, Array<Object> values)

        Validate that the specified argument collection is neither {@code null} nor a size of zero (no elements); otherwise throwing an exceptionwith the specified message.

        Validate.notEmpty(myCollection, "The collection must not be empty");
        Parameters:
        collection - the collection to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • notEmpty

         static <T extends Collection<out Object>> T notEmpty(T collection)

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

        Validate.notEmpty(myCollection);

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

        Parameters:
        collection - the collection to check, validated not null by this method
      • notEmpty

         static <T extends Map<out Object, out Object>> T notEmpty(T map, String message, Array<Object> values)

        Validate that the specified argument map is neither {@code null} nor a size of zero (no elements); otherwise throwing an exceptionwith the specified message.

        Validate.notEmpty(myMap, "The map must not be empty");
        Parameters:
        map - the map to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • notEmpty

         static <T extends Map<out Object, out Object>> T notEmpty(T map)

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

        Validate.notEmpty(myMap);

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

        Parameters:
        map - the map to check, validated not null by this method
      • notEmpty

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

        Validate that the specified argument character sequence isneither {@code 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");
        Parameters:
        chars - the character sequence to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • notEmpty

         static <T extends CharSequence> T notEmpty(T chars)

        Validate that the specified argument character sequence isneither {@code 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 validatedcharacter sequence is empty".

        Parameters:
        chars - the character sequence to check, validated not null by this method
      • notBlank

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

        Validate that the specified argument character sequence isneither {@code null}, a length of zero (no characters), emptynor whitespace; otherwise throwing an exception with the specifiedmessage.

        Validate.notBlank(myString, "The string must not be blank");
        Parameters:
        chars - the character sequence to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • notBlank

         static <T extends CharSequence> T notBlank(T chars)

        Validate that the specified argument character sequence isneither {@code null}, a length of zero (no characters), emptynor whitespace; otherwise throwing an exception.

        Validate.notBlank(myString);

        The message in the exception is "The validated charactersequence is blank".

        Parameters:
        chars - the character sequence to check, validated not null by this method
      • noNullElements

         static <T> Array<T> noNullElements(Array<T> array, String message, Array<Object> values)

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

        Validate.noNullElements(myArray, "The array contain null at position %d");

        If the array is {@code null}, then the message in the exceptionis "The validated object is null".

        If the array has a {@code null} element, then the iterationindex of the invalid element is appended to the {@code values} argument.

        Parameters:
        array - the array to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • noNullElements

         static <T> Array<T> noNullElements(Array<T> array)

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

        Validate.noNullElements(myArray);

        If the array is {@code null}, then the message in the exceptionis "The validated object is null".

        If the array has a {@code null} element, then the message in theexception is "The validated array contains null element at index:" followed by the index.

        Parameters:
        array - the array to check, validated not null by this method
      • noNullElements

         static <T extends Iterable<out Object>> T noNullElements(T iterable, String message, Array<Object> values)

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

        Validate.noNullElements(myCollection, "The collection contains null at position %d");

        If the iterable is {@code null}, then the message in the exceptionis "The validated object is null".

        If the iterable has a {@code null} element, then the iterationindex of the invalid element is appended to the {@code values} argument.

        Parameters:
        iterable - the iterable to check, validated not null by this method
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • noNullElements

         static <T extends Iterable<out Object>> T noNullElements(T iterable)

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

        Validate.noNullElements(myCollection);

        If the iterable is {@code null}, then the message in the exceptionis "The validated object is null".

        If the array has a {@code null} element, then the message in theexception is "The validated iterable contains null element at index:" followed by the index.

        Parameters:
        iterable - the iterable to check, validated not null by this method
      • validIndex

         static <T> Array<T> validIndex(Array<T> array, int index, String message, Array<Object> values)

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

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

        If the array is {@code null}, then the message of the exceptionis "The validated object is null".

        Parameters:
        array - the array to check, validated not null by this method
        index - the index to check
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • validIndex

         static <T> Array<T> validIndex(Array<T> array, int index)

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

        Validate.validIndex(myArray, 2);

        If the array is {@code null}, then the message of the exceptionis "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 theindex.

        Parameters:
        array - the array to check, validated not null by this method
        index - the index to check
      • validIndex

         static <T extends Collection<out Object>> T validIndex(T collection, int index, String message, Array<Object> values)

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

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

        If the collection is {@code null}, then the message of theexception is "The validated object is null".

        Parameters:
        collection - the collection to check, validated not null by this method
        index - the index to check
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • validIndex

         static <T extends Collection<out Object>> T validIndex(T collection, int index)

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

        Validate.validIndex(myCollection, 2);

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

        Parameters:
        collection - the collection to check, validated not null by this method
        index - the index to check
      • validIndex

         static <T extends CharSequence> T validIndex(T chars, int index, String message, Array<Object> values)

        Validates that the index is within the bounds of the argumentcharacter sequence; otherwise throwing an exception with thespecified message.

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

        If the character sequence is {@code null}, then the messageof the exception is "The validated object is null".

        Parameters:
        chars - the character sequence to check, validated not null by this method
        index - the index to check
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • validIndex

         static <T extends CharSequence> T validIndex(T chars, int index)

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

        Validate.validIndex(myStr, 2);

        If the character sequence is {@code null}, then the messageof the exception is "The validated object isnull".

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

        Parameters:
        chars - the character sequence to check, validated not null by this method
        index - the index to check
      • validState

         static void validState(boolean expression)

        Validate that the stateful condition is {@code true}; otherwisethrowing an exception. This method is useful when validating accordingto an arbitrary boolean expression, such as validating aprimitive 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 isfalse".

        Parameters:
        expression - the boolean expression to check
      • validState

         static void validState(boolean expression, String message, Array<Object> values)

        Validate that the stateful condition is {@code true}; otherwisethrowing an exception with the specified message. This method is useful whenvalidating according to an arbitrary boolean expression, such as validating aprimitive 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 format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • matchesPattern

         static void matchesPattern(CharSequence input, String pattern)

        Validate that the specified argument character sequence matches the specified regularexpression 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
      • matchesPattern

         static void matchesPattern(CharSequence input, String pattern, String message, Array<Object> values)

        Validate that the specified argument character sequence matches the specified regularexpression 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 format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • inclusiveBetween

         static <T> void inclusiveBetween(T start, T end, Comparable<T> value)

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

        Validate.inclusiveBetween(0, 2, 1);
        Parameters:
        start - the inclusive start value, not null
        end - the inclusive end value, not null
        value - the object to validate, not null
      • inclusiveBetween

         static <T> void inclusiveBetween(T start, T end, Comparable<T> value, String message, Array<Object> values)

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

        Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");
        Parameters:
        start - the inclusive start value, not null
        end - the inclusive end value, not null
        value - the object to validate, not null
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • exclusiveBetween

         static <T> void exclusiveBetween(T start, T end, Comparable<T> value)

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

        Validate.inclusiveBetween(0, 2, 1);
        Parameters:
        start - the exclusive start value, not null
        end - the exclusive end value, not null
        value - the object to validate, not null
      • exclusiveBetween

         static <T> void exclusiveBetween(T start, T end, Comparable<T> value, String message, Array<Object> values)

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

        Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");
        Parameters:
        start - the exclusive start value, not null
        end - the exclusive end value, not null
        value - the object to validate, not null
        message - the format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • isInstanceOf

         static void isInstanceOf(Class<out Object> type, 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
      • isInstanceOf

         static void isInstanceOf(Class<out Object> type, Object obj, String message, Array<Object> values)

        Validate that the argument is an instance of the specified class; otherwisethrowing an exception with the specified message. This method is useful whenvalidating according to an arbitrary class

        Validate.isInstanceOf(OkClass.classs, 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 format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended
      • isAssignableFrom

         static void isAssignableFrom(Class<out Object> superType, Class<out Object> 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
      • isAssignableFrom

         static void isAssignableFrom(Class<out Object> superType, Class<out Object> type, String message, Array<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 format exception message if invalid, not null
        values - the optional values for the formatted exception message, null array not recommended