Validate¶
- public class org.apache.commons.lang3.Validate[source]¶
This class assists in validating arguments. The validation methods are based along the following principles:
An invalid
nullargument causes aNullPointerException.A non-
nullargument causes anIllegalArgumentException.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#
- See also:
- Since:
2.0
Constructors¶
Deprecated. Constructs a new instance. |
- @Deprecated public Validate()[source]¶
Deprecated: Will be made private in 4.0. Use static methods.
Constructs a new instance. This class should not normally be instantiated.
Methods¶
Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception. |
|
Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message. |
|
Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception. |
|
|
Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception with the specified message. |
Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception. |
|
Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message. |
|
Validates that the specified argument is not infinite or Not-a-Number (NaN); otherwise throwing an exception. |
|
Validates that the specified argument is not infinite or Not-a-Number (NaN); otherwise throwing an exception with the specified message. |
|
Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception. |
|
Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message. |
|
Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception. |
|
|
Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception with the specified message. |
Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception. |
|
Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message. |
|
Tests whether the argument can be converted to the specified class; otherwise, throws an exception. |
|
Tests whether the argument can be converted to the specified class; otherwise, throws an exception. |
|
Tests whether the argument is an instance of the specified class; otherwise, throws an exception. |
|
Tests whether the argument is an instance of the specified class; otherwise, throws an exception with the specified message. |
|
Tests whether the argument condition is |
|
Tests whether the argument condition is |
|
Tests whether the argument condition is |
|
Tests whether the argument condition is |
|
Tests whether the argument condition is |
|
Validate that the specified argument character sequence matches the specified regular expression pattern; otherwise throwing an exception. |
|
Validate that the specified argument character sequence matches the specified regular expression pattern; otherwise throwing an exception with the specified message. |
|
Validate that the specified argument iterable is neither |
|
Validate that the specified argument iterable is neither |
|
Validate that the specified argument array is neither |
|
Validate that the specified argument array is neither |
|
Validates that the specified argument character sequence is neither |
|
Validates that the specified argument character sequence is not |
|
Validates that the specified argument character sequence is neither |
|
Validate that the specified argument character sequence is neither |
|
Validates that the specified argument array is neither |
|
Validates that the specified argument array is neither |
|
Validates that the specified argument collection is neither |
|
Validates that the specified argument collection is neither |
|
Validates that the specified argument map is neither |
|
Validate that the specified argument map is neither |
|
Validates that the specified argument is not Not-a-Number (NaN); otherwise throwing an exception. |
|
Validates that the specified argument is not Not-a-Number (NaN); otherwise throwing an exception with the specified message. |
|
Deprecated. Validate that the specified argument is not |
|
Validate that the specified argument is not |
|
Validates that the index is within the bounds of the argument character sequence; otherwise throwing an exception. |
|
Validates that the index is within the bounds of the argument character sequence; otherwise throwing an exception with the specified message. |
|
Validates that the index is within the bounds of the argument array; otherwise throwing an exception. |
|
Validates that the index is within the bounds of the argument array; otherwise throwing an exception with the specified message. |
|
Validates that the index is within the bounds of the argument collection; otherwise throwing an exception. |
|
Validates that the index is within the bounds of the argument collection; otherwise throwing an exception with the specified message. |
|
Validate that the stateful condition is |
|
Validate that the stateful condition is |
- @SuppressWarnings({"boxing"}) public static void exclusiveBetween(double start, double end, double value)[source]¶
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 – Thrown if the value falls out of the boundaries.
- Since:
3.3
- public static void exclusiveBetween(double start, double end, double value, String message)[source]¶
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 – Thrown if the value falls outside the boundaries.
- Since:
3.3
- public static <T> void exclusiveBetween(T start, T end, Comparable<T> value)[source]¶
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 – Thrown if the value falls outside the boundaries.
- See also:
#exclusiveBetween(Object, Object, Comparable, String, Object...)- Since:
3.0
- public static <T> void exclusiveBetween(T start, T end, Comparable<T> value, String message, Object... values)[source]¶
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 – Thrown if the value falls outside the boundaries.
- See also:
- Since:
3.0
- @SuppressWarnings({"boxing"}) public static void exclusiveBetween(long start, long end, long value)[source]¶
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 – Thrown if the value falls out of the boundaries.
- Since:
3.3
- public static void exclusiveBetween(long start, long end, long value, String message)[source]¶
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 – Thrown if the value falls outside the boundaries.
- Since:
3.3
- public static void finite(double value)[source]¶
Validates that the specified argument is not infinite or Not-a-Number (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 – Thrown if the value is infinite or Not-a-Number (NaN).
- See also:
- Since:
3.5
- public static void finite(double value, String message, Object... values)[source]¶
Validates that the specified argument is not infinite or Not-a-Number (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 – Thrown if the value is infinite or Not-a-Number (NaN).
- See also:
- Since:
3.5
- @SuppressWarnings({"boxing"}) public static void inclusiveBetween(double start, double end, double value)[source]¶
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 – Thrown if the value falls outside the boundaries (inclusive).
- Since:
3.3
- public static void inclusiveBetween(double start, double end, double value, String message)[source]¶
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 – Thrown if the value falls outside the boundaries.
- Since:
3.3
- public static <T> void inclusiveBetween(T start, T end, Comparable<T> value)[source]¶
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 – Thrown if the value falls outside the boundaries.
- See also:
#inclusiveBetween(Object, Object, Comparable, String, Object...)- Since:
3.0
- public static <T> void inclusiveBetween(T start, T end, Comparable<T> value, String message, Object... values)[source]¶
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 – Thrown if the value falls outside the boundaries.
- See also:
- Since:
3.0
- @SuppressWarnings({"boxing"}) public static void inclusiveBetween(long start, long end, long value)[source]¶
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 – Thrown if the value falls outside the boundaries (inclusive).
- Since:
3.3
- public static void inclusiveBetween(long start, long end, long value, String message)[source]¶
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 – Thrown if the value falls outside the boundaries.
- Since:
3.3
- public static void isAssignableFrom(Class<?> superType, Class<?> type)[source]¶
Tests whether the argument can be converted to the specified class; otherwise, 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 must be validated against, not null.
type – The class to check, not null.
- Throws:
java.lang.IllegalArgumentException – Thrown if type argument is not assignable to the specified superType.
- See also:
- Since:
3.0
- public static void isAssignableFrom(Class<?> superType, Class<?> type, String message, Object... values)[source]¶
Tests whether the argument can be converted to the specified class; otherwise, 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 cannot be converted to the” followed by the name of the class and “class”
- Parameters:
superType – 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 – Thrown if argument cannot be converted to the specified class.
- See also:
- public static void isInstanceOf(Class<?> type, Object obj)[source]¶
Tests whether the argument is an instance of the specified class; otherwise, 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 – Thrown if argument is not of specified class.
- See also:
- Since:
3.0
- public static void isInstanceOf(Class<?> type, Object obj, String message, Object... values)[source]¶
Tests whether the argument is an instance of the specified class; otherwise, throws 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 – Thrown if argument is not of specified class.
- See also:
- Since:
3.0
- public static void isTrue(boolean expression)[source]¶
Tests whether the argument condition is
true; otherwise, throws 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 – Thrown if expression is
false.- See also:
- public static void isTrue(boolean expression, String message, double value)[source]¶
Tests whether the argument condition is
true; otherwise, throws 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 – Thrown if expression is
false.- See also:
- public static void isTrue(boolean expression, String message, Object... values)[source]¶
Tests whether the argument condition is
true; otherwise, throws 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);
- 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 – Thrown if expression is
false.- See also:
- public static void isTrue(boolean expression, String message, long value)[source]¶
Tests whether the argument condition is
true; otherwise, throws 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 – Thrown if expression is
false.- See also:
- public static void isTrue(boolean expression, Supplier<String> messageSupplier)[source]¶
Tests whether the argument condition is
true; otherwise, throws 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);
- Parameters:
expression – The boolean expression to check.
messageSupplier – The exception message supplier.
- Throws:
java.lang.IllegalArgumentException – Thrown if expression is
false.- See also:
- Since:
3.18.0
- public static void matchesPattern(CharSequence input, String pattern)[source]¶
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
Patternclass.- Parameters:
input – The character sequence to validate, not null.
pattern – The regular expression pattern, not null.
- Throws:
java.lang.IllegalArgumentException – Thrown if the character sequence does not match the pattern.
- See also:
- Since:
3.0
- public static void matchesPattern(CharSequence input, String pattern, String message, Object... values)[source]¶
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
Patternclass.- 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 – Thrown if the character sequence does not match the pattern.
- See also:
- Since:
3.0
- public static <T extends Iterable<?>> T noNullElements(T iterable)[source]¶
Validate that the specified argument iterable is neither
nullnor contains any elements that arenull; 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
nullelement, 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IllegalArgumentException – Thrown if an element is
null.
- See also:
- public static <T extends Iterable<?>> T noNullElements(T iterable, String message, Object... values)[source]¶
Validate that the specified argument iterable is neither
nullnor contains any elements that arenull; 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
nullelement, then the iteration index of the invalid element is appended to thevaluesargument.- 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IllegalArgumentException – Thrown if an element is
null.
- See also:
- public static <T> T[] noNullElements(T[] array)[source]¶
Validate that the specified argument array is neither
nullnor contains any elements that arenull; 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
nullelement, 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IllegalArgumentException – Thrown if an element is
null.
- See also:
- public static <T> T[] noNullElements(T[] array, String message, Object... values)[source]¶
Validate that the specified argument array is neither
nullnor contains any elements that arenull; 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
nullelement, then the iteration index of the invalid element is appended to thevaluesargument.- 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IllegalArgumentException – Thrown if an element is
null.
- See also:
- public static <T extends CharSequence> T notBlank(T chars)[source]¶
Validates 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the character sequence is
null.java.lang.IllegalArgumentException – Thrown if the character sequence is blank.
- See also:
- Since:
3.0
- public static <T extends CharSequence> T notBlank(T chars, String message, Object... values)[source]¶
Validates that the specified argument character sequence is not
blank(whitespaces, empty ("") ornull); 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the character sequence is
null.java.lang.IllegalArgumentException – Thrown if the character sequence is blank.
- See also:
- Since:
3.0
- public static <T extends CharSequence> T notEmpty(T chars)[source]¶
Validates that the specified argument character sequence is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the character sequence is
null.java.lang.IllegalArgumentException – Thrown if the character sequence is empty.
- See also:
- public static <T extends CharSequence> T notEmpty(T chars, String message, Object... values)[source]¶
Validate that the specified argument character sequence is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the character sequence is
null.java.lang.IllegalArgumentException – Thrown if the character sequence is empty.
- See also:
- public static <T> T[] notEmpty(T[] array)[source]¶
Validates that the specified argument array is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IllegalArgumentException – Thrown if the array is empty.
- See also:
- public static <T> T[] notEmpty(T[] array, String message, Object... values)[source]¶
Validates that the specified argument array is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IllegalArgumentException – Thrown if the array is empty.
- See also:
- public static <T extends Collection<?>> T notEmpty(T collection)[source]¶
Validates that the specified argument collection is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the collection is
null.java.lang.IllegalArgumentException – Thrown if the collection is empty.
- See also:
- public static <T extends Collection<?>> T notEmpty(T collection, String message, Object... values)[source]¶
Validates that the specified argument collection is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the collection is
null.java.lang.IllegalArgumentException – Thrown if the collection is empty.
- See also:
- public static <T extends Map<?, ?>> T notEmpty(T map)[source]¶
Validates that the specified argument map is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the map is
null.java.lang.IllegalArgumentException – Thrown if the map is empty.
- See also:
- public static <T extends Map<?, ?>> T notEmpty(T map, String message, Object... values)[source]¶
Validate that the specified argument map is neither
nullnor 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
nullmethod for chaining).- Throws:
java.lang.NullPointerException – Thrown if the map is
null.java.lang.IllegalArgumentException – Thrown if the map is empty.
- See also:
- public static void notNaN(double value)[source]¶
Validates that the specified argument is not Not-a-Number (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 – Thrown if the value is not a number.
- See also:
- Since:
3.5
- public static void notNaN(double value, String message, Object... values)[source]¶
Validates that the specified argument is not Not-a-Number (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 – Thrown if the value is not a number.
- See also:
- Since:
3.5
- @Deprecated public static <T> T notNull(T object)[source]¶
Deprecated: Use
Objects#requireNonNull(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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the object is
null.- See also:
- public static <T> T notNull(T object, String message, Object... values)[source]¶
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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the object is
null.- See also:
- public static <T extends CharSequence> T validIndex(T chars, int index)[source]¶
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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the character sequence is
null.java.lang.IndexOutOfBoundsException – Thrown if the index is invalid.
- See also:
- Since:
3.0
- public static <T extends CharSequence> T validIndex(T chars, int index, String message, Object... values)[source]¶
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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the character sequence is
null.java.lang.IndexOutOfBoundsException – Thrown if the index is invalid.
- See also:
- Since:
3.0
- public static <T> T[] validIndex(T[] array, int index)[source]¶
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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IndexOutOfBoundsException – Thrown if the index is invalid.
- See also:
- Since:
3.0
- public static <T> T[] validIndex(T[] array, int index, String message, Object... values)[source]¶
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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the array is
null.java.lang.IndexOutOfBoundsException – Thrown if the index is invalid.
- See also:
- Since:
3.0
- public static <T extends Collection<?>> T validIndex(T collection, int index)[source]¶
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
nullfor method chaining).- Throws:
java.lang.NullPointerException – Thrown if the collection is
null.java.lang.IndexOutOfBoundsException – Thrown if the index is invalid.
- See also:
- Since:
3.0
- public static <T extends Collection<?>> T validIndex(T collection, int index, String message, Object... values)[source]¶
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
nullfor chaining).- Throws:
java.lang.NullPointerException – Thrown if the collection is
null.java.lang.IndexOutOfBoundsException – Thrown if the index is invalid.
- See also:
- Since:
3.0
- public static void validState(boolean expression)[source]¶
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 – Thrown if expression is
false.- See also:
- Since:
3.0
- public static void validState(boolean expression, String message, Object... values)[source]¶
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 – Thrown if expression is
false.- See also:
- Since:
3.0