@UnstableApi
public final class Assertions


Provides methods for asserting the truth of expressions and properties.

Summary

Public methods

static void
@Pure
checkArgument(boolean expression)

Throws IllegalArgumentException if expression evaluates to false.

static void
@Pure
checkArgument(boolean expression, Object errorMessage)

Throws IllegalArgumentException if expression evaluates to false.

static int
@Pure
checkIndex(int index, int start, int limit)

Throws IndexOutOfBoundsException if index falls outside the specified bounds.

static void

Throws IllegalStateException if the calling thread is not the application's main thread.

static String
@EnsuresNonNull(value = [""#1""])
@Pure
checkNotEmpty(@Nullable String string)

Throws IllegalArgumentException if string is null or zero length.

static String
@EnsuresNonNull(value = [""#1""])
@Pure
checkNotEmpty(@Nullable String string, Object errorMessage)

Throws IllegalArgumentException if string is null or zero length.

static T
@EnsuresNonNull(value = [""#1""])
@Pure
<T> checkNotNull(@Nullable T reference)

Throws NullPointerException if reference is null.

static T
@EnsuresNonNull(value = [""#1""])
@Pure
<T> checkNotNull(@Nullable T reference, Object errorMessage)

Throws NullPointerException if reference is null.

static void
@Pure
checkState(boolean expression)

Throws IllegalStateException if expression evaluates to false.

static void
@Pure
checkState(boolean expression, Object errorMessage)

Throws IllegalStateException if expression evaluates to false.

static T
@EnsuresNonNull(value = [""#1""])
@Pure
<T> checkStateNotNull(@Nullable T reference)

Throws IllegalStateException if reference is null.

static T
@EnsuresNonNull(value = [""#1""])
@Pure
<T> checkStateNotNull(@Nullable T reference, Object errorMessage)

Throws IllegalStateException if reference is null.

Public methods

checkArgument

@Pure
public static void checkArgument(boolean expression)

Throws IllegalArgumentException if expression evaluates to false.

Parameters
boolean expression

The expression to evaluate.

Throws
java.lang.IllegalArgumentException

If expression is false.

checkArgument

@Pure
public static void checkArgument(boolean expression, Object errorMessage)

Throws IllegalArgumentException if expression evaluates to false.

Parameters
boolean expression

The expression to evaluate.

Object errorMessage

The exception message if an exception is thrown. The message is converted to a String using valueOf.

Throws
java.lang.IllegalArgumentException

If expression is false.

checkIndex

@Pure
public static int checkIndex(int index, int start, int limit)

Throws IndexOutOfBoundsException if index falls outside the specified bounds.

Parameters
int index

The index to test.

int start

The start of the allowed range (inclusive).

int limit

The end of the allowed range (exclusive).

Returns
int

The index that was validated.

Throws
java.lang.IndexOutOfBoundsException

If index falls outside the specified bounds.

checkMainThread

@Pure
public static void checkMainThread()

Throws IllegalStateException if the calling thread is not the application's main thread.

Throws
java.lang.IllegalStateException

If the calling thread is not the application's main thread.

checkNotEmpty

@EnsuresNonNull(value = [""#1""])
@Pure
public static String checkNotEmpty(@Nullable String string)

Throws IllegalArgumentException if string is null or zero length.

Parameters
@Nullable String string

The string to check.

Returns
String

The non-null, non-empty string that was validated.

Throws
java.lang.IllegalArgumentException

If string is null or 0-length.

checkNotEmpty

@EnsuresNonNull(value = [""#1""])
@Pure
public static String checkNotEmpty(@Nullable String string, Object errorMessage)

Throws IllegalArgumentException if string is null or zero length.

Parameters
@Nullable String string

The string to check.

Object errorMessage

The exception message to use if the check fails. The message is converted to a string using valueOf.

Returns
String

The non-null, non-empty string that was validated.

Throws
java.lang.IllegalArgumentException

If string is null or 0-length.

checkNotNull

@EnsuresNonNull(value = [""#1""])
@Pure
public static T <T> checkNotNull(@Nullable T reference)

Throws NullPointerException if reference is null.

Parameters
<T>

The type of the reference.

@Nullable T reference

The reference.

Returns
T

The non-null reference that was validated.

Throws
java.lang.NullPointerException

If reference is null.

checkNotNull

@EnsuresNonNull(value = [""#1""])
@Pure
public static T <T> checkNotNull(@Nullable T reference, Object errorMessage)

Throws NullPointerException if reference is null.

Parameters
<T>

The type of the reference.

@Nullable T reference

The reference.

Object errorMessage

The exception message to use if the check fails. The message is converted to a string using valueOf.

Returns
T

The non-null reference that was validated.

Throws
java.lang.NullPointerException

If reference is null.

checkState

@Pure
public static void checkState(boolean expression)

Throws IllegalStateException if expression evaluates to false.

Parameters
boolean expression

The expression to evaluate.

Throws
java.lang.IllegalStateException

If expression is false.

checkState

@Pure
public static void checkState(boolean expression, Object errorMessage)

Throws IllegalStateException if expression evaluates to false.

Parameters
boolean expression

The expression to evaluate.

Object errorMessage

The exception message if an exception is thrown. The message is converted to a String using valueOf.

Throws
java.lang.IllegalStateException

If expression is false.

checkStateNotNull

@EnsuresNonNull(value = [""#1""])
@Pure
public static T <T> checkStateNotNull(@Nullable T reference)

Throws IllegalStateException if reference is null.

Parameters
<T>

The type of the reference.

@Nullable T reference

The reference.

Returns
T

The non-null reference that was validated.

Throws
java.lang.IllegalStateException

If reference is null.

checkStateNotNull

@EnsuresNonNull(value = [""#1""])
@Pure
public static T <T> checkStateNotNull(@Nullable T reference, Object errorMessage)

Throws IllegalStateException if reference is null.

Parameters
<T>

The type of the reference.

@Nullable T reference

The reference.

Object errorMessage

The exception message to use if the check fails. The message is converted to a string using valueOf.

Returns
T

The non-null reference that was validated.

Throws
java.lang.IllegalStateException

If reference is null.