Class Assertions
- java.lang.Object
-
- com.google.android.exoplayer2.util.Assertions
-
public final class Assertions extends Object
Provides methods for asserting the truth of expressions and properties.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
checkArgument(boolean expression)
ThrowsIllegalArgumentException
ifexpression
evaluates to false.static void
checkArgument(boolean expression, Object errorMessage)
ThrowsIllegalArgumentException
ifexpression
evaluates to false.static int
checkIndex(int index, int start, int limit)
ThrowsIndexOutOfBoundsException
ifindex
falls outside the specified bounds.static void
checkMainThread()
ThrowsIllegalStateException
if the calling thread is not the application's main thread.static String
checkNotEmpty(String string)
ThrowsIllegalArgumentException
ifstring
is null or zero length.static String
checkNotEmpty(String string, Object errorMessage)
ThrowsIllegalArgumentException
ifstring
is null or zero length.static <T> T
checkNotNull(T reference)
ThrowsNullPointerException
ifreference
is null.static <T> T
checkNotNull(T reference, Object errorMessage)
ThrowsNullPointerException
ifreference
is null.static void
checkState(boolean expression)
ThrowsIllegalStateException
ifexpression
evaluates to false.static void
checkState(boolean expression, Object errorMessage)
ThrowsIllegalStateException
ifexpression
evaluates to false.static <T> T
checkStateNotNull(T reference)
ThrowsIllegalStateException
ifreference
is null.static <T> T
checkStateNotNull(T reference, Object errorMessage)
ThrowsIllegalStateException
ifreference
is null.
-
-
-
Method Detail
-
checkArgument
@Pure public static void checkArgument(boolean expression)
ThrowsIllegalArgumentException
ifexpression
evaluates to false.- Parameters:
expression
- The expression to evaluate.- Throws:
IllegalArgumentException
- Ifexpression
is false.
-
checkArgument
@Pure public static void checkArgument(boolean expression, Object errorMessage)
ThrowsIllegalArgumentException
ifexpression
evaluates to false.- Parameters:
expression
- The expression to evaluate.errorMessage
- The exception message if an exception is thrown. The message is converted to aString
usingString.valueOf(Object)
.- Throws:
IllegalArgumentException
- Ifexpression
is false.
-
checkIndex
@Pure public static int checkIndex(int index, int start, int limit)
ThrowsIndexOutOfBoundsException
ifindex
falls outside the specified bounds.- Parameters:
index
- The index to test.start
- The start of the allowed range (inclusive).limit
- The end of the allowed range (exclusive).- Returns:
- The
index
that was validated. - Throws:
IndexOutOfBoundsException
- Ifindex
falls outside the specified bounds.
-
checkState
@Pure public static void checkState(boolean expression)
ThrowsIllegalStateException
ifexpression
evaluates to false.- Parameters:
expression
- The expression to evaluate.- Throws:
IllegalStateException
- Ifexpression
is false.
-
checkState
@Pure public static void checkState(boolean expression, Object errorMessage)
ThrowsIllegalStateException
ifexpression
evaluates to false.- Parameters:
expression
- The expression to evaluate.errorMessage
- The exception message if an exception is thrown. The message is converted to aString
usingString.valueOf(Object)
.- Throws:
IllegalStateException
- Ifexpression
is false.
-
checkStateNotNull
@EnsuresNonNull("#1") @Pure public static <T> T checkStateNotNull(@Nullable T reference)
ThrowsIllegalStateException
ifreference
is null.- Type Parameters:
T
- The type of the reference.- Parameters:
reference
- The reference.- Returns:
- The non-null reference that was validated.
- Throws:
IllegalStateException
- Ifreference
is null.
-
checkStateNotNull
@EnsuresNonNull("#1") @Pure public static <T> T checkStateNotNull(@Nullable T reference, Object errorMessage)
ThrowsIllegalStateException
ifreference
is null.- Type Parameters:
T
- The type of the reference.- Parameters:
reference
- The reference.errorMessage
- The exception message to use if the check fails. The message is converted to a string usingString.valueOf(Object)
.- Returns:
- The non-null reference that was validated.
- Throws:
IllegalStateException
- Ifreference
is null.
-
checkNotNull
@EnsuresNonNull("#1") @Pure public static <T> T checkNotNull(@Nullable T reference)
ThrowsNullPointerException
ifreference
is null.- Type Parameters:
T
- The type of the reference.- Parameters:
reference
- The reference.- Returns:
- The non-null reference that was validated.
- Throws:
NullPointerException
- Ifreference
is null.
-
checkNotNull
@EnsuresNonNull("#1") @Pure public static <T> T checkNotNull(@Nullable T reference, Object errorMessage)
ThrowsNullPointerException
ifreference
is null.- Type Parameters:
T
- The type of the reference.- Parameters:
reference
- The reference.errorMessage
- The exception message to use if the check fails. The message is converted to a string usingString.valueOf(Object)
.- Returns:
- The non-null reference that was validated.
- Throws:
NullPointerException
- Ifreference
is null.
-
checkNotEmpty
@EnsuresNonNull("#1") @Pure public static String checkNotEmpty(@Nullable String string)
ThrowsIllegalArgumentException
ifstring
is null or zero length.- Parameters:
string
- The string to check.- Returns:
- The non-null, non-empty string that was validated.
- Throws:
IllegalArgumentException
- Ifstring
is null or 0-length.
-
checkNotEmpty
@EnsuresNonNull("#1") @Pure public static String checkNotEmpty(@Nullable String string, Object errorMessage)
ThrowsIllegalArgumentException
ifstring
is null or zero length.- Parameters:
string
- The string to check.errorMessage
- The exception message to use if the check fails. The message is converted to a string usingString.valueOf(Object)
.- Returns:
- The non-null, non-empty string that was validated.
- Throws:
IllegalArgumentException
- Ifstring
is null or 0-length.
-
checkMainThread
@Pure public static void checkMainThread()
ThrowsIllegalStateException
if the calling thread is not the application's main thread.- Throws:
IllegalStateException
- If the calling thread is not the application's main thread.
-
-