Package org.kiwiproject.base
Class KiwiPreconditions
- java.lang.Object
-
- org.kiwiproject.base.KiwiPreconditions
-
public class KiwiPreconditions extends Object
Static utility methods similar to those found inPreconditions, 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.- Implementation Note:
- Many of the methods in this class use Lombok's
SneakyThrowsso that methods do not need to declare that they throw exceptions 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."
-
-
Constructor Summary
Constructors Constructor Description KiwiPreconditions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <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 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> 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 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 Detail
-
checkArgument
public static <T extends Throwable> void checkArgument(boolean expression, Class<T> exceptionType)
Ensures the truth of an expression involving one or more parameters to the calling method.Throws an exception of type T if
expressionis 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's
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 exception of type T if
expressionis 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's
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 exception of type T if
expressionis 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's
SneakyThrowsto throw any checked exceptions without declaring them.
-
requireNotBlank
public static String requireNotBlank(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.- Parameters:
value- the String value to check- Returns:
- the given String
- See Also:
checkArgumentNotBlank(String)
-
requireNotBlank
public static String requireNotBlank(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.- Parameters:
value- the String value to checkerrorMessage- the error message for the exception- Returns:
- the given String
- See Also:
checkArgumentNotBlank(String, String)
-
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:
checkArgumentNotBlank(String, String, Object...)
-
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
public static <T> void checkArgumentNotNull(T reference, String errorMessage)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).
-
checkArgumentNotBlank
public static void checkArgumentNotBlank(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.- Parameters:
string- a string
-
checkArgumentNotBlank
public static void checkArgumentNotBlank(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.- 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).
-
checkEvenItemCount
@SafeVarargs public static <T> void checkEvenItemCount(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.- Type Parameters:
T- the object type- Parameters:
items- items to count
-
checkEvenItemCount
public static <T> void checkEvenItemCount(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.- Type Parameters:
T- the object type- Parameters:
items- items to count
-
checkEvenItemCount
public static void checkEvenItemCount(IntSupplier countSupplier)
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
public static <T> T requireNotNullElseGet(T obj, Supplier<? extends T> supplier)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
public static void checkPositive(int value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static void checkPositive(long value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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(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
public static void checkPositiveOrZero(int value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static void checkPositiveOrZero(long value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static int requirePositive(int value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static long requirePositive(long value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static int requirePositiveOrZero(int value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static long requirePositiveOrZero(long value, String errorMessage)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- the error message template to use in the exception if not positiveerrorMessageArgs- 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
public static void checkValidPort(int port, String errorMessage)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- the error message template to use in the exception if port is not validerrorMessageArgs- 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
public static int requireValidPort(int port, String errorMessage)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- the error message template to use in the exception if port is not validerrorMessageArgs- 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
public static void checkValidNonZeroPort(int port, String errorMessage)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- the error message template to use in the exception if port is not validerrorMessageArgs- 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
public static int requireValidNonZeroPort(int port, String errorMessage)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- the error message template to use in the exception if port is not validerrorMessageArgs- the arguments to populate into the error message template- Returns:
- the given port if valid
- Throws:
IllegalStateException- if port is not valid
-
-