net.sf.qualitycheck
Class ConditionalCheck

java.lang.Object
  extended by net.sf.qualitycheck.ConditionalCheck

public final class ConditionalCheck
extends Object

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 list) { ConditionalCheck.isNumeric( numericArgument != null, numericArgument, "numericArgument" ); ConditionalCheck.isNumeric( list != null, list, "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.

Author:
Dominik Seichter

Method Summary
static
<T> void
contains(boolean condition, Collection<T> haystack, T needle)
          Ensures that an element needle is contained in a collection haystack.
static
<T> void
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
<T> T
equals(boolean condition, T expected, T check)
          Ensures that a passed object is equal to another object.
static
<T> void
equals(boolean condition, T expect, T check, String msg)
          Ensures that a passed object is equal to another object.
static
<T extends Comparable<T>>
void
greaterOrEqualThan(boolean condition, T expected, T check)
          Ensures that a passed Comparable is greater than or equal to Comparable.
static
<T extends Comparable<T>>
void
greaterOrEqualThan(boolean condition, T expected, T check, String message)
          Ensures that a passed Comparable is greater than or equal to another Comparable.
static
<T extends Comparable<T>>
void
greaterThan(boolean condition, T expected, T check)
          Ensures that a passed Comparable is greater to another Comparable.
static
<T extends Comparable<T>>
void
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
<T> T
instanceOf(boolean condition, Class<?> type, Object obj)
          Ensures that a passed argument is a member of a specific type.
static
<T> T
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
<T extends Number>
void
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
<T extends Number>
void
isNumber(boolean condition, String value, String name, Class<T> type)
          Ensures that a String argument is a number.
static
<T extends CharSequence>
void
isNumeric(boolean condition, T value)
          Ensures that a readable sequence of char values is numeric.
static
<T extends CharSequence>
void
isNumeric(boolean condition, T value, String name)
          Ensures that a readable sequence of char values is numeric.
static
<T extends Comparable<T>>
void
lesserThan(boolean condition, T expected, T check)
          Ensures that a passed Comparable is less than another Comparable.
static
<T extends Comparable<T>>
void
lesserThan(boolean condition, T expected, T check, String message)
          Ensures that a passed Comparable is less than another Comparable.
static
<T extends CharSequence>
void
matchesPattern(boolean condition, Pattern pattern, T chars)
          Ensures that a readable sequence of char values matches a specified pattern.
static
<T extends CharSequence>
void
matchesPattern(boolean condition, Pattern pattern, T chars, String name)
          Ensures that a readable sequence of char values matches a specified pattern.
static
<T extends Iterable<?>>
void
noNullElements(boolean condition, T iterable)
          Ensures that an iterable reference is neither null nor contains any elements that are null.
static
<T> void
noNullElements(boolean condition, T[] array)
          Ensures that an array does not contain null.
static
<T> void
noNullElements(boolean condition, T[] array, String name)
          Ensures that an array does not contain null.
static
<T extends Iterable<?>>
void
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
<T extends CharSequence>
void
notEmpty(boolean condition, T chars)
          Ensures that a passed string as a parameter of the calling method is not empty.
static
<T extends Collection<?>>
void
notEmpty(boolean condition, T collection)
          Ensures that a passed collection as a parameter of the calling method is not empty.
static
<T extends Map<?,?>>
void
notEmpty(boolean condition, T map)
          Ensures that a passed map as a parameter of the calling method is not empty.
static
<T> void
notEmpty(boolean condition, T[] array)
          Ensures that a passed map as a parameter of the calling method is not empty.
static
<T> void
notEmpty(boolean condition, T[] array, String name)
          Ensures that a passed map as a parameter of the calling method is not empty.
static
<T> void
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
<T extends CharSequence>
void
notEmpty(boolean condition, T chars, String name)
          Ensures that a passed string as a parameter of the calling method is not empty.
static
<T extends Collection<?>>
void
notEmpty(boolean condition, T collection, String name)
          Ensures that a passed collection as a parameter of the calling method is not empty.
static
<T extends Map<?,?>>
void
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
<T> void
notNull(boolean condition, T reference)
          Ensures that an object reference passed as a parameter to the calling method is not null.
static
<T> void
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

contains

public static <T> void contains(boolean condition,
                                @Nonnull
                                Collection<T> haystack,
                                @Nonnull
                                T needle)
Ensures that an element 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.

Parameters:
condition - condition must be true^ so that the check will be performed
haystack - A collection which must contain needle
needle - An object that must be contained into a collection.
Throws:
IllegalArgumentNotContainedException - if the passed needle can not be found in haystack

contains

public static <T> void contains(boolean condition,
                                @Nonnull
                                Collection<T> haystack,
                                @Nonnull
                                T needle,
                                @Nonnull
                                String name)
Ensures that an element 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.

Parameters:
condition - condition must be true^ so that the check will be performed
haystack - A collection which must contain needle
needle - An object that must be contained into a collection.
name - name of argument of needle
Throws:
IllegalArgumentNotContainedException - if the passed needle can not be found in haystack

equals

public static void equals(boolean condition,
                          @Nonnull
                          boolean expected,
                          @Nonnull
                          boolean check)
Ensures that a passed boolean is equal to another boolean. The comparison is made using 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - boolean to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          boolean expected,
                          @Nonnull
                          boolean check,
                          String message)
Ensures that a passed boolean is equal to another boolean. The comparison is made using expected != check.

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - boolean to be checked
message - an error message describing why the booleans must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          byte expected,
                          @Nonnull
                          byte check)
