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> T
Always throws anIllegalArgumentException
with 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 int
fromTo
(int size, int fromIndex, int toIndex) All-in-one check for the provided size, from-index and to-index.static int
All-in-one check for the provided string, from-index and to-index.static int
All-in-one check for the provided list, from-index and to-index.static <T> int
fromTo
(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 void
offsetLength
(byte[] array, int offset, int length) All-in-one check for the specified array, offset and length.static void
offsetLength
(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 anIntCheck
instance suitable for testingint
values.- Parameters:
value
- the value to be validated- Returns:
- an
IntCheck
instance suitable for testingint
values
-
that
Static factory method. Returns anObjectCheck
instance 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
ObjectCheck
instance suitable for validating values of type<T>
.
-
that
Static factory method. Returns anIntCheck
instance suitable for validatingint
values.- 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
IntCheck
instance suitable for testingint
values
-
that
Static factory method. Returns anObjectCheck
instance 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
ObjectCheck
instance suitable for validating values of type<T>
.
-
notNull
public static <T> ObjectCheck<T,IllegalArgumentException> notNull(T value) throws NullPointerException Static factory method. Returns anObjectCheck
instance suitable for validating values of type<T>
if the specified value is notnull
, else throws aNullPointerException
. null test. AnIllegalArgumentException
will 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
ObjectCheck
instance 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 anObjectCheck
instance suitable for validating values of type<T>
if the specified value is notnull
, else throws aNullPointerException
. null test. AnIllegalArgumentException
will 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
ObjectCheck
instance suitable for validating values of type<T>
. - Throws:
NullPointerException
- if the argument isnull
-
on
Static factory method. Returns anIntCheck
instance suitable for validatingint
values. If the value fails any of the tests passed to theIntCheck
instance, the exception produced by the provided exception factory is thrown.- Type Parameters:
X
- the type ofException
thrown 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
IntCheck
instance suitable for testingint
values
-
on
Static factory method. Returns anObjectCheck
instance suitable for validating values of type<T>
. If the value fails any of the tests passed to theObjectCheck
instance, the exception produced by the provided exception factory is thrown.- Type Parameters:
T
- the type of the value to be validatedX
- the type ofException
thrown 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
ObjectCheck
instance 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 anIntCheck
instance suitable for validatingint
values. If the value fails any of the tests passed to theIntCheck
instance, the exception produced by the provided exception factory is thrown.- Type Parameters:
X
- the type ofException
thrown 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
IntCheck
instance suitable for testingint
values
-
on
public static <T,X extends Exception> ObjectCheck<T,X> on(Function<String, X> excFactory, T value, String tag) Static factory method. Returns a newCheck
instance suitable for testing the provided argument.- Type Parameters:
T
- the type of the value to be validatedX
- the type ofException
thrown 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
ObjectCheck
instance 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
NullPointerException
ifarray
isnull
. - throws an
IndexOutOfBoundsException
ifoffset
orlength
is negative - throws an
IndexOutOfBoundsException
ifoffset+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
size
argument supposedly is the size or length of an array or array-like object.- throws an
IndexOutOfBoundsException
ifsize
,offset
orlength
is negative - throws an
IndexOutOfBoundsException
ifoffset+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
NullPointerException
iflist
isnull
- Throws an
IndexOutOfBoundsException
iffromIndex < 0
- Throws an
IndexOutOfBoundsException
iftoIndex
<fromIndex
- Throws an
IndexOutOfBoundsException
iftoIndex
>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
size
of 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
NullPointerException
if the array isnull
- Throws an
IndexOutOfBoundsException
iffromIndex
ortoIndex
is negative - Throws an
IndexOutOfBoundsException
iftoIndex < fromIndex
- Throws an
IndexOutOfBoundsException
iftoIndex > 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
length
of the array segment - See Also:
- Throws a
-
fromTo
All-in-one check for the provided string, from-index and to-index.
- Throws a
NullPointerException
ifstring
isnull
- Throws an
IndexOutOfBoundsException
iffromIndex
ortoIndex
is negative - Throws an
IndexOutOfBoundsException
iftoIndex < fromIndex
- Throws an
IndexOutOfBoundsException
iftoIndex > 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
length
of 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
size
argument supposedly is the size or length of an array or array-like object.- Throws an
IndexOutOfBoundsException
ifsize
orfromIndex
ortoIndex
is negative - Throws an
IndexOutOfBoundsException
iftoIndex < fromIndex
- Throws an
IndexOutOfBoundsException
iftoIndex > 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
length
of the segment
- Throws an
-
fail
Always throws anIllegalArgumentException
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 areturn
statement.- 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 areturn
statement - 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 areturn
statement.- 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 areturn
statement - 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 areturn
statement.- 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
fail
to be used as the expression in areturn
statement - Throws:
X
- always
-