Package dev.hypera.chameleon.util
Class Preconditions
- java.lang.Object
-
- dev.hypera.chameleon.util.Preconditions
-
public final class Preconditions extends Object
Preconditions, a set of convenience methods to aid in validating method or constructor invocation.We use JetBrains Annotations for nullability indication, however this makes everything a lot stricter so that Chameleon does not end up in an illegal state.
Heavily inspired by Google Guava's Preconditions.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcheckArgument(boolean expression)Ensures the expression istrueto validate an argument.static voidcheckArgument(boolean expression, @NotNull String message)Ensures the expression istrueto validate an argument.static voidcheckArgument(boolean expression, @NotNull String messageFormat, @NotNull Object... messageArgs)Ensures the expression istrueto validate an argument.static @NotNull StringcheckMatches(@NotNull String name, @NotNull Pattern regex, @Nullable String value)Ensures the givenvaluematches the given pattern.static <T> @NotNull Collection<T>checkNoneNull(@NotNull String name, @Nullable Collection<T> value)Ensures the givenvaluedoes not contain null.static <T> TcheckNotNull(@NotNull String name, T value)Ensures the givenvalueis not null.static <T> TcheckNotNull(T value)Ensures the givenvalueis not null.static @NotNull StringcheckNotNullOrEmpty(@NotNull String name, @Nullable String value)Ensures the givenvalueis not null or empty.static @NotNull StringcheckNotNullOrEmpty(@Nullable String value)Ensures the givenvalueis not null or empty.static <T> TcheckNotNullState(@NotNull String name, T value)Ensures the givenvalueis not null.static voidcheckState(boolean expression)Ensures that expression istrueto validate a state.static voidcheckState(boolean expression, @NotNull String message)Ensures the expression istrueto validate a state.static voidcheckState(boolean expression, @NotNull String messageFormat, @NotNull Object... messageArgs)Ensures the expression istrueto validate a state.
-
-
-
Method Detail
-
checkArgument
@Contract("false -> fail") public static void checkArgument(boolean expression)Ensures the expression istrueto validate an argument.- Parameters:
expression- Expression.- Throws:
IllegalArgumentException- ifexpressionisfalse.
-
checkArgument
@Contract("false, _ -> fail") public static void checkArgument(boolean expression, @NotNull @NotNull String message)Ensures the expression istrueto validate an argument.- Parameters:
expression- Expression.message- The exception message to use if check fails.- Throws:
IllegalArgumentException- ifexpressionisfalse.
-
checkArgument
@Contract("false, _, _ -> fail") public static void checkArgument(boolean expression, @NotNull @NotNull String messageFormat, @NotNull @NotNull Object... messageArgs)Ensures the expression istrueto validate an argument.- Parameters:
expression- Expression.messageFormat- The exception message format to use if check fails.messageArgs- The arguments to use with the message format if the check fails.- Throws:
IllegalArgumentException- ifexpressionisfalse.
-
checkState
@Contract("false -> fail") public static void checkState(boolean expression)Ensures that expression istrueto validate a state.- Parameters:
expression- Expression.- Throws:
IllegalStateException- ifexpressionisfalse.
-
checkState
@Contract("false, _ -> fail") public static void checkState(boolean expression, @NotNull @NotNull String message)Ensures the expression istrueto validate a state.- Parameters:
expression- Expression.message- The exception message to use if check fails.- Throws:
IllegalStateException- ifexpressionisfalse.
-
checkState
@Contract("false, _, _ -> fail") public static void checkState(boolean expression, @NotNull @NotNull String messageFormat, @NotNull @NotNull Object... messageArgs)Ensures the expression istrueto validate a state.- Parameters:
expression- Expression.messageFormat- The exception message format to use if check fails.messageArgs- The arguments to use with the message format if the check fails.- Throws:
IllegalStateException- ifexpressionisfalse.
-
checkNotNull
@Contract("!null -> param1; null -> fail") @NotNull public static <T> T checkNotNull(@Nullable T value)Ensures the givenvalueis not null.- Type Parameters:
T- Value type.- Parameters:
value- Argument value.- Returns:
value- Throws:
NullPointerException- ifvalueisnull.
-
checkNotNull
@Contract("_, !null -> param2; _, null -> fail") @NotNull public static <T> T checkNotNull(@NotNull @NotNull String name, @Nullable T value)Ensures the givenvalueis not null.- Type Parameters:
T- Value type.- Parameters:
name- Argument name, used in the exception message ifvalueis null.value- Argument value.- Returns:
value.- Throws:
IllegalArgumentException- ifvalueisnull.
-
checkNotNullState
@Contract("_, !null -> param2; _, null -> fail") @NotNull public static <T> T checkNotNullState(@NotNull @NotNull String name, @Nullable T value)Ensures the givenvalueis not null.- Type Parameters:
T- Value type.- Parameters:
name- Argument name, used in the exception message ifvalueis null.value- Argument value.- Returns:
value.- Throws:
IllegalStateException- ifvalueisnull.
-
checkNotNullOrEmpty
@Contract("!null -> param1; null -> fail") @NotNull public static @NotNull String checkNotNullOrEmpty(@Nullable @Nullable String value)Ensures the givenvalueis not null or empty.- Parameters:
value- Argument value.- Returns:
value.- Throws:
IllegalArgumentException- ifvalueisnullor empty.
-
checkNotNullOrEmpty
@Contract("_, !null -> param2; _, null -> fail") @NotNull public static @NotNull String checkNotNullOrEmpty(@NotNull @NotNull String name, @Nullable @Nullable String value)Ensures the givenvalueis not null or empty.- Parameters:
name- Argument name.value- Argument value.- Returns:
value.- Throws:
IllegalArgumentException- ifvalueisnullor empty.
-
checkNoneNull
@Contract("_, !null -> param2; _, null -> fail") @NotNull public static <T> @NotNull Collection<T> checkNoneNull(@NotNull @NotNull String name, @Nullable @Nullable Collection<T> value)Ensures the givenvaluedoes not contain null.- Type Parameters:
T- Value type.- Parameters:
name- Argument name, used in the exception message ifvaluecontains null.value- Argument value.- Returns:
value.- Throws:
IllegalArgumentException- ifvaluecontainsnull.
-
checkMatches
@Contract("_, _, null -> fail; _, _, _ -> param3") @NotNull public static @NotNull String checkMatches(@NotNull @NotNull String name, @NotNull @NotNull Pattern regex, @Nullable @Nullable String value)Ensures the givenvaluematches the given pattern.- Parameters:
name- Argument name.regex- RegEx pattern to match against.value- Argument value.- Returns:
value.- Throws:
IllegalArgumentException- if the givenvaluedoes not match the given pattern.
-
-