Class Preconditions (1.45.0)

public final class Preconditions

Simple static methods to be called at the start of your own methods to verify correct arguments and state.

NOTE: proxy for the Guava implementation of com.google.common.base.Preconditions.

Inheritance

Object > Preconditions

Static Methods

<T>checkNotNull(T reference)

public static T <T>checkNotNull(T reference)

Ensures that an object reference passed as a parameter to the calling method is not null.

Parameter
Name Description
reference T

an object reference

Returns
Type Description
T

the non-null reference that was validated

<T>checkNotNull(T reference, Object errorMessage)

public static T <T>checkNotNull(T reference, Object errorMessage)

Ensures that an object reference passed as a parameter to the calling method is not null.

Parameters
Name Description
reference T

an object reference

errorMessage Object

the exception message to use if the check fails; will be converted to a string using String#valueOf(Object)

Returns
Type Description
T

the non-null reference that was validated

<T>checkNotNull(T reference, String errorMessageTemplate, Object[] errorMessageArgs)

public static T <T>checkNotNull(T reference, String errorMessageTemplate, Object[] errorMessageArgs)

Ensures that an object reference passed as a parameter to the calling method is not null.

Parameters
Name Description
reference T

an object reference

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 Object[]

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

Returns
Type Description
T

the non-null reference that was validated

checkArgument(boolean expression)

public static void checkArgument(boolean expression)

Ensures the truth of an expression involving one or more parameters to the calling method.

Parameter
Name Description
expression boolean

a boolean expression

checkArgument(boolean expression, Object errorMessage)

public static void checkArgument(boolean expression, Object errorMessage)

Ensures the truth of an expression involving one or more parameters to the calling method.

Parameters
Name Description
expression boolean

a boolean expression

errorMessage Object

the exception message to use if the check fails; will be converted to a string using String#valueOf(Object)

checkArgument(boolean expression, String errorMessageTemplate, Object[] errorMessageArgs)

public static void checkArgument(boolean expression, String errorMessageTemplate, Object[] errorMessageArgs)

Ensures the truth of an expression involving one or more parameters to the calling method.

Parameters
Name Description
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 Object[]

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

checkState(boolean expression)

public static void checkState(boolean expression)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Parameter
Name Description
expression boolean

a boolean expression

checkState(boolean expression, Object errorMessage)

public static void checkState(boolean expression, Object errorMessage)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Parameters
Name Description
expression boolean

a boolean expression

errorMessage Object

the exception message to use if the check fails; will be converted to a string using String#valueOf(Object)

checkState(boolean expression, String errorMessageTemplate, Object[] errorMessageArgs)

public static void checkState(boolean expression, String errorMessageTemplate, Object[] errorMessageArgs)

Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

Parameters
Name Description
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 Object[]

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