Class KiwiPreconditions
Preconditions, but with a lovely
Kiwi flavor to them. That class has good documentation, so go read it if you need more information on the
intent and general usage.
If you're looking for preconditions related to validating arguments using Jakarta Beans Validation, they
are in KiwiValidations.
- Implementation Note:
- Several methods in this class use Lombok
SneakyThrowsso that they do not need to declare that they throwExceptions of type T, for the case that T is a checked exception. Read more details about how this works inSneakyThrows. Most notably, this should give you more insight into how the JVM (versus Java the language) actually work: "The JVM does not check for the consistency of the checked exception system; javac does, and this annotation lets you opt out of its mechanism."
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Throwable>
voidcheckArgument(boolean expression, Class<T> exceptionType) Ensures the truth of an expression involving one or more parameters to the calling method.static <T extends Throwable>
voidcheckArgument(boolean expression, Class<T> exceptionType, String errorMessage) Ensures the truth of an expression involving one or more parameters to the calling method.static <T extends Throwable>
voidcheckArgument(boolean expression, Class<T> exceptionType, String errorMessageTemplate, Object... errorMessageArgs) Ensures the truth of an expression involving one or more parameters to the calling method.static voidcheckArgumentContainsOnlyNotBlank(Collection<String> collection) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.static voidcheckArgumentContainsOnlyNotBlank(Collection<String> collection, String errorMessage) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.static voidcheckArgumentContainsOnlyNotBlank(Collection<String> collection, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.static <T> voidcheckArgumentContainsOnlyNotNull(Collection<T> collection) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.static <T> voidcheckArgumentContainsOnlyNotNull(Collection<T> collection, String errorMessage) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.static <T> voidcheckArgumentContainsOnlyNotNull(Collection<T> collection, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.static <T> voidcheckArgumentInstanceOf(T argument, Class<?> requiredType) Ensures the argument has the expected type.static <T> voidcheckArgumentInstanceOf(T argument, Class<?> requiredType, String errorMessage) Ensures the argument has the expected type.static <T> voidcheckArgumentInstanceOf(T argument, Class<?> requiredType, String errorMessageTemplate, Object... errorMessageArgs) Ensures the argument has the expected type.static voidcheckArgumentIsBlank(String string) Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing anIllegalArgumentExceptionif it is not null, empty, or blank.static voidcheckArgumentIsBlank(String string, String errorMessage) Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing anIllegalArgumentExceptionif it is not null, empty, or blank.static voidcheckArgumentIsBlank(String string, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing anIllegalArgumentExceptionif it is not null, empty, or blank.static <T> voidcheckArgumentIsNull(T reference) Ensures that an object reference passed as a parameter to the calling method is null, throwing anIllegalArgumentExceptionif not null.static <T> voidcheckArgumentIsNull(T reference, String errorMessage) Ensures that an object reference passed as a parameter to the calling method is null, throwing anIllegalArgumentExceptionif not null.static <T> voidcheckArgumentIsNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is null, throwing anIllegalArgumentExceptionif not null.static voidcheckArgumentNotBlank(String string) Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing anIllegalArgumentExceptionif it is null, empty, or blank.static voidcheckArgumentNotBlank(String string, String errorMessage) Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing anIllegalArgumentExceptionif it is null, empty, or blank.static voidcheckArgumentNotBlank(String string, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing anIllegalArgumentExceptionif it is null, empty, or blank.static <T> voidcheckArgumentNotEmpty(Collection<T> collection) Ensures that the collection passed as a parameter to the calling method is not null or empty.static <T> voidcheckArgumentNotEmpty(Collection<T> collection, String errorMessage) Ensures that the collection passed as a parameter to the calling method is not null or empty.static <T> voidcheckArgumentNotEmpty(Collection<T> collection, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the collection passed as a parameter to the calling method is not null or empty.static <K,V> void checkArgumentNotEmpty(Map<K, V> map) Ensures that the map passed as a parameter to the calling method is not null or empty.static <K,V> void checkArgumentNotEmpty(Map<K, V> map, String errorMessage) Ensures that the map passed as a parameter to the calling method is not null or empty.static <K,V> void checkArgumentNotEmpty(Map<K, V> map, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the map passed as a parameter to the calling method is not null or empty.static <T> voidcheckArgumentNotInstanceOf(T argument, Class<?> restrictedType) Ensures the argument type is not the restricted type.static <T> voidcheckArgumentNotInstanceOf(T argument, Class<?> restrictedType, String errorMessage) Ensures the argument type is not the restricted type.static <T> voidcheckArgumentNotInstanceOf(T argument, Class<?> restrictedType, String errorMessageTemplate, Object... errorMessageArgs) Ensures the argument type is not the restricted type.static <T> voidcheckArgumentNotNull(T reference) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null.static <T> voidcheckArgumentNotNull(T reference, String errorMessage) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null.static <T> voidcheckArgumentNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null.static <T> voidcheckEvenItemCount(Collection<T> items) Ensures that a collection of items has an even count, throwing anIllegalArgumentExceptionif items is null or there is an odd number of items.static voidcheckEvenItemCount(IntSupplier countSupplier) Ensures that a collection of items has an even count, throwing anIllegalArgumentExceptionif countSupplier is null or returns an odd number.static <T> voidcheckEvenItemCount(T... items) Ensures that a collection of items has an even count, throwing anIllegalArgumentExceptionif items is null or there is an odd number of items.static <T> voidcheckOnlyOneArgumentIsNull(T first, T second) Ensures that only one of two given arguments is null.static <T> voidcheckOnlyOneArgumentIsNull(T first, T second, String message) Ensures that only one of two given arguments is null.static <T> voidcheckOnlyOneArgumentIsNull(T first, T second, String errorMessageTemplate, Object... errorMessageArgs) Ensures that only one of two given arguments is null.static voidcheckPositive(int value) Ensures intvalueis a positive number (greater than zero).static voidcheckPositive(int value, String errorMessage) Ensures intvalueis a positive number (greater than zero).static voidcheckPositive(int value, String errorMessageTemplate, Object... errorMessageArgs) Ensures intvalueis a positive number (greater than zero).static voidcheckPositive(long value) Ensures longvalueis a positive number (greater than zero).static voidcheckPositive(long value, String errorMessage) Ensures longvalueis a positive number (greater than zero).static voidcheckPositive(long value, String errorMessageTemplate, Object... errorMessageArgs) Ensures longvalueis a positive number (greater than zero).static voidcheckPositiveOrZero(int value) Ensures intvalueis a positive number (greater than zero) or zero.static voidcheckPositiveOrZero(int value, String errorMessage) Ensures intvalueis a positive number (greater than zero) or zero.static voidcheckPositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) Ensures intvalueis a positive number (greater than zero) or zero.static voidcheckPositiveOrZero(long value) Ensures longvalueis a positive number (greater than zero) or zero.static voidcheckPositiveOrZero(long value, String errorMessage) Ensures longvalueis a positive number (greater than zero) or zero.static voidcheckPositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) Ensures longvalueis a positive number (greater than zero) or zero.static voidcheckValidNonZeroPort(int port) Ensures given port is valid (excluding zero), between 1 andMAX_PORT_NUMBER.static voidcheckValidNonZeroPort(int port, String errorMessage) Ensures given port is valid (excluding zero), between 1 andMAX_PORT_NUMBER.static voidcheckValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Ensures given port is valid (excluding zero), between 1 andMAX_PORT_NUMBER.static voidcheckValidPort(int port) Ensures given port is valid, between 0 andMAX_PORT_NUMBER.static voidcheckValidPort(int port, String errorMessage) Ensures given port is valid, between 0 andMAX_PORT_NUMBER.static voidcheckValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Ensures given port is valid, between 0 andMAX_PORT_NUMBER.static StringrequireNotBlank(String value) Ensures that a String passed as a parameter to the calling method is not blank, throwing anIllegalArgumentExceptionif blank or returning the String otherwise.static StringrequireNotBlank(String value, String errorMessage) Ensures that a String passed as a parameter to the calling method is not blank, throwing anIllegalArgumentExceptionif blank or returning the String otherwise.static StringrequireNotBlank(String value, String errorMessageTemplate, Object... errorMessageArgs) Ensures that a String passed as a parameter to the calling method is not blank, throwing anIllegalArgumentExceptionif blank or returning the String otherwise.static <T> TrequireNotNull(T reference) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null or returning the (non-null) reference otherwise.static <T> TrequireNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null or returning the (non-null) reference otherwise.static <T> TrequireNotNullElse(T obj, T defaultObj) Returns the first argument if it is notnull, otherwise the second argument (which must not benull).static <T> TrequireNotNullElseGet(T obj, Supplier<? extends T> supplier) Returns the first argument if it is notnull, otherwise the value supplied by theSupplier, which must not benull.static intrequirePositive(int value) Returns the int value if it is positive, throwing anIllegalStateExceptionif not positive.static intrequirePositive(int value, String errorMessage) Returns the intvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.static intrequirePositive(int value, String errorMessageTemplate, Object... errorMessageArgs) Returns the intvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.static longrequirePositive(long value) Returns the long value if it is positive, throwing anIllegalStateExceptionif not positive.static longrequirePositive(long value, String errorMessage) Returns the longvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.static longrequirePositive(long value, String errorMessageTemplate, Object... errorMessageArgs) Returns the longvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.static intrequirePositiveOrZero(int value) Returns the int value if it is positive or zero, throwing anIllegalStateExceptionif not positive or zero.static intrequirePositiveOrZero(int value, String errorMessage) Returns the intvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.static intrequirePositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) Returns the intvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.static longrequirePositiveOrZero(long value) Returns the long value if it is positive or zero, throwing anIllegalStateExceptionif not positive or zero.static longrequirePositiveOrZero(long value, String errorMessage) Returns the longvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.static longrequirePositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) Returns the longvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.static intrequireValidNonZeroPort(int port) Returns the given port if it is valid (excluding zero)static intrequireValidNonZeroPort(int port, String errorMessage) Returns the given port if it is valid (excluding zero)static intrequireValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Returns the given port if it is valid (excluding zero)static intrequireValidPort(int port) Returns the given port if it is validstatic intrequireValidPort(int port, String errorMessage) Returns the given port if it is validstatic intrequireValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Returns the given port if it is valid
-
Method Details
-
checkArgument
Ensures the truth of an expression involving one or more parameters to the calling method.Throws an
Exceptionof type T ifexpressionis false.- Type Parameters:
T- the type of exception- Parameters:
expression- a boolean expressionexceptionType- the type of exception to be thrown ifexpressionis false- Implementation Note:
- This uses Lombok
SneakyThrowsto throw any checked exceptions without declaring them.
-
checkArgument
public static <T extends Throwable> void checkArgument(boolean expression, Class<T> exceptionType, String errorMessage) Ensures the truth of an expression involving one or more parameters to the calling method.Throws an
Exceptionof type T ifexpressionis false.- Type Parameters:
T- the type of exception- Parameters:
expression- a boolean expressionexceptionType- the type of exception to be thrown ifexpressionis falseerrorMessage- the exception message to use if the check fails- Implementation Note:
- This uses Lombok
SneakyThrowsto throw any checked exceptions without declaring them.
-
checkArgument
public static <T extends Throwable> void checkArgument(boolean expression, Class<T> exceptionType, String errorMessageTemplate, Object... errorMessageArgs) Ensures the truth of an expression involving one or more parameters to the calling method.Throws an
Exceptionof type T ifexpressionis false.- Type Parameters:
T- the type of exception- Parameters:
expression- a boolean expressionexceptionType- the type of exception to be thrown ifexpressionis falseerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).- Throws:
NullPointerException- if the check fails and eithererrorMessageTemplateorerrorMessageArgsis null (don't let this happen)- Implementation Note:
- This uses Lombok
SneakyThrowsto throw any checked exceptions without declaring them.
-
requireNotBlank
Ensures that a String passed as a parameter to the calling method is not blank, throwing anIllegalArgumentExceptionif blank or returning the String otherwise.- Parameters:
value- the String value to check- Returns:
- the given String
- See Also:
-
requireNotBlank
Ensures that a String passed as a parameter to the calling method is not blank, throwing anIllegalArgumentExceptionif blank or returning the String otherwise.- Parameters:
value- the String value to checkerrorMessage- the error message for the exception- Returns:
- the given String
- See Also:
-
requireNotBlank
public static String requireNotBlank(String value, String errorMessageTemplate, Object... errorMessageArgs) Ensures that a String passed as a parameter to the calling method is not blank, throwing anIllegalArgumentExceptionif blank or returning the String otherwise.- Parameters:
value- the String value to checkerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).- Returns:
- the given String
- See Also:
-
requireNotNull
public static <T> T requireNotNull(T reference) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null or returning the (non-null) reference otherwise.- Type Parameters:
T- the type of object- Parameters:
reference- an object reference- Returns:
- the object type
-
requireNotNull
public static <T> T requireNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null or returning the (non-null) reference otherwise.- Type Parameters:
T- the type of object- Parameters:
reference- an object referenceerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).- Returns:
- the object type
-
checkArgumentNotNull
public static <T> void checkArgumentNotNull(T reference) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null.- Type Parameters:
T- the object type- Parameters:
reference- an object reference
-
checkArgumentNotNull
Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null.- Type Parameters:
T- the object type- Parameters:
reference- an object referenceerrorMessage- the error message for the exception
-
checkArgumentNotNull
public static <T> void checkArgumentNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is not null, throwing anIllegalArgumentExceptionif null.- Type Parameters:
T- the object type- Parameters:
reference- an object referenceerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).
-
checkOnlyOneArgumentIsNull
public static <T> void checkOnlyOneArgumentIsNull(T first, T second) Ensures that only one of two given arguments is null. ThrowsIllegalArgumentExceptionif both are null or both are non-null.- Type Parameters:
T- the object type- Parameters:
first- the first argumentsecond- the second argument
-
checkOnlyOneArgumentIsNull
Ensures that only one of two given arguments is null. ThrowsIllegalArgumentExceptionif both are null or both are non-null.- Type Parameters:
T- the object type- Parameters:
first- the first argumentsecond- the second argumentmessage- the error message to use if the check fails
-
checkOnlyOneArgumentIsNull
public static <T> void checkOnlyOneArgumentIsNull(T first, T second, String errorMessageTemplate, Object... errorMessageArgs) Ensures that only one of two given arguments is null. ThrowsIllegalArgumentExceptionif both are null or both are non-null.- Type Parameters:
T- the object type- Parameters:
first- the first argumentsecond- the second argumenterrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).
-
checkArgumentIsNull
public static <T> void checkArgumentIsNull(T reference) Ensures that an object reference passed as a parameter to the calling method is null, throwing anIllegalArgumentExceptionif not null.- Type Parameters:
T- the object type- Parameters:
reference- an object reference
-
checkArgumentIsNull
Ensures that an object reference passed as a parameter to the calling method is null, throwing anIllegalArgumentExceptionif not null.- Type Parameters:
T- the object type- Parameters:
reference- an object referenceerrorMessage- the error message for the exception
-
checkArgumentIsNull
public static <T> void checkArgumentIsNull(T reference, String errorMessageTemplate, Object... errorMessageArgs) Ensures that an object reference passed as a parameter to the calling method is null, throwing anIllegalArgumentExceptionif not null.- Type Parameters:
T- the object type- Parameters:
reference- an object referenceerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkArgumentNotBlank
Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing anIllegalArgumentExceptionif it is null, empty, or blank.- Parameters:
string- a string
-
checkArgumentNotBlank
Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing anIllegalArgumentExceptionif it is null, empty, or blank.- Parameters:
string- a stringerrorMessage- the error message for the exception
-
checkArgumentNotBlank
public static void checkArgumentNotBlank(String string, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the string passed as a parameter to the calling method is not null, empty or blank, throwing anIllegalArgumentExceptionif it is null, empty, or blank.- Parameters:
string- a stringerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).
-
checkArgumentIsBlank
Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing anIllegalArgumentExceptionif it is not null, empty, or blank.- Parameters:
string- a string
-
checkArgumentIsBlank
Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing anIllegalArgumentExceptionif it is not null, empty, or blank.- Parameters:
string- a stringerrorMessage- the error message for the exception
-
checkArgumentIsBlank
public static void checkArgumentIsBlank(String string, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the string passed as a parameter to the calling method is null, empty or blank, throwing anIllegalArgumentExceptionif it is not null, empty, or blank.- Parameters:
string- a stringerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkArgumentNotEmpty
Ensures that the collection passed as a parameter to the calling method is not null or empty. Throws anIllegalArgumentExceptionif the collection is null or empty.- Type Parameters:
T- the type of object in the collection- Parameters:
collection- a collection, possibly null
-
checkArgumentNotEmpty
Ensures that the collection passed as a parameter to the calling method is not null or empty. Throws anIllegalArgumentExceptionif the collection is null or empty.- Type Parameters:
T- the type of object in the collection- Parameters:
collection- a collection, possibly nullerrorMessage- the error message for the exception
-
checkArgumentNotEmpty
public static <T> void checkArgumentNotEmpty(Collection<T> collection, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the collection passed as a parameter to the calling method is not null or empty. Throws anIllegalArgumentExceptionif the collection is null or empty.- Type Parameters:
T- the type of object in the collection- Parameters:
collection- a collection, possibly nullerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkArgumentContainsOnlyNotNull
Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.Throws an
IllegalArgumentExceptionif the collection is null or empty, or if any elements are null.- Type Parameters:
T- the type of object in the collection- Parameters:
collection- a collection, possibly null
-
checkArgumentContainsOnlyNotNull
public static <T> void checkArgumentContainsOnlyNotNull(Collection<T> collection, String errorMessage) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.Throws an
IllegalArgumentExceptionif the collection is null or empty, or if any elements are null.- Type Parameters:
T- the type of object in the collection- Parameters:
collection- a collection, possibly nullerrorMessage- the error message for the exception
-
checkArgumentContainsOnlyNotNull
public static <T> void checkArgumentContainsOnlyNotNull(Collection<T> collection, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are null.Throws an
IllegalArgumentExceptionif the collection is null or empty, or if any elements are null.- Type Parameters:
T- the type of object in the collection- Parameters:
collection- a collection, possibly nullerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkArgumentContainsOnlyNotBlank
Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.Throws an
IllegalArgumentExceptionif the collection is null or empty, or if any elements are blank strings.- Parameters:
collection- a collection, possibly null- Implementation Note:
- uses
StringUtils.isBlank(CharSequence)to check for blank elements
-
checkArgumentContainsOnlyNotBlank
public static void checkArgumentContainsOnlyNotBlank(Collection<String> collection, String errorMessage) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.Throws an
IllegalArgumentExceptionif the collection is null or empty, or if any elements are blank strings.- Parameters:
collection- a collection, possibly nullerrorMessage- the error message for the exception- Implementation Note:
- uses
StringUtils.isBlank(CharSequence)to check for blank elements
-
checkArgumentContainsOnlyNotBlank
public static void checkArgumentContainsOnlyNotBlank(Collection<String> collection, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the collection passed as a parameter to the calling method is not null or empty, and that none of its elements are blank strings.Throws an
IllegalArgumentExceptionif the collection is null or empty, or if any elements are blank strings.- Parameters:
collection- a collection, possibly nullerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).- Implementation Note:
- uses
StringUtils.isBlank(CharSequence)to check for blank elements
-
checkArgumentNotEmpty
Ensures that the map passed as a parameter to the calling method is not null or empty. Throws anIllegalArgumentExceptionif the map is null or empty.- Type Parameters:
K- the type of keys in the mapV- the type of values in the map- Parameters:
map- a map, possibly null
-
checkArgumentNotEmpty
Ensures that the map passed as a parameter to the calling method is not null or empty. Throws anIllegalArgumentExceptionif the map is null or empty.- Type Parameters:
K- the type of keys in the mapV- the type of values in the map- Parameters:
map- a map, possibly nullerrorMessage- the error message for the exception
-
checkArgumentNotEmpty
public static <K,V> void checkArgumentNotEmpty(Map<K, V> map, String errorMessageTemplate, Object... errorMessageArgs) Ensures that the map passed as a parameter to the calling method is not null or empty. Throws anIllegalArgumentExceptionif the map is null or empty.- Type Parameters:
K- the type of keys in the mapV- the type of values in the map- Parameters:
map- a map, possibly nullerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkEvenItemCount
Ensures that a collection of items has an even count, throwing anIllegalArgumentExceptionif items is null or there is an odd number of items.- Type Parameters:
T- the object type- Parameters:
items- items to count
-
checkEvenItemCount
Ensures that a collection of items has an even count, throwing anIllegalArgumentExceptionif items is null or there is an odd number of items.- Type Parameters:
T- the object type- Parameters:
items- items to count
-
checkEvenItemCount
Ensures that a collection of items has an even count, throwing anIllegalArgumentExceptionif countSupplier is null or returns an odd number.- Parameters:
countSupplier- anIntSupplierthat returns the count to evaluate
-
requireNotNullElse
public static <T> T requireNotNullElse(T obj, T defaultObj) Returns the first argument if it is notnull, otherwise the second argument (which must not benull).The main reason for this method instead of the
Objects.requireNonNullElse(Object, Object)is that this method throws anIllegalArgumentExceptioninstead of aNullPointerException, which was an unfortunate choice by the JDK.- Type Parameters:
T- the type of the reference- Parameters:
obj- an object, possiblynulldefaultObj- a non-nullobject- Returns:
- the first non-
nullargument
-
requireNotNullElseGet
Returns the first argument if it is notnull, otherwise the value supplied by theSupplier, which must not benull.The main reason for this method instead of the
Objects.requireNonNullElse(Object, Object)is that this method throws anIllegalArgumentExceptioninstead of aNullPointerException, which was an unfortunate choice by the JDK.- Type Parameters:
T- the type of the reference- Parameters:
obj- an object, possiblynullsupplier- creates a non-nullobject- Returns:
- the first argument, or the value from
supplier
-
checkPositive
public static void checkPositive(int value) Ensures intvalueis a positive number (greater than zero).- Parameters:
value- the value to check for positivity- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositive
Ensures intvalueis a positive number (greater than zero).- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositive
public static void checkPositive(int value, String errorMessageTemplate, Object... errorMessageArgs) Ensures intvalueis a positive number (greater than zero).- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositive
public static void checkPositive(long value) Ensures longvalueis a positive number (greater than zero).- Parameters:
value- the value to check for positivity- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositive
Ensures longvalueis a positive number (greater than zero).- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositive
public static void checkPositive(long value, String errorMessageTemplate, Object... errorMessageArgs) Ensures longvalueis a positive number (greater than zero).- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositiveOrZero
public static void checkPositiveOrZero(int value) Ensures intvalueis a positive number (greater than zero) or zero.- Parameters:
value- the value to check for positivity- Throws:
IllegalStateException- if the value is not positive or zero- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositiveOrZero
Ensures intvalueis a positive number (greater than zero) or zero.- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositiveOrZero
public static void checkPositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) Ensures intvalueis a positive number (greater than zero) or zero.- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not zero or positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositiveOrZero
public static void checkPositiveOrZero(long value) Ensures longvalueis a positive number (greater than zero) or zero.- Parameters:
value- the value to check for positivity- Throws:
IllegalStateException- if the value is not positive or zero- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositiveOrZero
Ensures longvalueis a positive number (greater than zero) or zero.- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkPositiveOrZero
public static void checkPositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) Ensures longvalueis a positive number (greater than zero) or zero.- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not zero or positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositive
public static int requirePositive(int value) Returns the int value if it is positive, throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivity- Returns:
- the given value if positive
- Throws:
IllegalStateException- if the value is not positive
-
requirePositive
Returns the intvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Returns:
- the given value if positive
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositive
public static int requirePositive(int value, String errorMessageTemplate, Object... errorMessageArgs) Returns the intvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given value if positive
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositive
public static long requirePositive(long value) Returns the long value if it is positive, throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivity- Returns:
- the given value if positive
- Throws:
IllegalStateException- if the value is not positive
-
requirePositive
Returns the longvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Returns:
- the given value if positive
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositive
public static long requirePositive(long value, String errorMessageTemplate, Object... errorMessageArgs) Returns the longvalueif it is a positive number (greater than zero), throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given value if positive
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositiveOrZero
public static int requirePositiveOrZero(int value) Returns the int value if it is positive or zero, throwing anIllegalStateExceptionif not positive or zero.- Parameters:
value- the value to check for positivity or zero- Returns:
- the given value if positive or zero
- Throws:
IllegalStateException- if the value is not positive zero
-
requirePositiveOrZero
Returns the intvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Returns:
- the given value if positive or zero
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositiveOrZero
public static int requirePositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) Returns the intvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not zero or positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given value if positive or zero
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositiveOrZero
public static long requirePositiveOrZero(long value) Returns the long value if it is positive or zero, throwing anIllegalStateExceptionif not positive or zero.- Parameters:
value- the value to check for positivity or zero- Returns:
- the given value if positive or zero
- Throws:
IllegalStateException- if the value is not positive zero
-
requirePositiveOrZero
Returns the longvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessage- the error message to put in the exception if not positive- Returns:
- the given value if positive or zero
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
requirePositiveOrZero
public static long requirePositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) Returns the longvalueif it is a positive number (greater than zero) or zero, throwing anIllegalStateExceptionif not positive.- Parameters:
value- the value to check for positivityerrorMessageTemplate- a template for the exception message if value is not zero or positive, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given value if positive or zero
- Throws:
IllegalStateException- if the value is not positive (e.g. greater than zero)- See Also:
-
Preconditions.checkState(boolean, Object)
-
checkValidPort
public static void checkValidPort(int port) Ensures given port is valid, between 0 andMAX_PORT_NUMBER.- Parameters:
port- the port to check for validity- Throws:
IllegalStateException- if port is not valid
-
checkValidPort
Ensures given port is valid, between 0 andMAX_PORT_NUMBER.- Parameters:
port- the port to check for validityerrorMessage- the error message to put in the exception if the port is not valid- Throws:
IllegalStateException- if port is not valid
-
checkValidPort
public static void checkValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Ensures given port is valid, between 0 andMAX_PORT_NUMBER.- Parameters:
port- the port to check for validityerrorMessageTemplate- a template for the exception message if port is not valid, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Throws:
IllegalStateException- if port is not valid
-
requireValidPort
public static int requireValidPort(int port) Returns the given port if it is valid- Parameters:
port- the port to check for validity- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
requireValidPort
Returns the given port if it is valid- Parameters:
port- the port to check for validityerrorMessage- the error message to put in the exception if the port is not valid- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
requireValidPort
public static int requireValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Returns the given port if it is valid- Parameters:
port- the port to check for validityerrorMessageTemplate- a template for the exception message if port is not valid, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
checkValidNonZeroPort
public static void checkValidNonZeroPort(int port) Ensures given port is valid (excluding zero), between 1 andMAX_PORT_NUMBER.- Parameters:
port- the port to check for validity- Throws:
IllegalStateException- if port is not valid
-
checkValidNonZeroPort
Ensures given port is valid (excluding zero), between 1 andMAX_PORT_NUMBER.- Parameters:
port- the port to check for validityerrorMessage- the error message to put in the exception if the port is not valid- Throws:
IllegalStateException- if port is not valid
-
checkValidNonZeroPort
public static void checkValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Ensures given port is valid (excluding zero), between 1 andMAX_PORT_NUMBER.- Parameters:
port- the port to check for validityerrorMessageTemplate- a template for the exception message if port is not valid, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Throws:
IllegalStateException- if port is not valid
-
requireValidNonZeroPort
public static int requireValidNonZeroPort(int port) Returns the given port if it is valid (excluding zero)- Parameters:
port- the port to check for validity- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
requireValidNonZeroPort
Returns the given port if it is valid (excluding zero)- Parameters:
port- the port to check for validityerrorMessage- the error message to put in the exception if the port is not valid- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
requireValidNonZeroPort
public static int requireValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) Returns the given port if it is valid (excluding zero)- Parameters:
port- the port to check for validityerrorMessageTemplate- a template for the exception message if port is not valid, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
checkArgumentInstanceOf
Ensures the argument has the expected type.- Type Parameters:
T- the class of the required type- Parameters:
argument- the argument to checkrequiredType- the type that the argument is required to be
-
checkArgumentInstanceOf
public static <T> void checkArgumentInstanceOf(T argument, Class<?> requiredType, String errorMessage) Ensures the argument has the expected type.- Type Parameters:
T- the class of the required type- Parameters:
argument- the argument to checkrequiredType- the type that the argument is required to beerrorMessage- the error message to put in the exception if the argument is not the required type
-
checkArgumentInstanceOf
public static <T> void checkArgumentInstanceOf(T argument, Class<?> requiredType, String errorMessageTemplate, Object... errorMessageArgs) Ensures the argument has the expected type.- Type Parameters:
T- the class of the required type- Parameters:
argument- the argument to checkrequiredType- the type that the argument is required to beerrorMessageTemplate- the error message template to use in the exception if the argument is not the required type, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template
-
checkArgumentNotInstanceOf
Ensures the argument type is not the restricted type.- Type Parameters:
T- the class of the restricted type- Parameters:
argument- the argument to checkrestrictedType- the type that the argument must not be
-
checkArgumentNotInstanceOf
public static <T> void checkArgumentNotInstanceOf(T argument, Class<?> restrictedType, String errorMessage) Ensures the argument type is not the restricted type.- Type Parameters:
T- the class of the restricted type- Parameters:
argument- the argument to checkrestrictedType- the type that the argument must not beerrorMessage- the error message to put in the exception if the argument is of the restricted type
-
checkArgumentNotInstanceOf
public static <T> void checkArgumentNotInstanceOf(T argument, Class<?> restrictedType, String errorMessageTemplate, Object... errorMessageArgs) Ensures the argument type is not the restricted type.- Type Parameters:
T- the class of the restricted type- Parameters:
argument- the argument to checkrestrictedType- the type that the argument must not beerrorMessageTemplate- the error message to use in the exception if the argument is of the restricted type, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to populate into the error message template
-