Ensures that a passed byte is equal to another byte. The comparison is made using 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - byte to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          byte expected,
                          @Nonnull
                          byte check,
                          String message)
Ensures that a passed byte is equal to another byte. The comparison is made using expected != check.

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - byte to be checked
message - an error message describing why the bytes must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          char expected,
                          @Nonnull
                          char check)
Ensures that a passed char is equal to another char. The comparison is made using 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - char to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          char expected,
                          @Nonnull
                          char check,
                          String message)
Ensures that a passed char is equal to another char. The comparison is made using expected != check.

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - char to be checked
message - an error message describing why the chars must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          int expected,
                          @Nonnull
                          int check)
Ensures that a passed intH is equal to another int. The comparison is made using 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - int to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          int expected,
                          @Nonnull
                          int check,
                          String message)
Ensures that a passed int is equal to another int. The comparison is made using expected != check.

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - int to be checked
message - an error message describing why the ints must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          long expected,
                          @Nonnull
                          long check)
Ensures that a passed long is equal to another long. The comparison is made using 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - long to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          long expected,
                          @Nonnull
                          long check,
                          String message)
Ensures that a passed long is equal to another long. The comparison is made using expected != check.

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - long to be checked
message - an error message describing why the longs must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          short expected,
                          @Nonnull
                          short check)
Ensures that a passed short is equal to another short. The comparison is made using 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - short to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static void equals(boolean condition,
                          @Nonnull
                          short expected,
                          @Nonnull
                          short check,
                          String message)
Ensures that a passed short is equal to another short. The comparison is made using expected != check.

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - short to be checked
message - an error message describing why the shorts must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static <T> T equals(boolean condition,
                           @Nonnull
                           T expected,
                           @Nonnull
                           T check)
Ensures that a passed object is equal to another object. The comparison is made using a call to expected.equals(check) .

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - Object to be checked
Throws:
IllegalNotEqualException - if both argument values are not equal

equals

public static <T> void equals(boolean condition,
                              @Nonnull
                              T expect,
                              @Nonnull
                              T check,
                              String msg)
Ensures that a passed object is equal to another object. The comparison is made using a call to expected.equals(check) .

The condition must evaluate to true so that the check is executed.

Parameters:
condition - condition must be true^ so that the check will be performed
expect - expected value
check - object to be checked
msg - an error message describing why the objects must equal (will be passed to IllegalNotEqualException)
Throws:
IllegalNotEqualException - if both argument values are not equal

greaterOrEqualThan

