Class Check
Check class provides static factory methods for IntCheck and
ObjectCheck instances, which do the actual orchestration of the checks to
be executed. The Check class does contain a few validation methods itself,
like Check.fromTo() and
Check.offsetLength(). These stand somewhat
apart from the rest of the Klojang Check. They are included for convenience.
See the User Guide for a detailed description of Defensive Programming using Klojang Check.
- Author:
- Ayco Holleman
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TAlways throws anIllegalArgumentExceptionwith the specified message and message arguments.static <T, X extends Throwable>
TAlways throws the exception produced by the specified exception factory with the specified message and message arguments.static <T, X extends Throwable>
TAlways throws the exception supplied by the specifiedSupplier.static intfromTo(int size, int fromIndex, int toIndex) All-in-one check for the provided size, from-index and to-index.static intAll-in-one check for the provided string, from-index and to-index.static intAll-in-one check for the provided list, from-index and to-index.static <T> intfromTo(T[] array, int fromIndex, int toIndex) All-in-one check for the provided array, from-index and to-index.static <T> ObjectCheck<T, IllegalArgumentException> notNull(T value) Static factory method.static <T> ObjectCheck<T, IllegalArgumentException> Static factory method.static voidoffsetLength(byte[] array, int offset, int length) All-in-one check for the specified array, offset and length.static voidoffsetLength(int size, int offset, int length) All-in-one check for the provided size, offset and length.Static factory method.Static factory method.static <T, X extends Exception>
ObjectCheck<T, X> Static factory method.static <T, X extends Exception>
ObjectCheck<T, X> Static factory method.static IntCheck<IllegalArgumentException> that(int value) Static factory method.static IntCheck<IllegalArgumentException> Static factory method.static <T> ObjectCheck<T, IllegalArgumentException> that(T value) Static factory method.static <T> ObjectCheck<T, IllegalArgumentException> Static factory method.
-
Method Details
-
that
Static factory method. Returns anIntCheckinstance suitable for testingintvalues.- Parameters:
value- the value to be validated- Returns:
- an
IntCheckinstance suitable for testingintvalues
-
that
Static factory method. Returns anObjectCheckinstance suitable for validating values of type<T>.- Type Parameters:
T- the type of the value to be validated- Parameters:
value- the value to be validated- Returns:
- an
ObjectCheckinstance suitable for validating values of type<T>.
-
that
Static factory method. Returns anIntCheckinstance suitable for validatingintvalues.- Parameters:
value- the value to be validatedtag- a descriptive name for the value (in case the value is a method argument probably something close to the parameter name)- Returns:
- an
IntCheckinstance suitable for testingintvalues
-
that
Static factory method. Returns anObjectCheckinstance suitable for validating values of type<T>.- Type Parameters:
T- the type of the value to be validated- Parameters:
value- the value to be validatedtag- a descriptive name for the value (in case the value is a method argument probably something close to the parameter name)- Returns:
- an
ObjectCheckinstance suitable for validating values of type<T>.
-
notNull
public static <T> ObjectCheck<T, IllegalArgumentException> notNull(T value) throws NullPointerException Static factory method. Returns anObjectCheckinstance suitable for validating values of type<T>if the specified value is notnull, else throws aNullPointerException. null test. AnIllegalArgumentExceptionwill be thrown if the specified value fails any of the subsequently specified checks.- Type Parameters:
T- the type of the value to be validated- Parameters:
value- the value to be validated- Returns:
- an
ObjectCheckinstance suitable for validating values of type<T>. - Throws:
NullPointerException- if the argument isnull
-
notNull
public static <T> ObjectCheck<T, IllegalArgumentException> notNull(T value, String tag) throws NullPointerException Static factory method. Returns anObjectCheckinstance suitable for validating values of type<T>if the specified value is notnull, else throws aNullPointerException. null test. AnIllegalArgumentExceptionwill be thrown if the specified value fails any of the subsequently specified checks.- Type Parameters:
T- the type of the value to be validated- Parameters:
value- the value to be validatedtag- a descriptive name for the value (in case the value is a method argument probably something close to the parameter name)- Returns:
- an
ObjectCheckinstance suitable for validating values of type<T>. - Throws:
NullPointerException- if the argument isnull
-
on
Static factory method. Returns anIntCheckinstance suitable for validatingintvalues. If the value fails any of the tests passed to theIntCheckinstance, the exception produced by the provided exception factory is thrown.- Type Parameters:
X- the type ofExceptionthrown if the value fails to pass a test- Parameters:
excFactory- a function that will produce the exception if the value fails to pass a test. The function will be pass the exception message and must return the exception to be thrownvalue- the value to be validated- Returns:
- an
IntCheckinstance suitable for testingintvalues
-
on
Static factory method. Returns anObjectCheckinstance suitable for validating values of type<T>. If the value fails any of the tests passed to theObjectCheckinstance, the exception produced by the provided exception factory is thrown.- Type Parameters:
T- the type of the value to be validatedX- the type ofExceptionthrown if the value fails to pass a test- Parameters:
excFactory- a function that will produce the exception if the value fails to pass a test. The function will be pass the exception message and must return the exception to be thrownvalue- the value to be validated- Returns:
- an
ObjectCheckinstance suitable for validating values of type<T>.
-
on
public static <X extends Exception> IntCheck<X> on(Function<String, X> excFactory, int value, String tag) Static factory method. Returns anIntCheckinstance suitable for validatingintvalues. If the value fails any of the tests passed to theIntCheckinstance, the exception produced by the provided exception factory is thrown.- Type Parameters:
X- the type ofExceptionthrown if the value fails to pass a test- Parameters:
excFactory- a function that will produce the exception if the value fails to pass a test. The function will be pass the exception message and must return the exception to be thrownvalue- the value to be validatedtag- a descriptive name for the value (in case the value is a method argument probably something close to the parameter name)- Returns:
- an
IntCheckinstance suitable for testingintvalues
-
on
public static <T, X extends Exception> ObjectCheck<T,X> on(Function<String, X> excFactory, T value, String tag) Static factory method. Returns a newCheckinstance suitable for testing the provided argument.- Type Parameters:
T- the type of the value to be validatedX- the type ofExceptionthrown if the value fails to pass a test- Parameters:
excFactory- a function that will produce the exception if the value fails to pass a test. The function will be pass the exception message and must return the exception to be thrownvalue- the value to be validatedtag- a descriptive name for the value (in case the value is a method argument probably something close to the parameter name)- Returns:
- an
ObjectCheckinstance suitable for validating values of type<T>.
-
offsetLength
public static void offsetLength(byte[] array, int offset, int length) All-in-one check for the specified array, offset and length.
- throws an
NullPointerExceptionifarrayisnull. - throws an
IndexOutOfBoundsExceptionifoffsetorlengthis negative - throws an
IndexOutOfBoundsExceptionifoffset+length > array.length
NB The
fromTo()andoffsetLength()checks stand somewhat apart from the rest of the check framework. They happen through "ordinary" static utility methods and they test multiple things at once. They are included for convenience.- Parameters:
array- the arrayoffset- the offset within the arraylength- the length of the segment- See Also:
- throws an
-
offsetLength
public static void offsetLength(int size, int offset, int length) All-in-one check for the provided size, offset and length. The
sizeargument supposedly is the size or length of an array or array-like object.- throws an
IndexOutOfBoundsExceptionifsize,offsetorlengthis negative - throws an
IndexOutOfBoundsExceptionifoffset+length>size
NB The
fromTo()andoffsetLength()checks stand somewhat apart from the rest of the check framework. They happen through "ordinary" static utility methods and they test multiple things at once. They are included for convenience.- Parameters:
size- the length/size of the array or array-like objectoffset- the offsetlength- the length of the segment
- throws an
-
fromTo
All-in-one check for the provided list, from-index and to-index.
- Throws a
NullPointerExceptioniflistisnull - Throws an
IndexOutOfBoundsExceptioniffromIndex < 0 - Throws an
IndexOutOfBoundsExceptioniftoIndex<fromIndex - Throws an
IndexOutOfBoundsExceptioniftoIndex>list.size()
NB The
fromTo()andoffsetLength()checks stand somewhat apart from the rest of the check framework. They happen through "ordinary" static utility methods and they test multiple things at once. They are included for convenience.- Parameters:
list- the listfromIndex- the start index of the sublisttoIndex- the end index of the sublist- Returns:
- the
sizeof the sublist - See Also:
- Throws a
-
fromTo
public static <T> int fromTo(T[] array, int fromIndex, int toIndex) All-in-one check for the provided array, from-index and to-index.
- Throws a
NullPointerExceptionif the array isnull - Throws an
IndexOutOfBoundsExceptioniffromIndexortoIndexis negative - Throws an
IndexOutOfBoundsExceptioniftoIndex < fromIndex - Throws an
IndexOutOfBoundsExceptioniftoIndex > array.length
NB The
fromTo()andoffsetLength()checks stand somewhat apart from the rest of the check framework. They happen through "ordinary" static utility methods and they test multiple things at once. They are included for convenience.- Type Parameters:
T- the type of the array elements- Parameters:
array- the arrayfromIndex- the start index of the array segmenttoIndex- the end index of the array segment- Returns:
- the
lengthof the array segment - See Also:
- Throws a
-
fromTo
All-in-one check for the provided string, from-index and to-index.
- Throws a
NullPointerExceptionifstringisnull - Throws an
IndexOutOfBoundsExceptioniffromIndexortoIndexis negative - Throws an
IndexOutOfBoundsExceptioniftoIndex < fromIndex - Throws an
IndexOutOfBoundsExceptioniftoIndex > string.length()
NB The
fromTo()andoffsetLength()checks stand somewhat apart from the rest of the check framework. They happen through "ordinary" static utility methods and they test multiple things at once. They are included for convenience.- Parameters:
string- the stringfromIndex- the start index of the substringtoIndex- the end index of the substring- Returns:
- the
lengthof the substring - See Also:
- Throws a
-
fromTo
public static int fromTo(int size, int fromIndex, int toIndex) All-in-one check for the provided size, from-index and to-index. The
sizeargument supposedly is the size or length of an array or array-like object.- Throws an
IndexOutOfBoundsExceptionifsizeorfromIndexortoIndexis negative - Throws an
IndexOutOfBoundsExceptioniftoIndex < fromIndex - Throws an
IndexOutOfBoundsExceptioniftoIndex > size
NB The
fromTo()andoffsetLength()checks stand somewhat apart from the rest of the check framework. They happen through "ordinary" static utility methods and they test multiple things at once. They are included for convenience.- Parameters:
size- the size (or length) of the array, string, list, etc.fromIndex- the start index of the segmenttoIndex- the end index of the segment- Returns:
- the
lengthof the segment
- Throws an
-
fail
Always throws anIllegalArgumentExceptionwith the specified message and message arguments. The method is still declared to return a value of type<T>so it can be used as the expression for areturnstatement.- Type Parameters:
T- the desired type of the return value- Parameters:
message- the message (pattern)msgArgs- the message arguments. The first message argument within the message pattern would be${0}; the second would be${1}, etc. For more information, see Custom Error Messages.- Returns:
- nothing, but allows
fail()to be used as the expression in areturnstatement - Throws:
IllegalArgumentException- always
-
fail
Always throws the exception supplied by the specifiedSupplier. The method is still declared to return a value of type<T>so it can be used as the expression for areturnstatement.- Type Parameters:
T- the desired type of the return valueX- the type of the exception- Parameters:
excFactory- the supplier of the exception- Returns:
- nothing, but allows
fail()to be used as the expression in areturnstatement - Throws:
X- always
-
fail
public static <T, X extends Throwable> T fail(Function<String, X> excFactory, String message, Object... msgArgs) throws XAlways throws the exception produced by the specified exception factory with the specified message and message arguments. The method is still declared to return a value of type<T>so it can be used as the expression for areturnstatement.- Type Parameters:
T- the type of the object that would have been returned if it had passed the checksX- the type of the exception- Parameters:
excFactory- a function that takes aString(the exception message) and produces anException.message- the messagemsgArgs- the message arguments. The first message argument within the message pattern would be${0}; the second would be${1}, etc. For more information, see Custom Error Messages.- Returns:
- nothing, but allows
failto be used as the expression in areturnstatement - Throws:
X- always
-