Package com.privatejgoodies.common.base
Class Preconditions
java.lang.Object
com.privatejgoodies.common.base.Preconditions
Reduces the code necessary to check preconditions on method state and parameters.
- Author:
- Karsten Lentzsch
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckArgument(boolean expression, String message) Checks the truth of the given expression and throws a customizedIllegalArgumentExceptionif it is false.static voidcheckArgument(boolean expression, String messageFormat, Object... messageArgs) Checks the truth of the given expression and throws a customizedIllegalArgumentExceptionif it is false.static StringcheckNotBlank(String str, String message) Checks that the given string is not blank and throws a customizedNullPointerExceptionif it isnull, and a customizedIllegalArgumentExceptionif it is empty or whitespace.static StringcheckNotBlank(String str, String messageFormat, Object... messageArgs) Checks that the given string is not blank and throws a customizedNullPointerExceptionif it isnull, and a customizedIllegalArgumentExceptionif it is empty or whitespace.static <T> TcheckNotNull(T reference, String message) Checks that the given object reference is notnulland throws a customizedNullPointerExceptionif it is.static <T> TcheckNotNull(T reference, String messageFormat, Object... messageArgs) Checks that the given object reference is notnulland throws a customizedNullPointerExceptionif it is.static voidcheckState(boolean expression, String message) Checks the truth of the given expression and throws a customizedIllegalStateExceptionif it is false.static voidcheckState(boolean expression, String messageFormat, Object... messageArgs) Checks the truth of the given expression and throws a customizedIllegalStateExceptionif it is false.
-
Method Details
-
checkArgument
Checks the truth of the given expression and throws a customizedIllegalArgumentExceptionif it is false. Intended for doing parameter validation in methods and constructors, e.g.:public void foo(int count) { Preconditions.checkArgument(count > 0, "count must be positive."); }- Parameters:
expression- the precondition to check involving one ore more parameters to the calling method or constructormessage- the detail message to be used in the event that an exception is thrown- Throws:
IllegalArgumentException- ifexpressionis false
-
checkArgument
Checks the truth of the given expression and throws a customizedIllegalArgumentExceptionif it is false. Intended for doing parameter validation in methods and constructors, e.g.:public void foo(int count) { Preconditions.checkArgument(count > 0, "count must be positive: %s.", count); }- Parameters:
expression- the precondition to check involving one ore more parameters to the calling method or constructormessageFormat- aformatstring for the detail message to be used in the event that an exception is thrown.messageArgs- the arguments referenced by the format specifiers in themessageFormat- Throws:
IllegalArgumentException- ifexpressionis false
-
checkNotNull
Checks that the given object reference is notnulland throws a customizedNullPointerExceptionif it is. Intended for doing parameter validation in methods and constructors, e.g.:public void foo(Bar bar, Baz baz) { this.bar = Preconditions.checkNotNull(bar, "bar must not be null."); Preconditions.checkNotBull(baz, "baz must not be null."); }- Type Parameters:
T- the type of the reference- Parameters:
reference- the object reference to check for beingnullmessage- the detail message to be used in the event that an exception is thrown- Returns:
referenceif notnull- Throws:
NullPointerException- ifreferenceisnull
-
checkNotNull
Checks that the given object reference is notnulland throws a customizedNullPointerExceptionif it is. Intended for doing parameter validation in methods and constructors, e.g.:public void foo(Bar bar, Baz baz) { this.bar = Preconditions.checkNotNull(bar, "bar must not be null."); Preconditions.checkNotBull(baz, "The %s must not be null.", "baz"); }- Type Parameters:
T- the type of the reference- Parameters:
reference- the object reference to check for beingnullmessageFormat- aformatstring for the detail message to be used in the event that an exception is thrown.messageArgs- the arguments referenced by the format specifiers in themessageFormat- Returns:
referenceif notnull- Throws:
NullPointerException- ifreferenceisnull
-
checkState
Checks the truth of the given expression and throws a customizedIllegalStateExceptionif it is false. Intended for doing validation in methods involving the state of the calling instance, but not involving parameters of the calling method, e.g.:public void unlock() { Preconditions.checkState(locked, "Must be locked to be unlocked."); }- Parameters:
expression- the precondition to check involving the state of the calling instancemessage- the detail message to be used in the event that an exception is thrown- Throws:
IllegalStateException- ifexpressionis false
-
checkState
Checks the truth of the given expression and throws a customizedIllegalStateExceptionif it is false. Intended for doing validation in methods involving the state of the calling instance, but not involving parameters of the calling method, e.g.:public void unlock() { Preconditions.checkState(locked, "Must be locked to be unlocked. Most recent lock: %s", mostRecentLock); }- Parameters:
expression- the precondition to check involving the state of the calling instancemessageFormat- aformatstring for the detail message to be used in the event that an exception is thrown.messageArgs- the arguments referenced by the format specifiers in themessageFormat- Throws:
IllegalStateException- ifexpressionis false
-
checkNotBlank
Checks that the given string is not blank and throws a customizedNullPointerExceptionif it isnull, and a customizedIllegalArgumentExceptionif it is empty or whitespace. Intended for doing parameter validation in methods and constructors, e.g.:public void foo(String text) { checkNotBlank(text, "The text must not be null, empty or whitespace."); }- Parameters:
str- the string to check for being blankmessage- the detail message to be used in the event that an exception is thrown- Returns:
strif notnull- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty or whitespace
-
checkNotBlank
Checks that the given string is not blank and throws a customizedNullPointerExceptionif it isnull, and a customizedIllegalArgumentExceptionif it is empty or whitespace. Intended for doing parameter validation in methods and constructors, e.g.:public void foo(String text, String id) { checkNotBlank( text, "The text for %s must not be null, empty or whitespace.", id); }- Parameters:
str- the string to check for being blankmessageFormat- aformatstring for the detail message to be used in the event that an exception is thrown.messageArgs- the arguments referenced by the format specifiers in themessageFormat- Returns:
strif notnull- Throws:
NullPointerException- ifstrisnullIllegalArgumentException- ifstris empty or whitespace
-