public static <T extends Comparable<T>> void greaterOrEqualThan(boolean condition,
                                                                @Nonnull
                                                                T expected,
                                                                @Nonnull
                                                                T check)
Ensures that a passed 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.

Parameters:
condition - condition must be true so that the check will be performed
expected - Expected value
check - Comparable to be checked
Throws:
IllegalNotGreaterOrEqualThanException - if the argument value check is not greater or equal than value expected when using method compareTo

greaterOrEqualThan

public static <T extends Comparable<T>> void greaterOrEqualThan(boolean condition,
                                                                @Nonnull
                                                                T expected,
                                                                @Nonnull
                                                                T check,
                                                                String message)
Ensures that a passed 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.

Parameters:
condition - condition must be true so that the check will be performed
expected - Expected value
check - Comparable to be checked
message - an error message describing why the comparable must be greater than or equal a value (will be passed to IllegalNotGreaterOrEqualThanException)
Throws:
IllegalNotGreaterOrEqualThanException - if the argument value check is not greater than or equal to value expected when using method compareTo

greaterThan

public static <T extends Comparable<T>> void greaterThan(boolean condition,
                                                         @Nonnull
                                                         T expected,
                                                         @Nonnull
                                                         T check)
Ensures that a passed 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - Comparable to be checked
Throws:
IllegalNotGreaterThanException - if the argument value check is not greater than value expected when using method compareTo

greaterThan

public static <T extends Comparable<T>> void greaterThan(boolean condition,
                                                         @Nonnull
                                                         T expected,
                                                         @Nonnull
                                                         T check,
                                                         String message)
Ensures that a passed 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - Comparable to be checked
message - an error message describing why the comparable must be greater than a value (will be passed to IllegalNotGreaterThanException)
Throws:
IllegalNotGreaterThanException - if the argument value check is not greater than value expected when using method compareTo

hasAnnotation

public static void hasAnnotation(boolean condition,
                                 @Nonnull
                                 Class<?> clazz,
                                 @Nonnull
                                 Class<? extends Annotation> annotation)
Ensures that a passed class has an annotation of a specific type

Parameters:
condition - condition must be true^ so that the check will be performed
clazz - the class that must have a required annotation
annotation - the type of annotation that is required on the class
Throws:
IllegalMissingAnnotationException - if the passed annotation is not annotated at the given class

instanceOf

public static <T> T instanceOf(boolean condition,
                               @Nonnull
                               Class<?> type,
                               @Nonnull
                               Object obj)
Ensures that a passed argument is a member of a specific type.

Parameters:
condition - condition must be true^ so that the check will be performed
type - class that the given object is a member of
obj - the object reference that should be a member of a specific type
Throws:
IllegalInstanceOfArgumentException - if the given argument obj is not a member of type

instanceOf

public static <T> T instanceOf(boolean condition,
                               @Nonnull
                               Class<?> type,
                               @Nonnull
                               Object obj,
                               @Nullable
                               String name)
Ensures that a passed argument is a member of a specific type.

Parameters:
condition - condition must be true^ so that the check will be performed
type - class that the given object is a member of
obj - the object reference that should be a member of a specific type
name - name of object reference (in source code)
Throws:
IllegalInstanceOfArgumentException - if the given argument obj is not a member of type

isNull

public static void isNull(boolean condition,
                          @Nullable
                          Object reference)
Ensures that a given argument is 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.

Parameters:
condition - condition must be true^ so that the check will be performed
reference - reference which must be null
Throws:
IllegalNotNullArgumentException - if the given argument reference is not null

isNull

public static void isNull(boolean condition,
                          @Nullable
                          Object reference,
                          @Nullable
                          String name)
Ensures that a given argument is 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.

Parameters:
condition - condition must be true^ so that the check will be performed
reference - reference which must be null.
name - name of object reference (in source code)
Throws:
IllegalNotNullArgumentException - if the given argument reference is not null

isNumber

public static void isNumber(boolean condition,
                            @Nonnull
                            String value)
