Skip to content

Instantly share code, notes, and snippets.

@dmolesUC
Created January 3, 2018 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmolesUC/6905a009f40765fc68b78b6584be1270 to your computer and use it in GitHub Desktop.
Save dmolesUC/6905a009f40765fc68b78b6584be1270 to your computer and use it in GitHub Desktop.
Argument assertions
public class Arguments {
// ------------------------------------------------------------
// Checks
public static <T> T require(T argument, Predicate<T> condition, Supplier<String> msgSupplier) {
return require(argument, () -> condition.test(argument), msgSupplier);
}
public static <T> T requireNot(T argument, Predicate<T> condition, Supplier<String> msgSupplier) {
return require(argument, condition.negate(), msgSupplier);
}
public static <T> T require(T argument, BooleanSupplier test, Supplier<String> msgSupplier) {
if (test.getAsBoolean()) {
return argument;
}
throw new IllegalArgumentException(msgSupplier.get());
}
public static <T> T requireNot(T argument, BooleanSupplier test, Supplier<String> msgSupplier) {
return require(argument, () -> !test.getAsBoolean(), msgSupplier);
}
// ------------------------------------------------------------
// Constructor
private Arguments() {
// private to prevent accidental instatiation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment