Class Assertions
java.lang.Object
com.castlabs.sdk.base.subtitles.utilities.Assertions
Provides methods for asserting the truth of expressions and properties.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckArgument(boolean expression) ThrowsIllegalArgumentExceptionifexpressionevaluates to false.static voidcheckArgument(boolean expression, Object errorMessage) ThrowsIllegalArgumentExceptionifexpressionevaluates to false.static intcheckIndex(int index, int start, int limit) ThrowsIndexOutOfBoundsExceptionifindexfalls outside the specified bounds.static voidThrowsIllegalStateExceptionif the calling thread is not the application's main thread.static StringcheckNotEmpty(String string) ThrowsIllegalArgumentExceptionifstringis null or zero length.static StringcheckNotEmpty(String string, Object errorMessage) ThrowsIllegalArgumentExceptionifstringis null or zero length.static <T> TcheckNotNull(T reference) ThrowsNullPointerExceptionifreferenceis null.static <T> TcheckNotNull(T reference, Object errorMessage) ThrowsNullPointerExceptionifreferenceis null.static voidcheckState(boolean expression) ThrowsIllegalStateExceptionifexpressionevaluates to false.static voidcheckState(boolean expression, Object errorMessage) ThrowsIllegalStateExceptionifexpressionevaluates to false.static <T> TcheckStateNotNull(T reference) ThrowsIllegalStateExceptionifreferenceis null.static <T> TcheckStateNotNull(T reference, Object errorMessage) ThrowsIllegalStateExceptionifreferenceis null.
-
Method Details
-
checkArgument
public static void checkArgument(boolean expression) ThrowsIllegalArgumentExceptionifexpressionevaluates to false.- Parameters:
expression- The expression to evaluate.- Throws:
IllegalArgumentException- Ifexpressionis false.
-
checkArgument
ThrowsIllegalArgumentExceptionifexpressionevaluates to false.- Parameters:
expression- The expression to evaluate.errorMessage- The exception message if an exception is thrown. The message is converted to aStringusingString.valueOf(Object).- Throws:
IllegalArgumentException- Ifexpressionis false.
-
checkIndex
public static int checkIndex(int index, int start, int limit) ThrowsIndexOutOfBoundsExceptionifindexfalls 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
indexthat was validated. - Throws:
IndexOutOfBoundsException- Ifindexfalls outside the specified bounds.
-
checkState
public static void checkState(boolean expression) ThrowsIllegalStateExceptionifexpressionevaluates to false.- Parameters:
expression- The expression to evaluate.- Throws:
IllegalStateException- Ifexpressionis false.
-
checkState
ThrowsIllegalStateExceptionifexpressionevaluates to false.- Parameters:
expression- The expression to evaluate.errorMessage- The exception message if an exception is thrown. The message is converted to aStringusingString.valueOf(Object).- Throws:
IllegalStateException- Ifexpressionis false.
-
checkStateNotNull
@EnsuresNonNull("#1") public static <T> T checkStateNotNull(@Nullable T reference) ThrowsIllegalStateExceptionifreferenceis null.- Type Parameters:
T- The type of the reference.- Parameters:
reference- The reference.- Returns:
- The non-null reference that was validated.
- Throws:
IllegalStateException- Ifreferenceis null.
-
checkStateNotNull
@EnsuresNonNull("#1") public static <T> T checkStateNotNull(@Nullable T reference, Object errorMessage) ThrowsIllegalStateExceptionifreferenceis 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- Ifreferenceis null.
-
checkNotNull
@EnsuresNonNull("#1") public static <T> T checkNotNull(@Nullable T reference) ThrowsNullPointerExceptionifreferenceis null.- Type Parameters:
T- The type of the reference.- Parameters:
reference- The reference.- Returns:
- The non-null reference that was validated.
- Throws:
NullPointerException- Ifreferenceis null.
-
checkNotNull
ThrowsNullPointerExceptionifreferenceis 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- Ifreferenceis null.
-
checkNotEmpty
ThrowsIllegalArgumentExceptionifstringis null or zero length.- Parameters:
string- The string to check.- Returns:
- The non-null, non-empty string that was validated.
- Throws:
IllegalArgumentException- Ifstringis null or 0-length.
-
checkNotEmpty
@EnsuresNonNull("#1") public static String checkNotEmpty(@Nullable String string, Object errorMessage) ThrowsIllegalArgumentExceptionifstringis 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- Ifstringis null or 0-length.
-
checkMainThread
public static void checkMainThread()ThrowsIllegalStateExceptionif the calling thread is not the application's main thread.- Throws:
IllegalStateException- If the calling thread is not the application's main thread.
-