Ensures that a String argument is a number.

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which must be a number
Throws:
IllegalNumberArgumentException - if the given argument value is not a number

isNumber

public static <T extends Number> void isNumber(boolean condition,
                                               @Nonnull
                                               String value,
                                               @Nonnull
                                               Class<T> type)
Ensures that a String argument is a number. This overload supports all subclasses of Number. The number is first converted to a BigInteger

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which must be a number
type - requested return value type, must be a subclass of Number, i.e. one of BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
Throws:
IllegalNumberArgumentException - if the given argument value is no number

isNumber

public static void isNumber(boolean condition,
                            @Nonnull
                            String value,
                            @Nullable
                            String name)
Ensures that a string argument is a number according to Integer.parseInt

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which must be a number
name - name of object reference (in source code)
Throws:
IllegalNumberArgumentException - if the given argument value is no number

isNumber

public static <T extends Number> void isNumber(boolean condition,
                                               @Nonnull
                                               String value,
                                               @Nullable
                                               String name,
                                               @Nonnull
                                               Class<T> type)
Ensures that a String argument is a number. This overload supports all subclasses of 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.

Parameters:
condition - condition must be true^ so that the check will be performed
value - 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
Throws:
IllegalNumberArgumentException - if the given argument value is no number

isNumeric

public static <T extends CharSequence> void isNumeric(boolean condition,
                                                      @Nonnull
                                                      T value)
Ensures that a readable sequence of 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.

Parameters:
condition - condition must be true^ so that the check will be performed
value - a readable sequence of char values which must be a number
Throws:
IllegalNumberArgumentException - if the given argument value is no number

isNumeric

public static <T extends CharSequence> void isNumeric(boolean condition,
                                                      @Nonnull
                                                      T value,
                                                      @Nullable
                                                      String name)
Ensures that a readable sequence of 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).

Parameters:
condition - condition must be true^ so that the check will be performed
value - a readable sequence of char values which must be a number
name - name of object reference (in source code)
Throws:
IllegalNumberArgumentException - if the given argument value is no number

lesserThan

public static <T extends Comparable<T>> void lesserThan(boolean condition,
                                                        @Nonnull
                                                        T expected,
                                                        @Nonnull
                                                        T check)
Ensures that a passed Comparable is less than another Comparable. The comparison is made using expected.compareTo(check) <= 0.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - Comparable to be checked
Throws:
IllegalNotLesserThanException - if the argument value check is not lesser than value expected when using method compareTo

lesserThan

public static <T extends Comparable<T>> void lesserThan(boolean condition,
                                                        @Nonnull
                                                        T expected,
                                                        @Nonnull
                                                        T check,
                                                        String message)
Ensures that a passed Comparable is less than another Comparable. The comparison is made using expected.compareTo(check) <= 0.

Parameters:
condition - condition must be true^ so that the check will be performed
expected - Expected value
check - Comparable to be checked
message - an error message describing why the comparables must be less than a value (will be passed to IllegalNotLessThanException)
Throws:
IllegalNotLesserThanException - if the argument value check is not lesser than value expected when using method compareTo

matchesPattern

public static <T extends CharSequence> void matchesPattern(boolean condition,
                                                           @Nonnull
                                                           Pattern pattern,
                                                           @Nonnull
                                                           T chars)
Ensures that a readable sequence of 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.

Parameters:
condition - condition must be true^ so that the check will be performed
pattern - pattern, that the chars must correspond to
chars - a readable sequence of char values which should match the given pattern
Throws:
IllegalNullArgumentException - if the given argument chars is null
IllegalPatternArgumentException - if the given chars that does not match the pattern

matchesPattern

public static <T extends CharSequence> void matchesPattern(boolean condition,
                                                           @Nonnull
                                                           Pattern pattern,
                                                           @Nonnull
                                                           T chars,
                                                           @Nullable
                                                           String name)
Ensures that a readable sequence of char values matches a specified pattern. If the given character sequence does not match against the passed pattern, an IllegalPatternArgumentException will be thrown.

