Package com.jgoodies.common.base
Class Preconditions
- java.lang.Object
-
- com.jgoodies.common.base.Preconditions
-
public final class Preconditions extends java.lang.Object
Reduces the code necessary to check preconditions on method state and parameters.
-
-
Constructor Summary
Constructors Modifier Constructor Description private
Preconditions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkArgument(boolean expression, java.lang.String message)
Checks the truth of the given expression and throws a customizedIllegalArgumentException
if it is false.static void
checkArgument(boolean expression, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks the truth of the given expression and throws a customizedIllegalArgumentException
if it is false.static java.lang.String
checkNotBlank(java.lang.String str, java.lang.String message)
Checks that the given string is not blank and throws a customizedNullPointerException
if it isnull
, and a customizedIllegalArgumentException
if it is empty or whitespace.static java.lang.String
checkNotBlank(java.lang.String str, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks that the given string is not blank and throws a customizedNullPointerException
if it isnull
, and a customizedIllegalArgumentException
if it is empty or whitespace.static <T> T
checkNotNull(T reference, java.lang.String message)
Checks that the given object reference is notnull
and throws a customizedNullPointerException
if it is.static <T> T
checkNotNull(T reference, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks that the given object reference is notnull
and throws a customizedNullPointerException
if it is.static void
checkState(boolean expression, java.lang.String message)
Checks the truth of the given expression and throws a customizedIllegalStateException
if it is false.static void
checkState(boolean expression, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks the truth of the given expression and throws a customizedIllegalStateException
if it is false.(package private) static java.lang.String
format(java.lang.String messageFormat, java.lang.Object... messageArgs)
-
-
-
Method Detail
-
checkArgument
public static void checkArgument(boolean expression, java.lang.String message)
Checks the truth of the given expression and throws a customizedIllegalArgumentException
if 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:
java.lang.IllegalArgumentException
- ifexpression
is false
-
checkArgument
public static void checkArgument(boolean expression, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks the truth of the given expression and throws a customizedIllegalArgumentException
if 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
- aformat
string 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:
java.lang.IllegalArgumentException
- ifexpression
is false
-
checkNotNull
public static <T> T checkNotNull(T reference, java.lang.String message)
Checks that the given object reference is notnull
and throws a customizedNullPointerException
if 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 beingnull
message
- the detail message to be used in the event that an exception is thrown- Returns:
reference
if notnull
- Throws:
java.lang.NullPointerException
- ifreference
isnull
-
checkNotNull
public static <T> T checkNotNull(T reference, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks that the given object reference is notnull
and throws a customizedNullPointerException
if 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 beingnull
messageFormat
- aformat
string 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:
reference
if notnull
- Throws:
java.lang.NullPointerException
- ifreference
isnull
-
checkState
public static void checkState(boolean expression, java.lang.String message)
Checks the truth of the given expression and throws a customizedIllegalStateException
if 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:
java.lang.IllegalStateException
- ifexpression
is false
-
checkState
public static void checkState(boolean expression, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks the truth of the given expression and throws a customizedIllegalStateException
if 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
- aformat
string 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:
java.lang.IllegalStateException
- ifexpression
is false
-
checkNotBlank
public static java.lang.String checkNotBlank(java.lang.String str, java.lang.String message)
Checks that the given string is not blank and throws a customizedNullPointerException
if it isnull
, and a customizedIllegalArgumentException
if 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:
str
if notnull
- Throws:
java.lang.NullPointerException
- ifstr
isnull
java.lang.IllegalArgumentException
- ifstr
is empty or whitespace
-
checkNotBlank
public static java.lang.String checkNotBlank(java.lang.String str, java.lang.String messageFormat, java.lang.Object... messageArgs)
Checks that the given string is not blank and throws a customizedNullPointerException
if it isnull
, and a customizedIllegalArgumentException
if 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
- aformat
string 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:
str
if notnull
- Throws:
java.lang.NullPointerException
- ifstr
isnull
java.lang.IllegalArgumentException
- ifstr
is empty or whitespace
-
format
static java.lang.String format(java.lang.String messageFormat, java.lang.Object... messageArgs)
-
-