public final class Assertions extends Object
Modifier and Type | Method and Description |
---|---|
static void |
checkArgument(boolean expression)
Throws
IllegalArgumentException if expression evaluates to false. |
static void |
checkArgument(boolean expression,
Object errorMessage)
Throws
IllegalArgumentException if expression evaluates to false. |
static int |
checkIndex(int index,
int start,
int limit)
Throws
IndexOutOfBoundsException if index falls outside the specified bounds. |
static void |
checkMainThread()
Throws
IllegalStateException if the calling thread is not the application's main
thread. |
static String |
checkNotEmpty(String string)
Throws
IllegalArgumentException if string is null or zero length. |
static String |
checkNotEmpty(String string,
Object errorMessage)
Throws
IllegalArgumentException if string is null or zero length. |
static <T> T |
checkNotNull(T reference)
Throws
NullPointerException if reference is null. |
static <T> T |
checkNotNull(T reference,
Object errorMessage)
Throws
NullPointerException if reference is null. |
static void |
checkState(boolean expression)
Throws
IllegalStateException if expression evaluates to false. |
static void |
checkState(boolean expression,
Object errorMessage)
Throws
IllegalStateException if expression evaluates to false. |
public static void checkArgument(boolean expression)
IllegalArgumentException
if expression
evaluates to false.expression
- The expression to evaluate.IllegalArgumentException
- If expression
is false.public static void checkArgument(boolean expression, Object errorMessage)
IllegalArgumentException
if expression
evaluates to false.expression
- The expression to evaluate.errorMessage
- The exception message if an exception is thrown. The message is converted
to a String
using String.valueOf(Object)
.IllegalArgumentException
- If expression
is false.public static int checkIndex(int index, int start, int limit)
IndexOutOfBoundsException
if index
falls outside the specified bounds.index
- The index to test.start
- The start of the allowed range (inclusive).limit
- The end of the allowed range (exclusive).index
that was validated.IndexOutOfBoundsException
- If index
falls outside the specified bounds.public static void checkState(boolean expression)
IllegalStateException
if expression
evaluates to false.expression
- The expression to evaluate.IllegalStateException
- If expression
is false.public static void checkState(boolean expression, Object errorMessage)
IllegalStateException
if expression
evaluates to false.expression
- The expression to evaluate.errorMessage
- The exception message if an exception is thrown. The message is converted
to a String
using String.valueOf(Object)
.IllegalStateException
- If expression
is false.public static <T> T checkNotNull(T reference)
NullPointerException
if reference
is null.T
- The type of the reference.reference
- The reference.NullPointerException
- If reference
is null.public static <T> T checkNotNull(T reference, Object errorMessage)
NullPointerException
if reference
is null.T
- The type of the reference.reference
- The reference.errorMessage
- The exception message to use if the check fails. The message is converted
to a string using String.valueOf(Object)
.NullPointerException
- If reference
is null.public static String checkNotEmpty(String string)
IllegalArgumentException
if string
is null or zero length.string
- The string to check.IllegalArgumentException
- If string
is null or 0-length.public static String checkNotEmpty(String string, Object errorMessage)
IllegalArgumentException
if string
is null or zero length.string
- The string to check.errorMessage
- The exception message to use if the check fails. The message is converted
to a string using String.valueOf(Object)
.IllegalArgumentException
- If string
is null or 0-length.public static void checkMainThread()
IllegalStateException
if the calling thread is not the application's main
thread.IllegalStateException
- If the calling thread is not the application's main thread.