This gist is a small demonstration of using Guava's Preconditions
class to more concisely express preconditions and assertions. Often, methods and constructors have stanzas such as the following declared:
public static boolean doSomething(final Object param1, final Object param2) {
if (param1 == null) {
throw InvalidArgumentException("doSomething requires a non-null value for param1");
}
if (param2 == null) {
throw InvalidArgumentException("doSomething requires a non-null value for param2");
}
// do work ...
}
The checkArgument
and checkState
methods shorten these checks.
N.B. This could be further simplified by introducing two additional utility methods wrapping checkArgument
-- checkArgumentNotNull
and checkArgumentNotBlank
.