I like guava preconditions, but what I really need from it is one more method – check that the number is in range. Smt like this
//probably there should be checkStateInRange also
public static void checkArgumentInRange(double value, int min, int max) {
if (value < min || value > max) {
throw new IllegalArgumentException(String.format("%s must be in range [%s, %s]", value, min, max));
}
}
I believe I’m not alone and it’s a pretty common case. But such method doesn’t exist. Is there any reasons to not put such methods in com.google.common.base.Preconditions?
There are quite a few reasons I’d say. Here are the main ones:
Preconditionsmethods throws a specific exception type for what it checks:NullPointerException,IllegalArgumentException,IllegalStateExceptionorIndexOutOfBoundsException. A generalized range check would have no exception more specific thanIllegalArgumentExceptionto throw.checkArgumentandcheckStatedo everything you need. You can just writecheckArgument(value >= min && value <= max, ...). It’s simple and obvious what you’re checking.Additionally:
ints for the bounds, so you would really like to allow anyComparablethere.