Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make KiwiPreconditions methods accept same message template placeholders #1003

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 62 additions & 21 deletions src/main/java/org/kiwiproject/base/KiwiPreconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,16 @@ public static void checkPositive(int value, String errorMessage) {
* Ensures int {@code value} is a positive number (greater than zero).
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
* @see Preconditions#checkState(boolean, Object)
*/
public static void checkPositive(int value, String errorMessageTemplate, Object... errorMessageArgs) {
checkState(value > 0, errorMessageTemplate, errorMessageArgs);
if (value <= 0) {
throw newIllegalStateException(errorMessageTemplate, errorMessageArgs);
}
}

/**
Expand Down Expand Up @@ -559,13 +562,16 @@ public static void checkPositive(long value, String errorMessage) {
* Ensures long {@code value} is a positive number (greater than zero).
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageArgs the arguments to populate into the error message template
* @param errorMessageTemplate a template for the exception message if value is not positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to be substituted into the message template
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
* @see Preconditions#checkState(boolean, Object)
*/
public static void checkPositive(long value, String errorMessageTemplate, Object... errorMessageArgs) {
checkState(value > 0, errorMessageTemplate, errorMessageArgs);
if (value <= 0) {
throw newIllegalStateException(errorMessageTemplate, errorMessageArgs);
}
}

/**
Expand Down Expand Up @@ -595,13 +601,16 @@ public static void checkPositiveOrZero(int value, String errorMessage) {
* Ensures int {@code value} is a positive number (greater than zero) or zero.
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not zero or positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
* @see Preconditions#checkState(boolean, Object)
*/
public static void checkPositiveOrZero(int value, String errorMessageTemplate, Object... errorMessageArgs) {
checkState(value >= 0, errorMessageTemplate, errorMessageArgs);
if (value < 0) {
throw newIllegalStateException(errorMessageTemplate, errorMessageArgs);
}
}

/**
Expand Down Expand Up @@ -631,13 +640,16 @@ public static void checkPositiveOrZero(long value, String errorMessage) {
* Ensures long {@code value} is a positive number (greater than zero) or zero.
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not zero or positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
* @see Preconditions#checkState(boolean, Object)
*/
public static void checkPositiveOrZero(long value, String errorMessageTemplate, Object... errorMessageArgs) {
checkState(value >= 0, errorMessageTemplate, errorMessageArgs);
if (value < 0) {
throw newIllegalStateException(errorMessageTemplate, errorMessageArgs);
}
}

/**
Expand Down Expand Up @@ -670,7 +682,8 @@ public static int requirePositive(int value, String errorMessage) {
* Returns the int {@code value} if it is a positive number (greater than zero), throwing an {@link IllegalStateException} if not positive.
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @return the given value if positive
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
Expand Down Expand Up @@ -711,7 +724,8 @@ public static long requirePositive(long value, String errorMessage) {
* Returns the long {@code value} if it is a positive number (greater than zero), throwing an {@link IllegalStateException} if not positive.
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @return the given value if positive
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
Expand Down Expand Up @@ -752,7 +766,8 @@ public static int requirePositiveOrZero(int value, String errorMessage) {
* Returns the int {@code value} if it is a positive number (greater than zero) or zero, throwing an {@link IllegalStateException} if not positive.
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not zero or positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @return the given value if positive or zero
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
Expand Down Expand Up @@ -793,7 +808,8 @@ public static long requirePositiveOrZero(long value, String errorMessage) {
* Returns the long {@code value} if it is a positive number (greater than zero) or zero, throwing an {@link IllegalStateException} if not positive.
*
* @param value the value to check for positivity
* @param errorMessageTemplate the error message template to use in the exception if not positive
* @param errorMessageTemplate a template for the exception message if value is not zero or positive, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @return the given value if positive or zero
* @throws IllegalStateException if the value is not positive (e.g. greater than zero)
Expand Down Expand Up @@ -822,19 +838,26 @@ public static void checkValidPort(int port) {
* @throws IllegalStateException if port is not valid
*/
public static void checkValidPort(int port, String errorMessage) {
checkState(port >= 0 && port <= MAX_PORT_NUMBER, errorMessage);
checkState(isValidPort(port), errorMessage);
}

/**
* Ensures given port is valid, between 0 and {@link #MAX_PORT_NUMBER}.
*
* @param port the port to check for validity
* @param errorMessageTemplate the error message template to use in the exception if port is not valid
* @param errorMessageTemplate a template for the exception message if port is not valid, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @throws IllegalStateException if port is not valid
*/
public static void checkValidPort(int port, String errorMessageTemplate, Object... errorMessageArgs) {
checkState(port >= 0 && port <= MAX_PORT_NUMBER, errorMessageTemplate, errorMessageArgs);
if (!isValidPort(port)) {
throw newIllegalStateException(errorMessageTemplate, errorMessageArgs);
}
}

private static boolean isValidPort(int port) {
return port >= 0 && lessThanOrEqualToMaxPort(port);
}

/**
Expand Down Expand Up @@ -866,7 +889,8 @@ public static int requireValidPort(int port, String errorMessage) {
* Returns the given port if it is valid
*
* @param port the port to check for validity
* @param errorMessageTemplate the error message template to use in the exception if port is not valid
* @param errorMessageTemplate a template for the exception message if port is not valid, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @return the given port if valid
* @throws IllegalStateException if port is not valid
Expand Down Expand Up @@ -894,19 +918,30 @@ public static void checkValidNonZeroPort(int port) {
* @throws IllegalStateException if port is not valid
*/
public static void checkValidNonZeroPort(int port, String errorMessage) {
checkState(port > 0 && port <= MAX_PORT_NUMBER, errorMessage);
checkState(isValidPortAboveZero(port), errorMessage);
}

/**
* Ensures given port is valid (excluding zero), between 1 and {@link #MAX_PORT_NUMBER}.
*
* @param port the port to check for validity
* @param errorMessageTemplate the error message template to use in the exception if port is not valid
* @param errorMessageTemplate a template for the exception message if port is not valid, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @throws IllegalStateException if port is not valid
*/
public static void checkValidNonZeroPort(int port, String errorMessageTemplate, Object... errorMessageArgs) {
checkState(port > 0 && port <= MAX_PORT_NUMBER, errorMessageTemplate, errorMessageArgs);
if (!isValidPortAboveZero(port)) {
throw newIllegalStateException(errorMessageTemplate, errorMessageArgs);
}
}

private static boolean isValidPortAboveZero(int port) {
return port > 0 && lessThanOrEqualToMaxPort(port);
}

private static boolean lessThanOrEqualToMaxPort(int port) {
return port <= MAX_PORT_NUMBER;
}

/**
Expand Down Expand Up @@ -938,7 +973,8 @@ public static int requireValidNonZeroPort(int port, String errorMessage) {
* Returns the given port if it is valid (excluding zero)
*
* @param port the port to check for validity
* @param errorMessageTemplate the error message template to use in the exception if port is not valid
* @param errorMessageTemplate a template for the exception message if port is not valid, according to how
* {@link KiwiStrings#format(String, Object...)} handles placeholders
* @param errorMessageArgs the arguments to populate into the error message template
* @return the given port if valid
* @throws IllegalStateException if port is not valid
Expand Down Expand Up @@ -1049,4 +1085,9 @@ private static IllegalArgumentException newIllegalArgumentException(String error
return new IllegalArgumentException(errorMessage);
}

private static IllegalStateException newIllegalStateException(String errorMessageTemplate,
Object... errorMessageArgs) {
var errorMessage = format(errorMessageTemplate, errorMessageArgs);
return new IllegalStateException(errorMessage);
}
}
Loading