Class Verify
- java.lang.Object
-
- com.google.common.base.Verify
-
@GwtCompatible public final class Verify extends java.lang.Object
Static convenience methods that serve the same purpose as Java language assertions, except that they are always enabled. These methods should be used instead of Java assertions whenever there is a chance the check may fail "in real life". Example: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());
Comparison to alternatives
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.
- If checking whether the caller has violated your method or constructor's contract
(such as by passing an invalid argument), use the utilities of the
Preconditions
class instead. - If checking an impossible condition (which cannot happen unless your own class or its trusted dependencies is badly broken), this is what ordinary Java assertions are for. Note that assertions are not enabled by default; they are essentially considered "compiled comments."
- An explicit
if/throw
(as illustrated below) is always acceptable; we still recommend using ourVerifyException
exception type. Throwing a plainRuntimeException
is frowned upon. - Use of
Objects.requireNonNull(Object)
is generally discouraged, sinceverifyNotNull(Object)
andPreconditions.checkNotNull(Object)
perform the same function with more clarity.
Warning about performance
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()); }
Only
%s
is supportedAs with
Preconditions
,Verify
usesStrings.lenientFormat(java.lang.String, java.lang.Object...)
to format error message template strings. This only supports the"%s"
specifier, not the full range ofFormatter
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.More information
See Conditional failures explained in the Guava User Guide for advice on when this class should be used.- Since:
- 17.0
- If checking whether the caller has violated your method or constructor's contract
(such as by passing an invalid argument), use the utilities of the
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
verify(boolean expression)
Ensures thatexpression
istrue
, throwing aVerifyException
with no message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, char p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, char p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, char p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, char p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, char p1, java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, int p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, int p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, int p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, int p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, int p1, java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, long p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, long p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, long p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, long p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, long p1, java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, @Nullable java.lang.Object... errorMessageArgs)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1, java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1, java.lang.Object p2, java.lang.Object p3)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static void
verify(boolean expression, java.lang.String errorMessageTemplate, java.lang.Object p1, java.lang.Object p2, java.lang.Object p3, java.lang.Object p4)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.static <T> T
verifyNotNull(T reference)
Ensures thatreference
is non-null, throwing aVerifyException
with a default message otherwise.static <T> T
verifyNotNull(T reference, java.lang.String errorMessageTemplate, @Nullable java.lang.Object... errorMessageArgs)
Ensures thatreference
is non-null, throwing aVerifyException
with a custom message otherwise.
-
-
-
Method Detail
-
verify
public static void verify(boolean expression)
Ensures thatexpression
istrue
, throwing aVerifyException
with no message otherwise.- Throws:
VerifyException
- ifexpression
isfalse
- See Also:
Preconditions.checkState()
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull @Nullable java.lang.Object... errorMessageArgs)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.- Parameters:
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
getserrorMessageArgs[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 usingString.valueOf(Object)
.- Throws:
VerifyException
- ifexpression
isfalse
- See Also:
Preconditions.checkState()
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, char p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, int p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, long p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, char p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, int p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, long p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1, char p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, char p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, int p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, long p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1, int p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, char p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, int p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, long p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1, long p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, char p1, @CheckForNull java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, int p1, @CheckForNull java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, long p1, @CheckForNull java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1, @CheckForNull java.lang.Object p2)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1, @CheckForNull java.lang.Object p2, @CheckForNull java.lang.Object p3)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verify
public static void verify(boolean expression, java.lang.String errorMessageTemplate, @CheckForNull java.lang.Object p1, @CheckForNull java.lang.Object p2, @CheckForNull java.lang.Object p3, @CheckForNull java.lang.Object p4)
Ensures thatexpression
istrue
, throwing aVerifyException
with a custom message otherwise.See
verify(boolean, String, Object...)
for details.- Since:
- 23.1 (varargs overload since 17.0)
-
verifyNotNull
@CanIgnoreReturnValue public static <T> T verifyNotNull(@CheckForNull T reference)
Ensures thatreference
is non-null, throwing aVerifyException
with a default message otherwise.- Returns:
reference
, guaranteed to be non-null, for convenience- Throws:
VerifyException
- ifreference
isnull
- See Also:
Preconditions.checkNotNull()
-
verifyNotNull
@CanIgnoreReturnValue public static <T> T verifyNotNull(@CheckForNull T reference, java.lang.String errorMessageTemplate, @CheckForNull @Nullable java.lang.Object... errorMessageArgs)
Ensures thatreference
is non-null, throwing aVerifyException
with a custom message otherwise.- Parameters:
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
getserrorMessageArgs[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 usingString.valueOf(Object)
.- Returns:
reference
, guaranteed to be non-null, for convenience- Throws:
VerifyException
- ifreference
isnull
- See Also:
Preconditions.checkNotNull()
-
-