Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class Validate

extends Object
Class Inheritance
Imports
java.util.Collection, .Map, .Objects, java.util.concurrent.atomic.AtomicInteger, java.util.function.Supplier, java.util.regex.Pattern

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

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

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

#ThreadSafe#

Since
2.0
See Also
String#format(String, Object...)

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String
private static final String

Constructor Summary

AccessConstructor and Description
public
Validate()

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public static void
exclusiveBetween(final double
the exclusive start value.
start
,
final double
the exclusive end value.
end
,
final double
the value to validate.
value
)

Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception.

public static void
exclusiveBetween(final double
the exclusive start value.
start
,
final double
the exclusive end value.
end
,
final double
the value to validate.
value
,
final String
the exception message if invalid, not null.
message
)

Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.

public static void
exclusiveBetween(final long
the exclusive start value.
start
,
final long
the exclusive end value.
end
,
final long
the value to validate.
value
)

Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception.

public static void
exclusiveBetween(final long
the exclusive start value.
start
,
final long
the exclusive end value.
end
,
final long
the value to validate.
value
,
final String
the exception message if invalid, not null.
message
)

Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.

public static <
the type of the argument object.
T
>
void
exclusiveBetween(final T
the exclusive start value, not null.
start
,
final T
the exclusive end value, not null.
end
,
final Comparable<T>
the object to validate, not null.
value
)

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