Parameters:
condition - condition must be true^ so that the check will be performed
pattern - pattern, that the chars must correspond to
chars - a readable sequence of char values which should match the given pattern
name - name of object reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument chars is null
IllegalPatternArgumentException - if the given chars that does not match the pattern

noNullElements

public static <T extends Iterable<?>> void noNullElements(boolean condition,
                                                          @Nonnull
                                                          T iterable)
Ensures that an iterable reference is neither 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.

Parameters:
condition - condition must be true^ so that the check will be performed
iterable - the iterable reference which should not contain null
Throws:
IllegalNullElementsException - if the given argument iterable contains elements that are null

noNullElements

public static <T extends Iterable<?>> void noNullElements(boolean condition,
                                                          @Nonnull
                                                          T iterable,
                                                          String name)
Ensures that an iterable reference is neither null nor contains any elements that are null.

Parameters:
condition - condition must be true^ so that the check will be performed
iterable - the iterable reference which should not contain null
name - name of object reference (in source code)
Throws:
IllegalNullElementsException - if the given argument iterable contains elements that are null

noNullElements

public static <T> void noNullElements(boolean condition,
                                      @Nonnull
                                      T[] array)
Ensures that an array does not contain 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.

Parameters:
condition - condition must be true^ so that the check will be performed
array - reference to an array
Throws:
IllegalNullElementsException - if the given argument array contains null

noNullElements

public static <T> void noNullElements(boolean condition,
                                      @Nonnull
                                      T[] array,
                                      @Nullable
                                      String name)
Ensures that an array does not contain null.

Parameters:
condition - condition must be true^ so that the check will be performed
array - reference to an array
name - name of object reference (in source code)
Throws:
IllegalNullElementsException - if the given argument array contains null

notEmpty

public 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.

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.

Parameters:
condition - condition must be true^ so that the check will be performed
expression - the result of the expression to verify the emptiness of a reference (true means empty, false means not empty)
Throws:
IllegalNullArgumentException - if the given argument reference is null
IllegalEmptyArgumentException - if the given argument reference is empty

notEmpty

public static void notEmpty(boolean condition,
                            boolean expression,
                            @Nullable
                            String name)
Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the emptiness.

Parameters:
condition - condition must be true^ so that the check will be performed
expression - 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)
Throws:
IllegalEmptyArgumentException - if the given argument reference is empty

notEmpty

public static <T extends CharSequence> void notEmpty(boolean condition,
                                                     @Nonnull
                                                     T chars)
Ensures that a passed string as a parameter of the calling method is not empty.

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.

Parameters:
condition - condition must be true^ so that the check will be performed
chars - a readable sequence of char values which should not be empty
Throws:
IllegalNullArgumentException - if the given argument reference is null
IllegalEmptyArgumentException - if the given argument reference is empty

notEmpty

public static <T extends Collection<?>> void notEmpty(boolean condition,
                                                      @Nonnull
                                                      T collection)
Ensures that a passed collection as a parameter of the calling method is not empty.

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.

Parameters:
condition - condition must be true^ so that the check will be performed
collection - a collection which should not be empty
Throws:
IllegalNullArgumentException - if the given argument collection is null
IllegalEmptyArgumentException - if the given argument collection is empty

notEmpty

public static <T extends Map<?,?>> void notEmpty(boolean condition,
                                                 @Nonnull
                                                 T map)
Ensures that a passed map as a parameter of the calling method is not empty.

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.

Parameters:
condition - condition must be true^ so that the check will be performed
map - a map which should not be empty
Throws:
IllegalNullArgumentException - if the given argument map is null
IllegalEmptyArgumentException - if the given argument map is empty

notEmpty

public static <T> void notEmpty(boolean condition,
                                @Nonnull
                                T reference,
                                boolean expression,
                                @Nullable
                                String name)
Ensures that an object reference passed as a parameter to the calling method is not empty. The passed boolean value is the result of checking whether the reference is empty or not.

The following example describes how to use it.

 @ArgumentsChecked
 public setText(String text) {
        ConditionalCheck.notEmpty(true, text, text.isEmpty(), "text");
        this.text = text;
 }
 

