public class Validation extends Object
Helps with parameter validation. Parameters can be checked using a construct like this:
public void copy(Object[] object, int leftValue, int rightValue) {
Validation.begin().isNotNull(object, "object").check()
.isPositive(leftValue, "leftValue").isLess(leftValue, object.length, "leftValue").check()
.isPositive(rightValue, "rightValue").isLess(rightValue, object.length, "rightValue").isGreater(rightValue, leftValue, "rightValue").check();
// do something with the values
}
This example will perform several checks. Only the check() method
will throw an IllegalArgumentException if one of the previous checks
failed, so you can gather several reasons for a validation failure before
throwing an exception which will in turn decrease the time spent in
debugging.
In the example, object is first checked for a non-
null value and an IllegalArgumentException is thrown if
object is null. Afterwards leftValue
is checked for being a positive value that is also smaller than the length of
the array object. The IllegalArgumentException that is
thrown if the checks failed will contain a message for each of the failed
checks. At last rightValue is checked for being positive,
smaller than the array’s length and larger than leftValue.
Remember to call the check() method after performing the checks,
otherwise the IllegalArgumentException will never be thrown!
| Modifier and Type | Method and Description |
|---|---|
static Validation |
begin()
Returns a new
Validation object. |
Validation |
check()
Checks if one of the previous checks failed and throws an
IllegalArgumentException if a previous check did fail. |
Validation |
is(String objectName,
boolean value)
Checks if the given value is
true. |
<T> Validation |
isAll(String objectName,
T object,
T... expecteds)
Checks whether the given
object matches all of the
expecteds values. |
<T> Validation |
isEither(String objectName,
T object,
T... expecteds)
Checks whether the given
object matches any of the expecteds values. |
<T> Validation |
isEqual(String objectName,
T value,
T expected)
Checks if the given value is equal to the expected value.
|
Validation |
isGreater(String objectName,
double value,
double lowerBound)
Checks if
value is greater than lowerBound. |
Validation |
isGreater(String objectName,
long value,
long lowerBound)
Checks if
value is greater than lowerBound. |
Validation |
isGreaterOrEqual(String objectName,
double value,
double lowerBound)
Checks if
value is greater than lowerBound. |
Validation |
isGreaterOrEqual(String objectName,
long value,
long lowerBound)
Checks if
value is greater than lowerBound. |
Validation |
isInstanceOf(String objectName,
Object object,
Class<?> clazz)
Checks whether the given object is assignable to an object of the given
class.
|
Validation |
isLess(String objectName,
double value,
double upperBound)
Checks if
value is less than upperBound. |
Validation |
isLess(String objectName,
long value,
long upperBound)
Checks if
value is less than upperBound. |
Validation |
isLessOrEqual(String objectName,
double value,
double upperBound)
Checks if
value is less than upperBound. |
Validation |
isLessOrEqual(String objectName,
long value,
long upperBound)
Checks if
value is less than upperBound. |
Validation |
isNegative(String objectName,
double value)
Checks if the given value is less than
0. |
Validation |
isNegative(String objectName,
long value)
Checks if the given value is less than
0. |
Validation |
isNot(String objectName,
boolean value)
Checks if the given value is
false. |
<T> Validation |
isNotEqual(String objectName,
T value,
T expected)
Checks if the given value is not equal to the expected value.
|
<T> Validation |
isNotNull(String objectName,
T object)
Checks if the given object is not
null. |
<T> Validation |
isNull(String objectName,
T object)
Checks if the given object is
null. |
Validation |
isPositive(String objectName,
double value)
Checks if the given value is greater to or equal to
0. |
Validation |
isPositive(String objectName,
long value)
Checks if the given value is greater to or equal to
0. |
<T> Validation |
isSame(String objectName,
T value,
T expected)
Checks if the given value is the same as the expected value.
|
public static Validation begin()
Validation object.public Validation check() throws IllegalArgumentException
IllegalArgumentException if a previous check did fail.Validation object to allow method chainingIllegalArgumentException - if a previous check failedpublic Validation is(String objectName, boolean value)
true.objectName - The object’s namevalue - The value to checkValidation object to allow method chainingpublic Validation isNot(String objectName, boolean value)
false.objectName - The object’s namevalue - The value to checkValidation object to allow method chainingpublic <T> Validation isNotNull(String objectName, T object)
null.T - The type of object being validatedobjectName - The object’s nameobject - The object to checkValidation object to allow method chainingpublic <T> Validation isNull(String objectName, T object)
null.T - The type of object being validatedobjectName - The object’s nameobject - The object to checkValidation object to allow method chainingpublic <T> Validation isEither(String objectName, T object, T... expecteds)
object matches any of the expecteds values.T - The type of object being validatedobjectName - The name of the objectobject - The object to checkexpecteds - The values of which at least one has to be
equalValidation object to allow method chainingpublic <T> Validation isAll(String objectName, T object, T... expecteds)
object matches all of the
expecteds values.T - The type of object being validatedobjectName - The name of the objectobject - The object to checkexpecteds - The values that have to be equalValidation object to allow method chainingpublic Validation isLess(String objectName, long value, long upperBound)
value is less than upperBound.objectName - The object’s namevalue - The value to checkupperBound - The upper bound to check value againstValidation object to allow method chainingpublic Validation isLess(String objectName, double value, double upperBound)
value is less than upperBound.objectName - The object’s namevalue - The value to checkupperBound - The upper bound to check value againstValidation object to allow method chainingpublic Validation isLessOrEqual(String objectName, long value, long upperBound)
value is less than upperBound.objectName - The object’s namevalue - The value to checkupperBound - The upper bound to check value againstValidation object to allow method chainingpublic Validation isLessOrEqual(String objectName, double value, double upperBound)
value is less than upperBound.objectName - The object’s namevalue - The value to checkupperBound - The upper bound to check value againstValidation object to allow method chainingpublic <T> Validation isEqual(String objectName, T value, T expected)
T - The type of object being validatedobjectName - The object’s namevalue - The value to checkexpected - The expected value to check value againstValidation object to allow method chainingpublic <T> Validation isSame(String objectName, T value, T expected)
T - The type of object being validatedobjectName - The object’s namevalue - The value to checkexpected - The expected value to check value againstValidation object to allow method chainingpublic <T> Validation isNotEqual(String objectName, T value, T expected)
T - The type of object being validatedobjectName - The object’s namevalue - The value to checkexpected - The expected value to check value againstValidation object to allow method chainingpublic Validation isGreater(String objectName, long value, long lowerBound)
value is greater than lowerBound.objectName - The object’s namevalue - The value to checklowerBound - The lower bound to check value againstValidation object to allow method chainingpublic Validation isGreater(String objectName, double value, double lowerBound)
value is greater than lowerBound.objectName - The object’s namevalue - The value to checklowerBound - The lower bound to check value againstValidation object to allow method chainingpublic Validation isGreaterOrEqual(String objectName, long value, long lowerBound)
value is greater than lowerBound.objectName - The object’s namevalue - The value to checklowerBound - The lower bound to check value againstValidation object to allow method chainingpublic Validation isGreaterOrEqual(String objectName, double value, double lowerBound)
value is greater than lowerBound.objectName - The object’s namevalue - The value to checklowerBound - The lower bound to check value againstValidation object to allow method chainingpublic Validation isPositive(String objectName, long value)
0.objectName - The object’s namevalue - The value to checkValidation object to allow method chainingpublic Validation isPositive(String objectName, double value)
0.objectName - The object’s namevalue - The value to checkValidation object to allow method chainingpublic Validation isNegative(String objectName, long value)
0.objectName - The object’s namevalue - The value to checkValidation object to allow method chainingpublic Validation isNegative(String objectName, double value)
0.objectName - The object’s namevalue - The value to checkValidation object to allow method chainingpublic Validation isInstanceOf(String objectName, Object object, Class<?> clazz)
objectName - The object’s nameobject - The object to checkclazz - The class the object should be representable asValidation object to allow method chainingCopyright © 2026. All rights reserved.