public static <
the type of the argument object.
T
>
void
exclusiveBetween(final T
the exclusive start value, not null.
start
,
final T
the exclusive end value, not null.
end
,
final Comparable<T>
the object to validate, not null.
value
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static void
finite(final double
the value to validate.
value
)

Validates that the specified argument is not infinite or Not-a-Number (NaN); otherwise throwing an exception.

public static void
finite(final double
the value to validate.
value
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message.
values
)

Validates that the specified argument is not infinite or Not-a-Number (NaN); otherwise throwing an exception with the specified message.

private static String

Returns:

formatted message using String.format(message, values) if the values are not empty, otherwise return the unformatted message.
getMessage
(final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted message.
values
)

Gets the message using String.format(message, values) if the values are not empty, otherwise return the message unformatted.

public static void
inclusiveBetween(final double
the inclusive start value.
start
,
final double
the inclusive end value.
end
,
final double
the value to validate.
value
)

Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception.

public static void
inclusiveBetween(final double
the inclusive start value.
start
,
final double
the inclusive end value.
end
,
final double
the value to validate.
value
,
final String
the exception message if invalid, not null.
message
)

Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.

public static void
inclusiveBetween(final long
the inclusive start value.
start
,
final long
the inclusive end value.
end
,
final long
the value to validate.
value
)

Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception.

public static void
inclusiveBetween(final long
the inclusive start value.
start
,
final long
the inclusive end value.
end
,
final long
the value to validate.
value
,
final String
the exception message if invalid, not null.
message
)

Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.

public static <
the type of the argument object.
T
>
void
inclusiveBetween(final T
the inclusive start value, not null.
start
,
final T
the inclusive end value, not null.
end
,
final Comparable<T>
the object to validate, not null.
value
)

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

public static <
the type of the argument object.
T
>
void
inclusiveBetween(final T
the inclusive start value, not null.
start
,
final T
the inclusive end value, not null.
end
,
final Comparable<T>
the object to validate, not null.
value
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static void
isAssignableFrom(final Class<?>
the class must be validated against, not null.
superType
,
final Class<?>
the class to check, not null.
type
)

Validates that the argument can be converted to the specified class, if not, throws an exception.

public static void
isAssignableFrom(final Class<?>
the class must be validated against, not null.
superType
,
final Class<?>
the class to check, not null.
type
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

Validates that the argument can be converted to the specified class, if not throws an exception.

public static void
isInstanceOf(final Class<?>
the class the object must be validated against, not null.
type
,
final Object
the object to check, null throws an exception.
obj
)

Validates that the argument is an instance of the specified class, if not throws an exception.

public static void
isInstanceOf(final Class<?>
the class the object must be validated against, not null.
type
,
final Object
the object to check, null throws an exception.
obj
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

Validate that the argument is an instance of the specified class; otherwise throwing an exception with the specified message.

public static void
isTrue(final boolean
the boolean expression to check.
expression
)

Validate that the argument condition is true; otherwise throwing an exception.

public static void
isTrue(final boolean
the boolean expression to check.
expression
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final double
the value to append to the message when invalid.
value
)

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

public static void
isTrue(final boolean
the boolean expression to check.
expression
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final long
the value to append to the message when invalid.
value
)

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

public static void
isTrue(final boolean
the boolean expression to check.
expression
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static void
isTrue(final boolean
the boolean expression to check.
expression
,
final Supplier<String>
the exception message supplier.
messageSupplier
)

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

public static void
matchesPattern(final CharSequence
the character sequence to validate, not null.
input
,
final String
the regular expression pattern, not null.
pattern
)

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

public static void
matchesPattern(final CharSequence
the character sequence to validate, not null.
input
,
final String
the regular expression pattern, not null.
pattern
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static <
the iterable type.
T extends Iterable<?>
>
T

Returns:

the validated iterable (never null method for chaining).
noNullElements
(final T
the iterable to check, validated not null by this method.
iterable
)

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

public static <
the iterable type.
T extends Iterable<?>
>
T

Returns:

the validated iterable (never null method for chaining).
noNullElements
(final T
the iterable to check, validated not null by this method.
iterable
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
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.

public static <
the array type.
T
>
T[]

Returns:

the validated array (never null method for chaining).
noNullElements
(final T[]
the array to check, validated not null by this method.
array
)

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

public static <
the array type.
T
>
T[]

Returns:

the validated array (never null method for chaining).
noNullElements
(final T[]
the array to check, validated not null by this method.
array
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
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.

public static <
the character sequence type.
T extends CharSequence
>
T

Returns:

the validated character sequence (never null method for chaining).
notBlank
(final T
the character sequence to check, validated not null by this method.
chars
)

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

public static <
the character sequence type.
T extends CharSequence
>
T

Returns:

the validated character sequence (never null method for chaining).
notBlank
(final T
the character sequence to check, validated not null by this method.
chars
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

Validates that the specified argument character sequence is not blank (whitespaces, empty ("") or null); otherwise throwing an exception with the specified message.

public static <
the collection type.
T extends Collection<?>
>
T

Returns:

the validated collection (never null method for chaining).
notEmpty
(final T
the collection to check, validated not null by this method.
collection
)

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

public static <
the map type.
T extends Map<?, ?>
>
T

Returns:

the validated map (never null method for chaining).
notEmpty
(final T
the map to check, validated not null by this method.
map
)

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

public static <
the character sequence type.
T extends CharSequence
>
T

Returns:

the validated character sequence (never null method for chaining).
notEmpty
(final T
the character sequence to check, validated not null by this method.
chars
)

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

public static <
the collection type.
T extends Collection<?>
>
T

Returns:

the validated collection (never null method for chaining).
notEmpty
(final T
the collection to check, validated not null by this method.
collection
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static <
the map type.
T extends Map<?, ?>
>
T

Returns:

the validated map (never null method for chaining).
notEmpty
(final T
the map to check, validated not null by this method.
map
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
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.

public static <
the character sequence type.
T extends CharSequence
>
T

Returns:

the validated character sequence (never null method for chaining).
notEmpty
(final T
the character sequence to check, validated not null by this method.
chars
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
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.

public static <
the array type.
T
>
T[]

Returns:

the validated array (never null method for chaining).
notEmpty
(final T[]
the array to check, validated not null by this method.
array
)

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

public static <
the array type.
T
>
T[]

Returns:

the validated array (never null method for chaining).
notEmpty
(final T[]
the array to check, validated not null by this method.
array
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static void
notNaN(final double
the value to validate.
value
)

Validates that the specified argument is not Not-a-Number (NaN); otherwise throwing an exception.

public static void
notNaN(final double
the value to validate.
value
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message.
values
)

Validates that the specified argument is not Not-a-Number (NaN); otherwise throwing an exception with the specified message.

public static <
the object type.
T
>
T

Returns:

the validated object (never null for method chaining).
notNull
(final T
the object to check.
object
)

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

public static <
the object type.
T
>
T

Returns:

the validated object (never null for method chaining).
notNull
(final T
the object to check.
object
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message.
values
)

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

private static Supplier<String>
toSupplier(final String message, final Object... values)

public static <
the collection type.
T extends Collection<?>
>
T

Returns:

the validated collection (never null for method chaining).
validIndex
(final T
the collection to check, validated not null by this method.
collection
,
final int
the index to check.
index
)

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

public static <
the character sequence type.
T extends CharSequence
>
T

Returns:

the validated character sequence (never null for method chaining).
validIndex
(final T
the character sequence to check, validated not null by this method.
chars
,
final int
the index to check.
index
)

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

public static <
the collection type.
T extends Collection<?>
>
T

Returns:

the validated collection (never null for chaining).
validIndex
(final T
the collection to check, validated not null by this method.
collection
,
final int
the index to check.
index
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static <
the character sequence type.
T extends CharSequence
>
T

Returns:

the validated character sequence (never null for method chaining).
validIndex
(final T
the character sequence to check, validated not null by this method.
chars
,
final int
the index to check.
index
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static <
the array type.
T
>
T[]

Returns:

the validated array (never null for method chaining).
validIndex
(final T[]
the array to check, validated not null by this method.
array
,
final int
the index to check.
index
)

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

public static <
the array type.
T
>
T[]

Returns:

the validated array (never null for method chaining).
validIndex
(final T[]
the array to check, validated not null by this method.
array
,
final int
the index to check.
index
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

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

public static void
validState(final boolean
the boolean expression to check.
expression
)

Validate that the stateful condition is true; otherwise throwing an exception.

public static void
validState(final boolean
the boolean expression to check.
expression
,
final String
the String#format(String, Object...) exception message if invalid, not null.
message
,
final Object...
the optional values for the formatted exception message, null array not recommended.
values
)

Validate that the stateful condition is true; otherwise throwing an exception with the specified message.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait