|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.google.common.base.Preconditions
public final class Preconditions
Contains static methods that can be called at the start of your methods to verify correct arguments and state. Note that the standard Java idiom is to use the following directly:
if (!argumentAssumption) {
throw new IllegalArgumentException();
}
if (!stateAssumption) {
throw new IllegalStateException();
}
if (requiredArgument == null) {
throw new NullPointerException();
}
It is perfectly acceptable to stick to the standard idiom. There are two
primary reasons you might prefer to use this class instead. First, it is
significantly more compact (especially if you use static import, but even if
you don't).
Moreover, Preconditions goes to a little extra effort to highlight for you what it thinks (heuristically) are the two key frames of the stack trace. First is the line that identifies which precondition check failed. Second is the line of the most likely "offender", which is defined as the first frame in the stack trace that comes from a different class from the class checking the precondition. Example message:
java.lang.IllegalArgumentException: precondition failed: (your message here)
failed check: at somepackage.SomeClass.someMethod(SomeClass.java:101)
offending call: at otherpackage.Caller.callingMethod(Caller.java:99)
(Again, the above is only the exception message, and the full stack
trace is kept intact.)
| Method Summary | ||
|---|---|---|
static void |
checkArgument(boolean expression)
Ensures that expression is true. |
|
static void |
checkArgument(boolean expression,
Object message)
Ensures that expression is true. |
|
static void |
checkArgument(boolean expression,
String errorFormat,
Object... args)
Ensures that expression is true. |
|
static
|
checkContentsNotNull(T iterable)
Ensures that iterable is not null and that it contains no
null elements. |
|
static
|
checkNotNull(T reference)
Ensures that reference is not null. |
|
static
|
checkNotNull(T reference,
Object message)
Ensures that reference is not null. |
|
static
|
checkNotNull(T reference,
String errorFormat,
Object... args)
Ensures that reference is not null. |
|
static void |
checkState(boolean expression)
Ensures that expression is true. |
|
static void |
checkState(boolean expression,
Object message)
Ensures that expression is true. |
|
static void |
checkState(boolean expression,
String errorFormat,
Object... args)
Ensures that expression is true. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static void checkArgument(boolean expression)
expression is true.
expression - any boolean expression involving an argument to the
current method
IllegalArgumentException - if expression is falsepublic static void checkState(boolean expression)
expression is true.
expression - any boolean expression involving the state of the
current instance (and not involving arguments)
IllegalStateException - if expression is falsepublic static <T> T checkNotNull(T reference)
reference is not null.
reference - an object reference that was passed as a parameter to the
current method
NullPointerException - if reference is null
public static void checkArgument(boolean expression,
Object message)
expression is true.
expression - any boolean expression involving an argument to the
current methodmessage - a message object which will be converted using
Object#toString and included in the exception message if the
check fails
IllegalArgumentException - if expression is false
public static void checkState(boolean expression,
Object message)
expression is true.
expression - any boolean expression involving the state of the
current instance (and not involving arguments)message - a message object which will be converted using
Object#toString and included in the exception message if the
check fails
IllegalStateException - if expression is false
public static <T> T checkNotNull(T reference,
Object message)
reference is not null.
reference - an object reference that was passed as a parameter to the
current methodmessage - a message object which will be converted using
Object#toString and included in the exception message if the
check fails
NullPointerException - if reference is nullpublic static <T extends Iterable<?>> T checkContentsNotNull(T iterable)
iterable is not null and that it contains no
null elements.
iterable - the Iterable to check for nullness
iterable if not null
NullPointerException - if iterable is null, or if it contains
any null elements
public static void checkArgument(boolean expression,
String errorFormat,
Object... args)
expression is true.
expression - any boolean expression involving an argument to the
current methoderrorFormat - format of error message to produce if the check failsargs - the arguments for errorFormat
IllegalArgumentException - if expression is false
public static void checkState(boolean expression,
String errorFormat,
Object... args)
expression is true.
expression - any boolean expression involving the state of the
current instance (and not involving arguments)errorFormat - format of error message to produce if the check failsargs - the arguments for errorFormat
IllegalStateException - if errorFormat is false
public static <T> T checkNotNull(T reference,
String errorFormat,
Object... args)
reference is not null.
reference - an object reference that was passed as a parameter to the
current methoderrorFormat - format of error message to produce if the check failsargs - the arguments for errorFormat
NullPointerException - if reference is null
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||