
net.yetamine.checks.Constraint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of net.yetamine.checks Show documentation
Show all versions of net.yetamine.checks Show documentation
A set of utilities for compact runtime checks.
package net.yetamine.checks;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* A utility class for checking general conditions.
*/
public final class Constraint {
/**
* Checks whether a condition is satisfied.
*
* @param
* the type of the exception to throw
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
void check(boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for an object is satisfied.
*
* @param
* the type of the result
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition predicate. It must not be {@code null}.
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
T check(T result, Predicate super T> condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition.test(result)) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for an object is satisfied.
*
* @param
* the type of the result
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
T check(T result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
char check(char result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
byte check(byte result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
short check(short result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
int check(int result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
long check(long result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
float check(float result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
/**
* Checks whether a condition for a value is satisfied.
*
* @param
* the type of the exception to throw
* @param result
* the result of this method
* @param condition
* the condition result
* @param exceptionSupplier
* the supplier of the exception which is thrown when the
* condition is not met. It must not be {@code null}.
*
* @return the provided result
*
* @throws X
* if the condition result is {@code false}
*/
public static // @formatter:break
double check(double result, boolean condition, Supplier extends X> exceptionSupplier) throws X {
assert (exceptionSupplier != null) : "Exception supplier must not be null.";
if (condition) {
return result;
}
throw exceptionSupplier.get();
}
private Constraint() {
throw new AssertionError();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy