Package com.gengoai
Class Validation
- java.lang.Object
-
- com.gengoai.Validation
-
public final class Validation extends Object
Convenience methods for validating method arguments.
- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkArgument(boolean evaluation)
Throws aIllegalArgumentException
if the given boolean evaluates to false.static void
checkArgument(boolean evaluation, @NonNull Supplier<String> message)
Check argument.static void
checkArgument(boolean evaluation, String message)
Throws aIllegalArgumentException
if the given boolean evaluates to false.static void
checkArgumentIsInstanceOf(Object p, Type... types)
static int
checkElementIndex(int index, int size)
Checks that the given index is in the range[0, size)
static int
checkElementIndex(int index, int size, String message)
Check element index int.static int
checkElementIndex(int index, int size, Supplier<String> message)
Checks that the given index is in the range[0, size)
static int
checkPositionIndex(int index, int size)
Checks that the given index is in the range[0, size]
static int
checkPositionIndex(int index, int size, String message)
Checks that the given index is in the range[0, size]
static void
checkState(boolean evaluation)
Throws aIllegalStateException
if the given boolean evaluates to false.static void
checkState(boolean evaluation, String message)
Throws aIllegalStateException
if the given boolean evaluates to false.static void
checkState(boolean evaluation, Supplier<String> messageSupplier)
Check state.static <T> T
notNull(T object)
Throws aNullPointerException
if the given object is null.static <T> T
notNull(T object, String message)
Throws aNullPointerException
if the given object is null.static String
notNullOrBlank(String string)
Throws aIllegalArgumentException
if the given string is null or blank.static String
notNullOrBlank(String string, @NonNull Supplier<String> supplier)
Throws aIllegalArgumentException
if the given string is null or blank.static String
notNullOrBlank(String string, String message)
Throws aIllegalArgumentException
if the given string is null or blank.static void
validate(boolean evaluation, @NonNull Supplier<RuntimeException> exceptionSupplier)
Checks that the given evaluation or true and if it is not generates aRuntimeException
using the givenSupplier
and throws it.static <T> T
validate(boolean evaluation, @NonNull Supplier<RuntimeException> exceptionSupplier, T returnValue)
Checks that the evaluation is true.static <T> T
validate(T value, @NonNull Predicate<T> evaluator, @NonNull Supplier<RuntimeException> exceptionSupplier, boolean nullable)
Uses the givenPredicate
to evaluate the given value.static <T> T
validateArg(T value, @NonNull Predicate<T> predicate, String message, boolean nullable)
Checks that the evaluation is true.
-
-
-
Method Detail
-
checkArgument
public static void checkArgument(boolean evaluation)
Throws aIllegalArgumentException
if the given boolean evaluates to false.- Parameters:
evaluation
- the object to check
-
checkArgument
public static void checkArgument(boolean evaluation, String message)
Throws aIllegalArgumentException
if the given boolean evaluates to false.- Parameters:
evaluation
- the object to checkmessage
- the message to use in theIllegalArgumentException
-
checkArgument
public static void checkArgument(boolean evaluation, @NonNull @NonNull Supplier<String> message)
Check argument.- Parameters:
evaluation
- the evaluationmessage
- the message
-
checkElementIndex
public static int checkElementIndex(int index, int size)
Checks that the given index is in the range[0, size)
- Parameters:
index
- the index to validatesize
- the size of the array, list, etc.- Returns:
- the index
- Throws:
IndexOutOfBoundsException
- if index is negative or greater than equal to sizeIllegalArgumentException
- if size is negative
-
checkElementIndex
public static int checkElementIndex(int index, int size, String message)
Check element index int.- Parameters:
index
- the indexsize
- the sizemessage
- the message- Returns:
- the int
-
checkElementIndex
public static int checkElementIndex(int index, int size, Supplier<String> message)
Checks that the given index is in the range[0, size)
- Parameters:
index
- the index to validatesize
- the size of the array, list, etc.message
- Message to prepend to the exception- Returns:
- the index
- Throws:
IndexOutOfBoundsException
- if index is negative or greater than equal to sizeIllegalArgumentException
- if size is negative
-
checkPositionIndex
public static int checkPositionIndex(int index, int size, String message)
Checks that the given index is in the range[0, size]
- Parameters:
index
- the index to validatesize
- the size of the array, list, etc.message
- Message to prepend to the exception- Returns:
- the index
- Throws:
IndexOutOfBoundsException
- if index is negative or greater than sizeIllegalArgumentException
- if size is negative
-
checkPositionIndex
public static int checkPositionIndex(int index, int size)
Checks that the given index is in the range[0, size]
- Parameters:
index
- the index to validatesize
- the size of the array, list, etc.- Returns:
- the index
- Throws:
IndexOutOfBoundsException
- if index is negative or greater than sizeIllegalArgumentException
- if size is negative
-
checkState
public static void checkState(boolean evaluation)
Throws aIllegalStateException
if the given boolean evaluates to false.- Parameters:
evaluation
- the object to check
-
checkState
public static void checkState(boolean evaluation, String message)
Throws aIllegalStateException
if the given boolean evaluates to false.- Parameters:
evaluation
- the object to checkmessage
- the message to use in theIllegalStateException
-
checkState
public static void checkState(boolean evaluation, Supplier<String> messageSupplier)
Check state.- Parameters:
evaluation
- the evaluationmessageSupplier
- the message supplier
-
notNull
public static <T> T notNull(T object)
Throws aNullPointerException
if the given object is null.- Type Parameters:
T
- the type of the given object- Parameters:
object
- the object to check- Returns:
- the object
-
notNull
public static <T> T notNull(T object, String message)
Throws aNullPointerException
if the given object is null.- Type Parameters:
T
- the type of the given object- Parameters:
object
- the object to checkmessage
- the message to use in theNullPointerException
- Returns:
- the object
-
notNullOrBlank
public static String notNullOrBlank(String string)
Throws aIllegalArgumentException
if the given string is null or blank.- Parameters:
string
- the string to check- Returns:
- the object
-
notNullOrBlank
public static String notNullOrBlank(String string, String message)
Throws aIllegalArgumentException
if the given string is null or blank.- Parameters:
string
- the string to checkmessage
- the message to use in theIllegalArgumentException
- Returns:
- the object
-
notNullOrBlank
public static String notNullOrBlank(String string, @NonNull @NonNull Supplier<String> supplier)
Throws aIllegalArgumentException
if the given string is null or blank.- Parameters:
string
- the string to checksupplier
- the message supplier to use in theIllegalArgumentException
- Returns:
- the object
-
validate
public static void validate(boolean evaluation, @NonNull @NonNull Supplier<RuntimeException> exceptionSupplier)
Checks that the given evaluation or true and if it is not generates aRuntimeException
using the givenSupplier
and throws it.- Parameters:
evaluation
- the evaluation to testexceptionSupplier
- the supplier providing the exception to throw.
-
validate
public static <T> T validate(boolean evaluation, @NonNull @NonNull Supplier<RuntimeException> exceptionSupplier, T returnValue)
Checks that the evaluation is true. If evaluation is true, the given return value is returned, otherwise the exceptionSupplier
is used to generate aRuntimeException
to throw.- Type Parameters:
T
- the return type parameter- Parameters:
evaluation
- the evaluation testexceptionSupplier
- the supplier to use to generate exceptions (null will cause a default RuntimeException to be thrown)returnValue
- the return value to return if the evaluation is true- Returns:
- the return value
-
validate
public static <T> T validate(T value, @NonNull @NonNull Predicate<T> evaluator, @NonNull @NonNull Supplier<RuntimeException> exceptionSupplier, boolean nullable)
Uses the givenPredicate
to evaluate the given value. If evaluation is true, the given value is returned, otherwise the exception supplier is used to generate aRuntimeException
to throw. When the value is null and it is not allowed to be nullable aNullPointerException
wil be thrown.- Type Parameters:
T
- the return type parameter- Parameters:
value
- the value to testevaluator
- the predicate to use to test the valueexceptionSupplier
- the supplier to use to generate exceptionsnullable
- True the value is able to be null, False it cannot be null- Returns:
- the given value
-
validateArg
public static <T> T validateArg(T value, @NonNull @NonNull Predicate<T> predicate, String message, boolean nullable)
Checks that the evaluation is true. If evaluation is true, the given value is returned, otherwise the exception supplier is used to generate anIllegalArgumentException
to throw.- Type Parameters:
T
- the return type parameter- Parameters:
value
- the value to testpredicate
- the predicatemessage
- the message to use when creating the runtime exceptionnullable
- True the value is able to be null, False it cannot be null- Returns:
- the given value
-
-