Parameters:
condition - condition must be true^ so that the check will be performed
reference - an object reference which should not be empty
expression - 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)
Throws:
IllegalNullArgumentException - if the given argument reference is null
IllegalEmptyArgumentException - if the given argument reference is empty

notEmpty

public static <T extends CharSequence> void notEmpty(boolean condition,
                                                     @Nonnull
                                                     T chars,
                                                     @Nullable
                                                     String name)
Ensures that a passed string as a parameter of the calling method is not empty.

The following example describes how to use it.

 @ArgumentsChecked
 public setText(String text) {
        this.text = Check.notEmpty(text, "text");
 }
 

Parameters:
condition - condition must be true^ so that the check will be performed
chars - a readable sequence of char values which should not be empty
name - name of object reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument string is null
IllegalEmptyArgumentException - if the given argument string is empty

notEmpty

public static <T extends Map<?,?>> void notEmpty(boolean condition,
                                                 @Nonnull
                                                 T map,
                                                 @Nullable
                                                 String name)
Ensures that a passed map as a parameter of the calling method is not empty.

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.

Parameters:
condition - condition must be true^ so that the check will be performed
map - a map which should not be empty
name - name of object reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument map is null
IllegalEmptyArgumentException - if the given argument map is empty

notEmpty

public static <T extends Collection<?>> void notEmpty(boolean condition,
                                                      @Nonnull
                                                      T collection,
                                                      @Nullable
                                                      String name)
Ensures that a passed collection as a parameter of the calling method is not empty.

The following example describes how to use it.

 @ArgumentsChecked
 public setCollection(Collection<String> collection) {
        this.collection = Check.notEmpty(collection, "collection");
 }
 

Parameters:
condition - condition must be true^ so that the check will be performed
collection - a collection which should not be empty
name - name of object reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument collection is null
IllegalEmptyArgumentException - if the given argument collection is empty

notEmpty

public static <T> void notEmpty(boolean condition,
                                @Nonnull
                                T[] array)
Ensures that a passed map as a parameter of the calling method is not empty.

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.

Parameters:
condition - condition must be true^ so that the check will be performed
array - a map which should not be empty
Throws:
IllegalNullArgumentException - if the given argument array is null
IllegalEmptyArgumentException - if the given argument array is empty

notEmpty

public static <T> void notEmpty(boolean condition,
                                @Nonnull
                                T[] array,
                                @Nullable
                                String name)
Ensures that a passed map as a parameter of the calling method is not empty.

Parameters:
condition - condition must be true^ so that the check will be performed
array - a map which should not be empty
name - name of object reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument array is null
IllegalEmptyArgumentException - if the given argument array is empty

notNaN

public static void notNaN(boolean condition,
                          double value)
Ensures that a double argument is not NaN (not a number).

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.

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which should not be NaN
Throws:
IllegalNaNArgumentException - if the given argument value is NaN
See Also:
Double.NaN

notNaN

public static void notNaN(boolean condition,
                          double value,
                          @Nullable
                          String name)
Ensures that a double argument is not NaN (not a number).

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which should not be NaN
name - name of object reference (in source code)
Throws:
IllegalNaNArgumentException - if the given argument value is NaN
See Also:
Double.NaN

notNaN

public static void notNaN(boolean condition,
                          float value)
Ensures that a double argument is not NaN (not a number).

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.

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which should not be NaN
Throws:
IllegalNaNArgumentException - if the given argument value is NaN
See Also:
Float.NaN

notNaN

public static void notNaN(boolean condition,
                          float value,
                          @Nullable
                          String name)
Ensures that a double argument is not NaN (not a number).

Parameters:
condition - condition must be true^ so that the check will be performed
value - value which should not be NaN
name - name of object reference (in source code)
Throws:
IllegalNaNArgumentException - if the given argument value is NaN
See Also:
Float.NaN

notNegative

public static void notNegative(boolean condition,
                               @Nonnull
                               int value)
