# Class Preconditions (2.1.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [2.1.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.util.Preconditions)
- [2.0.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/2.0.3/com.google.api.client.util.Preconditions)
- [1.47.1](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.47.1/com.google.api.client.util.Preconditions)
- [1.46.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.46.3/com.google.api.client.util.Preconditions)
- [1.45.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.45.3/com.google.api.client.util.Preconditions)
- [1.44.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.44.2/com.google.api.client.util.Preconditions)
- [1.43.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.43.2/com.google.api.client.util.Preconditions)
- [1.42.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.42.3/com.google.api.client.util.Preconditions)
- [1.41.8](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.41.8/com.google.api.client.util.Preconditions)

    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](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> Preconditions

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## 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` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html` 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` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` 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` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html[]` 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` a boolean expression |
| `errorMessage` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html` 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` a boolean expression |
| `errorMessageTemplate` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` 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` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html[]` 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` a boolean expression |
| `errorMessage` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html` 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` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` a boolean expression |
| `errorMessageTemplate` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` 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` | `https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html[]` the arguments to be substituted into the message template. Arguments are converted to strings using String#valueOf(Object). |