public final class Preconditions extends Object
Modifier and Type | Method and Description |
---|---|
static void |
checkArgument(boolean expression,
String message)
Checks the truth of the given expression and throws a customized
IllegalArgumentException if it is false. |
static void |
checkArgument(boolean expression,
String messageFormat,
Object... messageArgs)
Checks the truth of the given expression and throws a customized
IllegalArgumentException if it is false. |
static String |
checkNotBlank(String str,
String message)
Checks that the given string is not blank and throws a customized
NullPointerException if it is null , and a customized
IllegalArgumentException if it is empty or whitespace. |
static String |
checkNotBlank(String str,
String messageFormat,
Object... messageArgs)
Checks that the given string is not blank and throws a customized
NullPointerException if it is null , and a customized
IllegalArgumentException if it is empty or whitespace. |
static <T> T |
checkNotNull(T reference,
String message)
Checks that the given object reference is not
null
and throws a customized NullPointerException if it is. |
static <T> T |
checkNotNull(T reference,
String messageFormat,
Object... messageArgs)
Checks that the given object reference is not
null
and throws a customized NullPointerException if it is. |
static void |
checkState(boolean expression,
String message)
Checks the truth of the given expression and throws a customized
IllegalStateException if it is false. |
static void |
checkState(boolean expression,
String messageFormat,
Object... messageArgs)
Checks the truth of the given expression and throws a customized
IllegalStateException if it is false. |
public static void checkArgument(boolean expression, String message)
IllegalArgumentException
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."); }
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 thrownIllegalArgumentException
- if expression
is falsepublic static void checkArgument(boolean expression, String messageFormat, Object... messageArgs)
IllegalArgumentException
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); }
expression
- the precondition to check involving one ore more
parameters to the calling method or constructormessageFormat
- a format
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 the messageFormat
IllegalArgumentException
- if expression
is falsepublic static <T> T checkNotNull(T reference, String message)
null
and throws a customized NullPointerException
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."); }
T
- the type of the referencereference
- the object reference to check for being null
message
- the detail message to be used in the event that
an exception is thrownreference
if not null
NullPointerException
- if reference
is null
public static <T> T checkNotNull(T reference, String messageFormat, Object... messageArgs)
null
and throws a customized NullPointerException
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"); }
T
- the type of the referencereference
- the object reference to check for being null
messageFormat
- a format
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 the messageFormat
reference
if not null
NullPointerException
- if reference
is null
public static void checkState(boolean expression, String message)
IllegalStateException
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."); }
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 thrownIllegalStateException
- if expression
is falsepublic static void checkState(boolean expression, String messageFormat, Object... messageArgs)
IllegalStateException
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); }
expression
- the precondition to check involving the state
of the calling instancemessageFormat
- a format
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 the messageFormat
IllegalStateException
- if expression
is falsepublic static String checkNotBlank(String str, String message)
NullPointerException
if it is null
, and a customized
IllegalArgumentException
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."); }
str
- the string to check for being blankmessage
- the detail message to be used in the event that
an exception is thrownstr
if not null
NullPointerException
- if str
is null
IllegalArgumentException
- if str
is empty or whitespacepublic static String checkNotBlank(String str, String messageFormat, Object... messageArgs)
NullPointerException
if it is null
, and a customized
IllegalArgumentException
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); }
str
- the string to check for being blankmessageFormat
- a format
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 the messageFormat
str
if not null
NullPointerException
- if str
is null
IllegalArgumentException
- if str
is empty or whitespaceCopyright © 2014 JGoodies Software GmbH. All rights reserved.