Added an illustrative comment in the ParamMap precondition check. Added methods to take a formatted message in Preconditions.

This commit is contained in:
Inderjeet Singh 2010-07-13 17:24:19 +00:00
parent 58704f9aad
commit 8bcbab629a
2 changed files with 14 additions and 1 deletions

View File

@ -72,7 +72,8 @@ class ParamMap {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key, Type typeOfValue) { public <T> T get(String key, Type typeOfValue) {
Preconditions.checkArgument(spec.checkIfCompatible(key, typeOfValue)); Preconditions.checkArgument(spec.checkIfCompatible(key, typeOfValue),
"Incompatible key %s for type %s", key, typeOfValue);
return (T) contents.get(key); return (T) contents.get(key);
} }

View File

@ -23,9 +23,21 @@ final class Preconditions {
} }
} }
public static void checkArgument(boolean condition, String msg, Object... msgArgs) {
if (!condition) {
throw new IllegalArgumentException(String.format(msg, msgArgs));
}
}
public static void checkNotNull(Object obj) { public static void checkNotNull(Object obj) {
if (obj == null) { if (obj == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
public static void checkNotNull(Object obj, String msg, Object... msgArgs) {
if (obj == null) {
throw new IllegalArgumentException(String.format(msg, msgArgs));
}
}
} }