|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.qualitycheck.ConditionalCheck
public final class ConditionalCheck
This class adds conditional checks to test your arguments to be valid. The checks are the same as offered in
Check
, but all have an additional condition. The check is only executed if the condition is @code
true
.
Examples for ConditionalCheck's are parameters that must be checked if they are not null. E.g.
public void method(@Nullable final String numericArgument, @Nullable final List
There are also cases where you can do more optimized technical checks:
public void method(final String address) {
ConditionalCheck.matchesPattern(address.length() <= 15,
Pattern.compile("\\d{3}.\\d{3}.\\d{3}.\\d{3}"), address);
ConditionalCheck.matchesPattern(address.length() > 15,
Pattern.compile("[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}:[a-f0-9]{2}"), address);
}
Use ConditionalCheck
with care! Coditional checks are often an indicator for bad design. Good APIs accept
only values that are always valid. Seldom there are arguments which must be checked only in certain cases.
Additionally, take care that you do not mix up technical and functional checks. Conditional checks often tend to be
more functional than the technical checks offered by Check
.
Logical checks should not be done using ConditionalCheck
, because the way checks work one cannot be sure if
every branch is covered by measuring the branch coverage.
Method Summary | ||
---|---|---|
static
|
contains(boolean condition,
Collection<T> haystack,
T needle)
Ensures that an element needle is contained in a collection haystack . |
|
static
|
contains(boolean condition,
Collection<T> haystack,
T needle,
String name)
Ensures that an element needle is contained in a collection haystack . |
|
static void |
equals(boolean condition,
boolean expected,
boolean check)
Ensures that a passed boolean is equal to another boolean. |
|
static void |
equals(boolean condition,
boolean expected,
boolean check,
String message)
Ensures that a passed boolean is equal to another boolean. |
|
static void |
equals(boolean condition,
byte expected,
byte check)
Ensures that a passed byte is equal to another byte. |
|
static void |
equals(boolean condition,
byte expected,
byte check,
String message)
Ensures that a passed byte is equal to another byte. |
|
static void |
equals(boolean condition,
char expected,
char check)
Ensures that a passed char is equal to another char. |
|
static void |
equals(boolean condition,
char expected,
char check,
String message)
Ensures that a passed char is equal to another char. |
|
static void |
equals(boolean condition,
int expected,
int check)
Ensures that a passed intH is equal to another int. |
|
static void |
equals(boolean condition,
int expected,
int check,
String message)
Ensures that a passed int is equal to another int. |
|
static void |
equals(boolean condition,
long expected,
long check)
Ensures that a passed long is equal to another long. |
|
static void |
equals(boolean condition,
long expected,
long check,
String message)
Ensures that a passed long is equal to another long. |
|
static void |
equals(boolean condition,
short expected,
short check)
Ensures that a passed short is equal to another short. |
|
static void |
equals(boolean condition,
short expected,
short check,
String message)
Ensures that a passed short is equal to another short. |
|
static
|
equals(boolean condition,
T expected,
T check)
Ensures that a passed object is equal to another object. |
|
static
|
equals(boolean condition,
T expect,
T check,
String msg)
Ensures that a passed object is equal to another object. |
|
static
|
greaterOrEqualThan(boolean condition,
T expected,
T check)
Ensures that a passed Comparable is greater than or equal to Comparable . |
|
static
|
greaterOrEqualThan(boolean condition,
T expected,
T check,
String message)
Ensures that a passed Comparable is greater than or equal to another Comparable . |
|
static
|
greaterThan(boolean condition,
T expected,
T check)
Ensures that a passed Comparable is greater to another Comparable . |
|
static
|
greaterThan(boolean condition,
T expected,
T check,
String message)
Ensures that a passed Comparable is greater than another Comparable . |
|
static void |
hasAnnotation(boolean condition,
Class<?> clazz,
Class<? extends Annotation> annotation)
Ensures that a passed class has an annotation of a specific type |
|
static
|
instanceOf(boolean condition,
Class<?> type,
Object obj)
Ensures that a passed argument is a member of a specific type. |
|
static
|
instanceOf(boolean condition,
Class<?> type,
Object obj,
String name)
Ensures that a passed argument is a member of a specific type. |
|
static void |
isNull(boolean condition,
Object reference)
Ensures that a given argument is null . |
|
static void |
isNull(boolean condition,
Object reference,
String name)
Ensures that a given argument is null . |
|
static void |
isNumber(boolean condition,
String value)
Ensures that a String argument is a number. |
|
static
|
isNumber(boolean condition,
String value,
Class<T> type)
Ensures that a String argument is a number. |
|
static void |
isNumber(boolean condition,
String value,
String name)
Ensures that a string argument is a number according to Integer.parseInt |
|
static
|
isNumber(boolean condition,
String value,
String name,
Class<T> type)
Ensures that a String argument is a number. |
|
static
|
isNumeric(boolean condition,
T value)
Ensures that a readable sequence of char values is numeric. |
|
static
|
isNumeric(boolean condition,
T value,
String name)
Ensures that a readable sequence of char values is numeric. |
|
static
|
lesserThan(boolean condition,
T expected,
T check)
Ensures that a passed Comparable is less than another Comparable . |
|
static
|
lesserThan(boolean condition,
T expected,
T check,
String message)
Ensures that a passed Comparable is less than another Comparable . |
|
static
|
matchesPattern(boolean condition,
Pattern pattern,
T chars)
Ensures that a readable sequence of char values matches a specified pattern. |
|
static
|
matchesPattern(boolean condition,
Pattern pattern,
T chars,
String name)
Ensures that a readable sequence of char values matches a specified pattern. |
|
static
|
noNullElements(boolean condition,
T iterable)
Ensures that an iterable reference is neither null nor contains any elements that are null . |
|
static
|
noNullElements(boolean condition,
T[] array)
Ensures that an array does not contain null . |
|
static
|
noNullElements(boolean condition,
T[] array,
String name)
Ensures that an array does not contain null . |
|
static
|
noNullElements(boolean condition,
T iterable,
String name)
Ensures that an iterable reference is neither null nor contains any elements that are null . |
|
static void |
notEmpty(boolean condition,
boolean expression)
Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the emptiness. |
|
static void |
notEmpty(boolean condition,
boolean expression,
String name)
Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the emptiness. |
|
static
|
notEmpty(boolean condition,
T chars)
Ensures that a passed string as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T collection)
Ensures that a passed collection as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T map)
Ensures that a passed map as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T[] array)
Ensures that a passed map as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T[] array,
String name)
Ensures that a passed map as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T reference,
boolean expression,
String name)
Ensures that an object reference passed as a parameter to the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T chars,
String name)
Ensures that a passed string as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T collection,
String name)
Ensures that a passed collection as a parameter of the calling method is not empty. |
|
static
|
notEmpty(boolean condition,
T map,
String name)
Ensures that a passed map as a parameter of the calling method is not empty. |
|
static void |
notNaN(boolean condition,
double value)
Ensures that a double argument is not NaN (not a number). |
|
static void |
notNaN(boolean condition,
double value,
String name)
Ensures that a double argument is not NaN (not a number). |
|
static void |
notNaN(boolean condition,
float value)
Ensures that a double argument is not NaN (not a number). |
|
static void |
notNaN(boolean condition,
float value,
String name)
Ensures that a double argument is not NaN (not a number). |
|
static void |
notNegative(boolean condition,
int value)
Ensures that an integer reference passed as a parameter to the calling method is not smaller than 0 . |
|
static void |
notNegative(boolean condition,
int value,
String name)
Ensures that an integer reference passed as a parameter to the calling method is not smaller than 0 . |
|
static
|
notNull(boolean condition,
T reference)
Ensures that an object reference passed as a parameter to the calling method is not null . |
|
static
|
notNull(boolean condition,
T reference,
String name)
Ensures that an object reference passed as a parameter to the calling method is not null . |
|
static void |
notPositive(boolean condition,
int value)
Ensures that an integer reference passed as a parameter to the calling method is not greater than 0 . |
|
static void |
notPositive(boolean condition,
int value,
String name)
Ensures that an integer reference passed as a parameter to the calling method is not greater than 0 . |
|
static void |
positionIndex(boolean condition,
int index,
int size)
Ensures that a given position index is valid within the size of an array, list or string ... |
|
static void |
range(boolean condition,
int start,
int end,
int size)
Ensures that the given arguments are a valid range. |
|
static void |
stateIsTrue(boolean condition,
boolean expression)
Ensures that a given state is true . |
|
static void |
stateIsTrue(boolean condition,
boolean expression,
Class<? extends RuntimeException> clazz)
Ensures that a given state is true and allows to specify the class of exception which is thrown in case
the state is not true . |
|
static void |
stateIsTrue(boolean condition,
boolean expression,
String description)
Ensures that a given state is true . |
|
static void |
stateIsTrue(boolean condition,
boolean expression,
String descriptionTemplate,
Object... descriptionTemplateArgs)
Ensures that a given state is true |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static <T> void contains(boolean condition, @Nonnull Collection<T> haystack, @Nonnull T needle)
needle
is contained in a collection haystack
.
This is in particular useful if you want to check whether an enum value is contained in an EnumSet
. The
check is implemented using Collection.contains(Object)
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.contains(Collection, Object, String)
and pass as second
argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedhaystack
- A collection which must contain needle
needle
- An object that must be contained into a collection.
IllegalArgumentNotContainedException
- if the passed needle
can not be found in haystack
public static <T> void contains(boolean condition, @Nonnull Collection<T> haystack, @Nonnull T needle, @Nonnull String name)
needle
is contained in a collection haystack
.
This is in particular useful if you want to check whether an enum value is contained in an EnumSet
. The
check is implemented using Collection.contains(Object)
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedhaystack
- A collection which must contain needle
needle
- An object that must be contained into a collection.name
- name of argument of needle
IllegalArgumentNotContainedException
- if the passed needle
can not be found in haystack
public static void equals(boolean condition, @Nonnull boolean expected, @Nonnull boolean check)
expected != check
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.equals(boolean, boolean, String)
and pass as second
argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- boolean to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull boolean expected, @Nonnull boolean check, String message)
expected != check
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- boolean to be checkedmessage
- an error message describing why the booleans must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull byte expected, @Nonnull byte check)
expected != check
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.equals(byte, byte, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- byte to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull byte expected, @Nonnull byte check, String message)
expected != check
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- byte to be checkedmessage
- an error message describing why the bytes must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull char expected, @Nonnull char check)
expected != check
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.equals(char, char, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- char to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull char expected, @Nonnull char check, String message)
expected != check
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- char to be checkedmessage
- an error message describing why the chars must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull int expected, @Nonnull int check)
expected != check
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.equals(int, int, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- int to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull int expected, @Nonnull int check, String message)
expected != check
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- int to be checkedmessage
- an error message describing why the ints must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull long expected, @Nonnull long check)
expected != check
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.equals(long, long, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- long to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull long expected, @Nonnull long check, String message)
expected != check
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- long to be checkedmessage
- an error message describing why the longs must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull short expected, @Nonnull short check)
expected != check
.
The condition must evaluate to true
so that the check is executed.
We recommend to use the overloaded method Check.equals(short, short, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- short to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static void equals(boolean condition, @Nonnull short expected, @Nonnull short check, String message)
expected != check
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- short to be checkedmessage
- an error message describing why the shorts must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static <T> T equals(boolean condition, @Nonnull T expected, @Nonnull T check)
expected.equals(check)
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- Object to be checked
IllegalNotEqualException
- if both argument values are not equalpublic static <T> void equals(boolean condition, @Nonnull T expect, @Nonnull T check, String msg)
expected.equals(check)
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpect
- expected valuecheck
- object to be checkedmsg
- an error message describing why the objects must equal (will be passed to
IllegalNotEqualException
)
IllegalNotEqualException
- if both argument values are not equalpublic static <T extends Comparable<T>> void greaterOrEqualThan(boolean condition, @Nonnull T expected, @Nonnull T check)
Comparable
is greater than or equal to Comparable
. The comparison is made
using expected.compareTo(check) > 0
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
so that the check will be performedexpected
- Expected valuecheck
- Comparable to be checked
IllegalNotGreaterOrEqualThanException
- if the argument value check
is not greater or equal than value expected
when using
method compareTo
public static <T extends Comparable<T>> void greaterOrEqualThan(boolean condition, @Nonnull T expected, @Nonnull T check, String message)
Comparable
is greater than or equal to another Comparable
. The comparison
is made using expected.compareTo(check) > 0
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
so that the check will be performedexpected
- Expected valuecheck
- Comparable to be checkedmessage
- an error message describing why the comparable must be greater than or equal a value (will be passed
to IllegalNotGreaterOrEqualThanException
)
IllegalNotGreaterOrEqualThanException
- if the argument value check
is not greater than or equal to value expected
when using
method compareTo
public static <T extends Comparable<T>> void greaterThan(boolean condition, @Nonnull T expected, @Nonnull T check)
Comparable
is greater to another Comparable
. The comparison is made using
expected.compareTo(check) >= 0
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- Comparable to be checked
IllegalNotGreaterThanException
- if the argument value check
is not greater than value expected
when using method
compareTo
public static <T extends Comparable<T>> void greaterThan(boolean condition, @Nonnull T expected, @Nonnull T check, String message)
Comparable
is greater than another Comparable
. The comparison is made using
expected.compareTo(check) >= 0
.
The condition must evaluate to true
so that the check is executed.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- Comparable to be checkedmessage
- an error message describing why the comparable must be greater than a value (will be passed to
IllegalNotGreaterThanException
)
IllegalNotGreaterThanException
- if the argument value check
is not greater than value expected
when using method
compareTo
public static void hasAnnotation(boolean condition, @Nonnull Class<?> clazz, @Nonnull Class<? extends Annotation> annotation)
condition
- condition must be true
^ so that the check will be performedclazz
- the class that must have a required annotationannotation
- the type of annotation that is required on the class
IllegalMissingAnnotationException
- if the passed annotation is not annotated at the given classpublic static <T> T instanceOf(boolean condition, @Nonnull Class<?> type, @Nonnull Object obj)
condition
- condition must be true
^ so that the check will be performedtype
- class that the given object is a member ofobj
- the object reference that should be a member of a specific type
IllegalInstanceOfArgumentException
- if the given argument obj
is not a member of type
public static <T> T instanceOf(boolean condition, @Nonnull Class<?> type, @Nonnull Object obj, @Nullable String name)
condition
- condition must be true
^ so that the check will be performedtype
- class that the given object is a member ofobj
- the object reference that should be a member of a specific type
name
- name of object reference (in source code)
IllegalInstanceOfArgumentException
- if the given argument obj
is not a member of type
public static void isNull(boolean condition, @Nullable Object reference)
null
.
Normally, the usage of null
arguments is disregarded by the authors of quality-check. Still, there are
certain circumstances where null is required, e.g. the primary key of an entity before it is written to the
database for the first time. In such cases it is ok to use null values and there should also be checks for them.
For example, to avoid overwriting an existing primary key with a new one.
condition
- condition must be true
^ so that the check will be performedreference
- reference which must be null
IllegalNotNullArgumentException
- if the given argument reference
is not nullpublic static void isNull(boolean condition, @Nullable Object reference, @Nullable String name)
null
.
Normally, the usage of null
arguments is disregarded by the authors of quality-check. Still, there are
certain circumstances where null is required, e.g. the primary key of an entity before it is written to the
database for the first time. In such cases it is ok to use null values and there should also be checks for them.
For example, to avoid overwriting an existing primary key with a new one.
condition
- condition must be true
^ so that the check will be performedreference
- reference which must be null.name
- name of object reference (in source code)
IllegalNotNullArgumentException
- if the given argument reference
is not nullpublic static void isNumber(boolean condition, @Nonnull String value)
condition
- condition must be true
^ so that the check will be performedvalue
- value which must be a number
IllegalNumberArgumentException
- if the given argument value
is not a numberpublic static <T extends Number> void isNumber(boolean condition, @Nonnull String value, @Nonnull Class<T> type)
Number
. The number
is first converted to a BigInteger
condition
- condition must be true
^ so that the check will be performedvalue
- value which must be a numbertype
- requested return value type, must be a subclass of Number
, i.e. one of BigDecimal,
BigInteger, Byte, Double, Float, Integer, Long, Short
IllegalNumberArgumentException
- if the given argument value
is no numberpublic static void isNumber(boolean condition, @Nonnull String value, @Nullable String name)
Integer.parseInt
condition
- condition must be true
^ so that the check will be performedvalue
- value which must be a numbername
- name of object reference (in source code)
IllegalNumberArgumentException
- if the given argument value
is no numberpublic static <T extends Number> void isNumber(boolean condition, @Nonnull String value, @Nullable String name, @Nonnull Class<T> type)
Number
. The number
is first converted to a BigDecimal
or BigInteger
. Floating point types are only supported if the
type
is one of Float, Double, BigDecimal
.
This method does also check against the ranges of the given datatypes.
condition
- condition must be true
^ so that the check will be performedvalue
- value which must be a number and in the range of the given datatype.name
- (optional) name of object reference (in source code).type
- requested return value type, must be a subclass of Number
, i.e. one of BigDecimal,
BigInteger, Byte, Double, Float, Integer, Long, Short
IllegalNumberArgumentException
- if the given argument value
is no numberpublic static <T extends CharSequence> void isNumeric(boolean condition, @Nonnull T value)
char
values is numeric. Numeric arguments consist only of the
characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank
account number).
We recommend to use the overloaded method Check.isNumeric(CharSequence, String)
and pass as second
argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedvalue
- a readable sequence of char
values which must be a number
IllegalNumberArgumentException
- if the given argument value
is no numberpublic static <T extends CharSequence> void isNumeric(boolean condition, @Nonnull T value, @Nullable String name)
char
values is numeric. Numeric arguments consist only of the
characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank
account number).
condition
- condition must be true
^ so that the check will be performedvalue
- a readable sequence of char
values which must be a numbername
- name of object reference (in source code)
IllegalNumberArgumentException
- if the given argument value
is no numberpublic static <T extends Comparable<T>> void lesserThan(boolean condition, @Nonnull T expected, @Nonnull T check)
Comparable
is less than another Comparable
. The comparison is made using
expected.compareTo(check) <= 0
.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- Comparable to be checked
IllegalNotLesserThanException
- if the argument value check
is not lesser than value expected
when using method
compareTo
public static <T extends Comparable<T>> void lesserThan(boolean condition, @Nonnull T expected, @Nonnull T check, String message)
Comparable
is less than another Comparable
. The comparison is made using
expected.compareTo(check) <= 0
.
condition
- condition must be true
^ so that the check will be performedexpected
- Expected valuecheck
- Comparable to be checkedmessage
- an error message describing why the comparables must be less than a value (will be passed to
IllegalNotLessThanException
)
IllegalNotLesserThanException
- if the argument value check
is not lesser than value expected
when using method
compareTo
public static <T extends CharSequence> void matchesPattern(boolean condition, @Nonnull Pattern pattern, @Nonnull T chars)
char
values matches a specified pattern. If the given character
sequence does not match against the passed pattern, an IllegalPatternArgumentException
will be thrown.
We recommend to use the overloaded method Check.matchesPattern(Pattern, CharSequence, String)
and pass as
second argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedpattern
- pattern, that the chars
must correspond tochars
- a readable sequence of char
values which should match the given pattern
IllegalNullArgumentException
- if the given argument chars
is null
IllegalPatternArgumentException
- if the given chars
that does not match the pattern
public static <T extends CharSequence> void matchesPattern(boolean condition, @Nonnull Pattern pattern, @Nonnull T chars, @Nullable String name)
char
values matches a specified pattern. If the given character
sequence does not match against the passed pattern, an IllegalPatternArgumentException
will be thrown.
condition
- condition must be true
^ so that the check will be performedpattern
- pattern, that the chars
must correspond tochars
- a readable sequence of char
values which should match the given patternname
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument chars
is null
IllegalPatternArgumentException
- if the given chars
that does not match the pattern
public static <T extends Iterable<?>> void noNullElements(boolean condition, @Nonnull T iterable)
null
nor contains any elements that are null
.
We recommend to use the overloaded method Check.noNullElements(Iterable, String)
and pass as second
argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performediterable
- the iterable reference which should not contain null
IllegalNullElementsException
- if the given argument iterable
contains elements that are null
public static <T extends Iterable<?>> void noNullElements(boolean condition, @Nonnull T iterable, String name)
null
nor contains any elements that are null
.
condition
- condition must be true
^ so that the check will be performediterable
- the iterable reference which should not contain null
name
- name of object reference (in source code)
IllegalNullElementsException
- if the given argument iterable
contains elements that are null
public static <T> void noNullElements(boolean condition, @Nonnull T[] array)
null
.
We recommend to use the overloaded method Check.noNullElements(Object[], String)
and pass as second
argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedarray
- reference to an array
IllegalNullElementsException
- if the given argument array
contains null
public static <T> void noNullElements(boolean condition, @Nonnull T[] array, @Nullable String name)
null
.
condition
- condition must be true
^ so that the check will be performedarray
- reference to an arrayname
- name of object reference (in source code)
IllegalNullElementsException
- if the given argument array
contains null
public static void notEmpty(boolean condition, boolean expression)
We recommend to use the overloaded method Check.notEmpty(boolean, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedexpression
- the result of the expression to verify the emptiness of a reference (true
means empty,
false
means not empty)
IllegalNullArgumentException
- if the given argument reference
is null
IllegalEmptyArgumentException
- if the given argument reference
is emptypublic static void notEmpty(boolean condition, boolean expression, @Nullable String name)
condition
- condition must be true
^ so that the check will be performedexpression
- the result of the expression to verify the emptiness of a reference (true
means empty,
false
means not empty)name
- name of object reference (in source code)
IllegalEmptyArgumentException
- if the given argument reference
is emptypublic static <T extends CharSequence> void notEmpty(boolean condition, @Nonnull T chars)
We recommend to use the overloaded method Check.notEmpty(CharSequence, String)
and pass as second
argument the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedchars
- a readable sequence of char
values which should not be empty
IllegalNullArgumentException
- if the given argument reference
is null
IllegalEmptyArgumentException
- if the given argument reference
is emptypublic static <T extends Collection<?>> void notEmpty(boolean condition, @Nonnull T collection)
We recommend to use the overloaded method Check.notEmpty(Collection, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedcollection
- a collection which should not be empty
IllegalNullArgumentException
- if the given argument collection
is null
IllegalEmptyArgumentException
- if the given argument collection
is emptypublic static <T extends Map<?,?>> void notEmpty(boolean condition, @Nonnull T map)
We recommend to use the overloaded method Check.notEmpty(Collection, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedmap
- a map which should not be empty
IllegalNullArgumentException
- if the given argument map
is null
IllegalEmptyArgumentException
- if the given argument map
is emptypublic static <T> void notEmpty(boolean condition, @Nonnull T reference, boolean expression, @Nullable String name)
The following example describes how to use it.
@ArgumentsChecked public setText(String text) { ConditionalCheck.notEmpty(true, text, text.isEmpty(), "text"); this.text = text; }
condition
- condition must be true
^ so that the check will be performedreference
- an object reference which should not be emptyexpression
- the result of the expression to verify the emptiness of a reference (true
means empty,
false
means not empty)name
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument reference
is null
IllegalEmptyArgumentException
- if the given argument reference
is emptypublic static <T extends CharSequence> void notEmpty(boolean condition, @Nonnull T chars, @Nullable String name)
The following example describes how to use it.
@ArgumentsChecked public setText(String text) { this.text = Check.notEmpty(text, "text"); }
condition
- condition must be true
^ so that the check will be performedchars
- a readable sequence of char
values which should not be emptyname
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument string
is null
IllegalEmptyArgumentException
- if the given argument string
is emptypublic static <T extends Map<?,?>> void notEmpty(boolean condition, @Nonnull T map, @Nullable String name)
We recommend to use the overloaded method Check.notEmpty(Collection, String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedmap
- a map which should not be emptyname
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument map
is null
IllegalEmptyArgumentException
- if the given argument map
is emptypublic static <T extends Collection<?>> void notEmpty(boolean condition, @Nonnull T collection, @Nullable String name)
The following example describes how to use it.
@ArgumentsChecked public setCollection(Collection<String> collection) { this.collection = Check.notEmpty(collection, "collection"); }
condition
- condition must be true
^ so that the check will be performedcollection
- a collection which should not be emptyname
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument collection
is null
IllegalEmptyArgumentException
- if the given argument collection
is emptypublic static <T> void notEmpty(boolean condition, @Nonnull T[] array)
We recommend to use the overloaded method Check.notEmpty(Object[], String)
and pass as second argument
the name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedarray
- a map which should not be empty
IllegalNullArgumentException
- if the given argument array
is null
IllegalEmptyArgumentException
- if the given argument array
is emptypublic static <T> void notEmpty(boolean condition, @Nonnull T[] array, @Nullable String name)
condition
- condition must be true
^ so that the check will be performedarray
- a map which should not be emptyname
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument array
is null
IllegalEmptyArgumentException
- if the given argument array
is emptypublic static void notNaN(boolean condition, double value)
We recommend to use the overloaded method Check.notNaN(double, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedvalue
- value which should not be NaN
IllegalNaNArgumentException
- if the given argument value
is NaNDouble.NaN
public static void notNaN(boolean condition, double value, @Nullable String name)
condition
- condition must be true
^ so that the check will be performedvalue
- value which should not be NaNname
- name of object reference (in source code)
IllegalNaNArgumentException
- if the given argument value
is NaNDouble.NaN
public static void notNaN(boolean condition, float value)
We recommend to use the overloaded method Check.notNaN(float, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedvalue
- value which should not be NaN
IllegalNaNArgumentException
- if the given argument value
is NaNFloat.NaN
public static void notNaN(boolean condition, float value, @Nullable String name)
condition
- condition must be true
^ so that the check will be performedvalue
- value which should not be NaNname
- name of object reference (in source code)
IllegalNaNArgumentException
- if the given argument value
is NaNFloat.NaN
public static void notNegative(boolean condition, @Nonnull int value)
0
.
We recommend to use the overloaded method Check.notNegative(int, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedvalue
- a number
IllegalNegativeArgumentException
- if the given argument reference
is smaller than 0
public static void notNegative(boolean condition, @Nonnull int value, @Nullable String name)
0
.
condition
- condition must be true
^ so that the check will be performedvalue
- a numbername
- name of the number reference (in source code)
IllegalNullArgumentException
- if the given argument reference
is smaller than 0
public static <T> void notNull(boolean condition, @Nonnull T reference)
null
.
We recommend to use the overloaded method Check.notNull(Object, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedreference
- an object reference
IllegalNullArgumentException
- if the given argument reference
is null
public static <T> void notNull(boolean condition, @Nonnull T reference, @Nullable String name)
null
.
condition
- condition must be true
^ so that the check will be performedreference
- an object referencename
- name of object reference (in source code)
IllegalNullArgumentException
- if the given argument reference
is null
public static void notPositive(boolean condition, @Nonnull int value)
0
.
We recommend to use the overloaded method Check.notPositive(int, String)
and pass as second argument the
name of the parameter to enhance the exception message.
condition
- condition must be true
^ so that the check will be performedvalue
- a number
IllegalPositiveArgumentException
- if the given argument reference
is smaller than 0
public static void notPositive(boolean condition, @Nonnull int value, @Nullable String name)
0
.
condition
- condition must be true
^ so that the check will be performedvalue
- a numbername
- name of the number reference (in source code)
IllegalNullArgumentException
- if the given argument reference
is smaller than 0
public static void positionIndex(boolean condition, int index, int size)
condition
- condition must be true
^ so that the check will be performedindex
- index of an array, list or stringsize
- size of an array list or string
IllegalPositionIndexException
- if the index is not a valid position index within an array, list or string of size sizepublic static void range(boolean condition, @Nonnegative int start, @Nonnegative int end, @Nonnegative int size)
true
:
condition
- condition must be true
^ so that the check will be performedstart
- the start value of the range (must be a positive integer or 0)end
- the end value of the range (must be a positive integer or 0)size
- the size value of the range (must be a positive integer or 0)
IllegalRangeException
- if the given arguments do not form a valid rangepublic static void stateIsTrue(boolean condition, boolean expression)
true
.
We recommend to use the overloaded method Check.stateIsTrue(boolean, String)
and pass as second argument
the name of the parameter to enhance the exception message. A better way is to create specific exceptions (with a
good wording) for your case and to use the overloaded method Check.stateIsTrue(boolean, Class)
and pass
as second argument your exception.
condition
- condition must be true
^ so that the check will be performedexpression
- an expression that must be true to indicate a valid state
IllegalStateOfArgumentException
- if the given arguments caused an invalid statepublic static void stateIsTrue(boolean condition, boolean expression, Class<? extends RuntimeException> clazz)
true
and allows to specify the class of exception which is thrown in case
the state is not true
.
condition
- condition must be true
^ so that the check will be performedexpression
- an expression that must be true
to indicate a valid stateclazz
- an subclass of RuntimeException
which will be thrown if the given state is not valid
clazz
- a new instance of clazz
if the given arguments caused an invalid state
RuntimeInstantiationException
- Attention: Be aware, that a RuntimeInstantiationException
can be thrown when
the given clazz
cannot be instantiatedpublic static void stateIsTrue(boolean condition, boolean expression, @Nonnull String description)
true
.
condition
- condition must be true
^ so that the check will be performedexpression
- an expression that must be true
to indicate a valid statedescription
- will be used in the error message to describe why the arguments caused an invalid state
IllegalStateOfArgumentException
- if the given arguments caused an invalid statepublic static void stateIsTrue(boolean condition, boolean expression, @Nonnull String descriptionTemplate, Object... descriptionTemplateArgs)
true
condition
- condition must be true
^ so that the check will be performedexpression
- an expression that must be true
to indicate a valid statedescriptionTemplate
- format string template that explains why the state is invaliddescriptionTemplateArgs
- format string template arguments to explain why the state is invalid
IllegalStateOfArgumentException
- if the given arguments caused an invalid state
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |