Created
January 3, 2018 23:40
-
-
Save dmolesUC/6905a009f40765fc68b78b6584be1270 to your computer and use it in GitHub Desktop.
Argument assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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