@GwtCompatible public final class Verify extends Object
Bill bill = remoteService.getLastUnpaidBill();
// In case bug 12345 happens again we'd rather just die
Verify.verify(bill.status() == Status.UNPAID,
"Unexpected bill status: %s", bill.status());
Note: In some cases the differences explained below can be subtle. When it's unclear which approach to use, don't worry too much about it; just pick something that seems reasonable and it will be fine.
Preconditions
class instead.
if/throw
(as illustrated below) is always acceptable; we still
recommend using our VerifyException
exception type. Throwing a plain RuntimeException
is frowned upon.
Objects.requireNonNull(Object)
is generally discouraged, since
verifyNotNull(Object)
and Preconditions.checkNotNull(Object)
perform the
same function with more clarity.
Remember that parameter values for message construction must all be computed eagerly, and autoboxing and varargs array creation may happen as well, even when the verification succeeds and the message ends up unneeded. Performance-sensitive verification checks should continue to use usual form:
Bill bill = remoteService.getLastUnpaidBill();
if (bill.status() != Status.UNPAID) {
throw new VerifyException("Unexpected bill status: " + bill.status());
}
%s
is supportedAs with Preconditions
, Verify
uses Strings.lenientFormat(java.lang.String, java.lang.Object...)
to format
error message template strings. This only supports the "%s"
specifier, not the full range
of Formatter
specifiers. However, note that if the number of arguments does not
match the number of occurrences of "%s"
in the format string, Verify
will still
behave as expected, and will still include all argument values in the error message; the message
will simply not be formatted exactly as intended.
Modifier and Type | Method and Description |
---|---|
static void |
verify(boolean expression)
Ensures that
expression is true , throwing a VerifyException with no
message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
char p1)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
char p1,
char p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
char p1,
int p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
char p1,
long p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
char p1,
Object p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
int p1)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
int p1,
char p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
int p1,
int p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
int p1,
long p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
int p1,
Object p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
long p1)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
long p1,
char p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
long p1,
int p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
long p1,
long p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
long p1,
Object p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object... errorMessageArgs)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1,
char p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1,
int p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1,
long p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1,
Object p2)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1,
Object p2,
Object p3)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static void |
verify(boolean expression,
String errorMessageTemplate,
Object p1,
Object p2,
Object p3,
Object p4)
Ensures that
expression is true , throwing a VerifyException with a
custom message otherwise. |
static <T> T |
verifyNotNull(T reference)
Ensures that
reference is non-null, throwing a VerifyException with a default
message otherwise. |
static <T> T |
verifyNotNull(T reference,
String errorMessageTemplate,
Object... errorMessageArgs)
Ensures that
reference is non-null, throwing a VerifyException with a custom
message otherwise. |
public static void verify(boolean expression)
expression
is true
, throwing a VerifyException
with no
message otherwise.VerifyException
- if expression
is false
Preconditions.checkState()
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object... errorMessageArgs)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.expression
- a boolean expressionerrorMessageTemplate
- a template for the exception message should the check fail. The
message is formed by replacing each %s
placeholder in the template with an
argument. These are matched by position - the first %s
gets errorMessageArgs[0]
, etc. Unmatched arguments will be appended to the formatted message in
square braces. Unmatched placeholders will be left as-is.errorMessageArgs
- the arguments to be substituted into the message template. Arguments
are converted to strings using String.valueOf(Object)
.VerifyException
- if expression
is false
Preconditions.checkState()
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, char p1)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, long p1)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, char p1, char p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1, char p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, long p1, char p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, char p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, char p1, int p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1, int p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, long p1, int p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, int p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, char p1, long p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1, long p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, long p1, long p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, long p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, char p1, @NullableDecl Object p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, int p1, @NullableDecl Object p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, long p1, @NullableDecl Object p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, @NullableDecl Object p2)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, @NullableDecl Object p2, @NullableDecl Object p3)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
public static void verify(boolean expression, @NullableDecl String errorMessageTemplate, @NullableDecl Object p1, @NullableDecl Object p2, @NullableDecl Object p3, @NullableDecl Object p4)
expression
is true
, throwing a VerifyException
with a
custom message otherwise.
See verify(boolean, String, Object...)
for details.
@CanIgnoreReturnValue public static <T> T verifyNotNull(@NullableDecl T reference)
reference
is non-null, throwing a VerifyException
with a default
message otherwise.reference
, guaranteed to be non-null, for convenienceVerifyException
- if reference
is null
Preconditions.checkNotNull()
@CanIgnoreReturnValue public static <T> T verifyNotNull(@NullableDecl T reference, @NullableDecl String errorMessageTemplate, @NullableDecl Object... errorMessageArgs)
reference
is non-null, throwing a VerifyException
with a custom
message otherwise.errorMessageTemplate
- a template for the exception message should the check fail. The
message is formed by replacing each %s
placeholder in the template with an
argument. These are matched by position - the first %s
gets errorMessageArgs[0]
, etc. Unmatched arguments will be appended to the formatted message in
square braces. Unmatched placeholders will be left as-is.errorMessageArgs
- the arguments to be substituted into the message template. Arguments
are converted to strings using String.valueOf(Object)
.reference
, guaranteed to be non-null, for convenienceVerifyException
- if reference
is null
Preconditions.checkNotNull()
Copyright © 2010–2019. All rights reserved.