Class Preconditions (1.41.8)

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
NameDescription
referenceT

an object reference

Returns
TypeDescription
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
NameDescription
referenceT

an object reference

errorMessageObject

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

Returns
TypeDescription
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
NameDescription
referenceT

an object reference

errorMessageTemplateString

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.

errorMessageArgsObject[]

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

Returns
TypeDescription
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
NameDescription
expressionboolean

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
NameDescription
expressionboolean

a boolean expression

errorMessageObject

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
NameDescription
expressionboolean

a boolean expression

errorMessageTemplateString

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.

errorMessageArgsObject[]

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
NameDescription
expressionboolean

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
NameDescription
expressionboolean

a boolean expression

errorMessageObject

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
NameDescription
expressionboolean

a boolean expression

errorMessageTemplateString

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.

errorMessageArgsObject[]

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