Class 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 Detail

      • checkArgument

        @Contract("false -> fail")
        public static void checkArgument​(boolean expression)
        Ensures the expression is true to validate an argument.
        Parameters:
        expression - Expression.
        Throws:
        IllegalArgumentException - if expression is false.
      • checkArgument

        @Contract("false, _ -> fail")
        public static void checkArgument​(boolean expression,
                                         @NotNull
                                         @NotNull String message)
        Ensures the expression is true to validate an argument.
        Parameters:
        expression - Expression.
        message - The exception message to use if check fails.
        Throws:
        IllegalArgumentException - if expression is false.
      • checkArgument

        @Contract("false, _, _ -> fail")
        public static void checkArgument​(boolean expression,
                                         @NotNull
                                         @NotNull String messageFormat,
                                         @NotNull
                                         @NotNull Object... messageArgs)
        Ensures the expression is true to 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 - if expression is false.
      • checkState

        @Contract("false -> fail")
        public static void checkState​(boolean expression)
        Ensures that expression is true to validate a state.
        Parameters:
        expression - Expression.
        Throws:
        IllegalStateException - if expression is false.
      • checkState

        @Contract("false, _ -> fail")
        public static void checkState​(boolean expression,
                                      @NotNull
                                      @NotNull String message)
        Ensures the expression is true to validate a state.
        Parameters:
        expression - Expression.
        message - The exception message to use if check fails.
        Throws:
        IllegalStateException - if expression is false.
      • checkState

        @Contract("false, _, _ -> fail")
        public static void checkState​(boolean expression,
                                      @NotNull
                                      @NotNull String messageFormat,
                                      @NotNull
                                      @NotNull Object... messageArgs)
        Ensures the expression is true to 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 - if expression is false.
      • checkNotNull

        @Contract("!null -> param1; null -> fail")
        @NotNull
        public static <T> T checkNotNull​(@Nullable
                                         T value)
        Ensures the given value is not null.
        Type Parameters:
        T - Value type.
        Parameters:
        value - Argument value.
        Returns:
        value
        Throws:
        NullPointerException - if value is null.
      • checkNotNull

        @Contract("_, !null -> param2; _, null -> fail")
        @NotNull
        public static <T> T checkNotNull​(@NotNull
                                         @NotNull String name,
                                         @Nullable
                                         T value)
        Ensures the given value is not null.
        Type Parameters:
        T - Value type.
        Parameters:
        name - Argument name, used in the exception message if value is null.
        value - Argument value.
        Returns:
        value.
        Throws:
        IllegalArgumentException - if value is null.
      • checkNotNullState

        @Contract("_, !null -> param2; _, null -> fail")
        @NotNull
        public static <T> T checkNotNullState​(@NotNull
                                              @NotNull String name,
                                              @Nullable
                                              T value)
        Ensures the given value is not null.
        Type Parameters:
        T - Value type.
        Parameters:
        name - Argument name, used in the exception message if value is null.
        value - Argument value.
        Returns:
        value.
        Throws:
        IllegalStateException - if value is null.
      • checkNotNullOrEmpty

        @Contract("!null -> param1; null -> fail")
        @NotNull
        public static @NotNull String checkNotNullOrEmpty​(@Nullable
                                                          @Nullable String value)
        Ensures the given value is not null or empty.
        Parameters:
        value - Argument value.
        Returns:
        value.
        Throws:
        IllegalArgumentException - if value is null or empty.
      • checkNotNullOrEmpty

        @Contract("_, !null -> param2; _, null -> fail")
        @NotNull
        public static @NotNull String checkNotNullOrEmpty​(@NotNull
                                                          @NotNull String name,
                                                          @Nullable
                                                          @Nullable String value)
        Ensures the given value is not null or empty.
        Parameters:
        name - Argument name.
        value - Argument value.
        Returns:
        value.
        Throws:
        IllegalArgumentException - if value is null or 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 given value does not contain null.
        Type Parameters:
        T - Value type.
        Parameters:
        name - Argument name, used in the exception message if value contains null.
        value - Argument value.
        Returns:
        value.
        Throws:
        IllegalArgumentException - if value contains null.
      • 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 given value matches the given pattern.
        Parameters:
        name - Argument name.
        regex - RegEx pattern to match against.
        value - Argument value.
        Returns:
        value.
        Throws:
        IllegalArgumentException - if the given value does not match the given pattern.