Top Description Constructors Methods
com.google.common.base

public final Class Verify

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Strings.lenientFormat

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.

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 supported

As with Preconditions, Verify uses Strings#lenientFormat to format error message template strings. This only supports the "%s" specifier, not the full range of java.util.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.

More information

See Conditional failures explained in the Guava User Guide for advice on when this class should be used.
Since
17.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
verify(boolean expression)

Ensures that expression is true, throwing a VerifyException with no message otherwise.

public static void
verify(boolean
a boolean expression
expression
,
String
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.
errorMessageTemplate
,
@Nullable Object...
the arguments to be substituted into the message template. Arguments are converted to strings using String#valueOf(Object).
errorMessageArgs
)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, char p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, int p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, long p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, Object p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, char p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, int p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, long p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, Object p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, char p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, int p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, long p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, Object p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, char p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, int p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, long p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, Object p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, char p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, int p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, long p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public static void
verify(boolean expression, String errorMessageTemplate, Object p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

public 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.

public 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.

public static <T> T

Returns:

reference, guaranteed to be non-null, for convenience
verifyNotNull
(T reference)

Ensures that reference is non-null, throwing a VerifyException with a default message otherwise.

public static <T> T

Returns:

reference, guaranteed to be non-null, for convenience
verifyNotNull
(T reference, String
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.
errorMessageTemplate
,
@Nullable Object...
the arguments to be substituted into the message template. Arguments are converted to strings using String#valueOf(Object).
errorMessageArgs
)

Ensures that reference is non-null, throwing a VerifyException with a custom message otherwise.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Verifyback to summary
private Verify()

Method Detail

verifyback to summary
public static void verify(boolean expression)

Ensures that expression is true, throwing a VerifyException with no message otherwise.

Exceptions
VerifyException:
if expression is false
See Also
Preconditions.checkState()
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, @Nullable Object... errorMessageArgs)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

Parameters
expression:boolean

a boolean expression

errorMessageTemplate:String

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:@Nullable Object[]

the arguments to be substituted into the message template. Arguments are converted to strings using String#valueOf(Object).

Exceptions
VerifyException:
if expression is false
See Also
Preconditions.checkState()
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, char p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, int p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, long p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, Object p1)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, char p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, int p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, long p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, Object p1, char p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, char p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, int p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, long p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, Object p1, int p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, char p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, int p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, long p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, Object p1, long p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, char p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, int p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, long p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public static void verify(boolean expression, String errorMessageTemplate, Object p1, Object p2)

Ensures that expression is true, throwing a VerifyException with a custom message otherwise.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public 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.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyback to summary
public 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.

See verify(boolean, String, Object...) for details.

Since
23.1 (varargs overload since 17.0)
verifyNotNullback to summary
public static <T> T verifyNotNull(T reference)

Ensures that reference is non-null, throwing a VerifyException with a default message otherwise.

Returns:T

reference, guaranteed to be non-null, for convenience

Annotations
@CanIgnoreReturnValue
Exceptions
VerifyException:
if reference is null
See Also
Preconditions.checkNotNull()
verifyNotNullback to summary
public static <T> T verifyNotNull(T reference, String errorMessageTemplate, @Nullable Object... errorMessageArgs)

Ensures that reference is non-null, throwing a VerifyException with a custom message otherwise.

Parameters
errorMessageTemplate:String

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:@Nullable Object[]

the arguments to be substituted into the message template. Arguments are converted to strings using String#valueOf(Object).

Returns:T

reference, guaranteed to be non-null, for convenience

Annotations
@CanIgnoreReturnValue
Exceptions
VerifyException:
if reference is null
See Also
Preconditions.checkNotNull()