Ensures that an integer reference passed as a parameter to the calling method is not smaller than 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.

Parameters:
condition - condition must be true^ so that the check will be performed
value - a number
Throws:
IllegalNegativeArgumentException - if the given argument reference is smaller than 0

notNegative

public static void notNegative(boolean condition,
                               @Nonnull
                               int value,
                               @Nullable
                               String name)
Ensures that an integer reference passed as a parameter to the calling method is not smaller than 0.

Parameters:
condition - condition must be true^ so that the check will be performed
value - a number
name - name of the number reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument reference is smaller than 0

notNull

public static <T> void notNull(boolean condition,
                               @Nonnull
                               T reference)
Ensures that an object reference passed as a parameter to the calling method is not 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.

Parameters:
condition - condition must be true^ so that the check will be performed
reference - an object reference
Throws:
IllegalNullArgumentException - if the given argument reference is null

notNull

public static <T> void notNull(boolean condition,
                               @Nonnull
                               T reference,
                               @Nullable
                               String name)
Ensures that an object reference passed as a parameter to the calling method is not null.

Parameters:
condition - condition must be true^ so that the check will be performed
reference - an object reference
name - name of object reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument reference is null

notPositive

public static void notPositive(boolean condition,
                               @Nonnull
                               int value)
Ensures that an integer reference passed as a parameter to the calling method is not greater than 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.

Parameters:
condition - condition must be true^ so that the check will be performed
value - a number
Throws:
IllegalPositiveArgumentException - if the given argument reference is smaller than 0

notPositive

public static void notPositive(boolean condition,
                               @Nonnull
                               int value,
                               @Nullable
                               String name)
Ensures that an integer reference passed as a parameter to the calling method is not greater than 0.

Parameters:
condition - condition must be true^ so that the check will be performed
value - a number
name - name of the number reference (in source code)
Throws:
IllegalNullArgumentException - if the given argument reference is smaller than 0

positionIndex

public 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 ...

Parameters:
condition - condition must be true^ so that the check will be performed
index - index of an array, list or string
size - size of an array list or string
Throws:
IllegalPositionIndexException - if the index is not a valid position index within an array, list or string of size size

range

public static void range(boolean condition,
                         @Nonnegative
                         int start,
                         @Nonnegative
                         int end,
                         @Nonnegative
                         int size)
Ensures that the given arguments are a valid range. A range (start, end, size) is valid if the following conditions are true:

Parameters:
condition - condition must be true^ so that the check will be performed
start - 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)
Throws:
IllegalRangeException - if the given arguments do not form a valid range

stateIsTrue

public static void stateIsTrue(boolean condition,
                               boolean expression)
Ensures that a given state is 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expression - an expression that must be true to indicate a valid state
Throws:
IllegalStateOfArgumentException - if the given arguments caused an invalid state

stateIsTrue

public 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.

Parameters:
condition - condition must be true^ so that the check will be performed
expression - an expression that must be true to indicate a valid state
clazz - an subclass of RuntimeException which will be thrown if the given state is not valid
Throws:
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 instantiated

stateIsTrue

public static void stateIsTrue(boolean condition,
                               boolean expression,
                               @Nonnull
                               String description)
Ensures that a given state is true.

Parameters:
condition - condition must be true^ so that the check will be performed
expression - an expression that must be true to indicate a valid state
description - will be used in the error message to describe why the arguments caused an invalid state
Throws:
IllegalStateOfArgumentException - if the given arguments caused an invalid state

stateIsTrue

public static void stateIsTrue(boolean condition,
                               boolean expression,
                               @Nonnull
                               String descriptionTemplate,
                               Object... descriptionTemplateArgs)
Ensures that a given state is true

Parameters:
condition - condition must be true^ so that the check will be performed
expression - an expression that must be true to indicate a valid state
descriptionTemplate - format string template that explains why the state is invalid
descriptionTemplateArgs - format string template arguments to explain why the state is invalid
Throws:
IllegalStateOfArgumentException - if the given arguments caused an invalid state


Copyright © 2012-2013. All Rights Reserved.