Class Preconditions


  • @Internal
    public final class Preconditions
    extends Object
    A collection of static utility methods to validate input.

    This class is modelled after Google Guava's Preconditions class, and partly takes code from that class. We add this code to the Flink code base in order to reduce external dependencies.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkArgument​(boolean condition)
      Checks the given boolean condition, and throws an IllegalArgumentException if the condition is not met (evaluates to false).
      static void checkArgument​(boolean condition, Object errorMessage)
      Checks the given boolean condition, and throws an IllegalArgumentException if the condition is not met (evaluates to false).
      static void checkArgument​(boolean condition, String errorMessageTemplate, Object... errorMessageArgs)
      Checks the given boolean condition, and throws an IllegalArgumentException if the condition is not met (evaluates to false).
      static void checkCompletedNormally​(CompletableFuture<?> future)
      Ensures that future has completed normally.
      static void checkElementIndex​(int index, int size)
      Ensures that the given index is valid for an array, list or string of the given size.
      static void checkElementIndex​(int index, int size, String errorMessage)
      Ensures that the given index is valid for an array, list or string of the given size.
      static <T> T checkNotNull​(T reference)
      Ensures that the given object reference is not null.
      static <T> T checkNotNull​(T reference, String errorMessage)
      Ensures that the given object reference is not null.
      static <T> T checkNotNull​(T reference, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures that the given object reference is not null.
      static void checkState​(boolean condition)
      Checks the given boolean condition, and throws an IllegalStateException if the condition is not met (evaluates to false).
      static void checkState​(boolean condition, Object errorMessage)
      Checks the given boolean condition, and throws an IllegalStateException if the condition is not met (evaluates to false).
      static void checkState​(boolean condition, String errorMessageTemplate, Object... errorMessageArgs)
      Checks the given boolean condition, and throws an IllegalStateException if the condition is not met (evaluates to false).
    • Method Detail

      • checkNotNull

        public static <T> T checkNotNull​(@Nullable
                                         T reference)
        Ensures that the given object reference is not null. Upon violation, a NullPointerException with no message is thrown.
        Parameters:
        reference - The object reference
        Returns:
        The object reference itself (generically typed).
        Throws:
        NullPointerException - Thrown, if the passed reference was null.
      • checkNotNull

        public static <T> T checkNotNull​(@Nullable
                                         T reference,
                                         @Nullable
                                         String errorMessage)
        Ensures that the given object reference is not null. Upon violation, a NullPointerException with the given message is thrown.
        Parameters:
        reference - The object reference
        errorMessage - The message for the NullPointerException that is thrown if the check fails.
        Returns:
        The object reference itself (generically typed).
        Throws:
        NullPointerException - Thrown, if the passed reference was null.
      • checkNotNull

        public static <T> T checkNotNull​(T reference,
                                         @Nullable
                                         String errorMessageTemplate,
                                         @Nullable
                                         Object... errorMessageArgs)
        Ensures that the given object reference is not null. Upon violation, a NullPointerException with the given message is thrown.

        The error message is constructed from a template and an arguments array, after a similar fashion as String.format(String, Object...), but supporting only %s as a placeholder.

        Parameters:
        reference - The object reference
        errorMessageTemplate - The message template for the NullPointerException that is thrown if the check fails. The template substitutes its %s placeholders with the error message arguments.
        errorMessageArgs - The arguments for the error message, to be inserted into the message template for the %s placeholders.
        Returns:
        The object reference itself (generically typed).
        Throws:
        NullPointerException - Thrown, if the passed reference was null.
      • checkArgument

        public static void checkArgument​(boolean condition)
        Checks the given boolean condition, and throws an IllegalArgumentException if the condition is not met (evaluates to false).
        Parameters:
        condition - The condition to check
        Throws:
        IllegalArgumentException - Thrown, if the condition is violated.
      • checkArgument

        public static void checkArgument​(boolean condition,
                                         @Nullable
                                         Object errorMessage)
        Checks the given boolean condition, and throws an IllegalArgumentException if the condition is not met (evaluates to false). The exception will have the given error message.
        Parameters:
        condition - The condition to check
        errorMessage - The message for the IllegalArgumentException that is thrown if the check fails.
        Throws:
        IllegalArgumentException - Thrown, if the condition is violated.
      • checkArgument

        public static void checkArgument​(boolean condition,
                                         @Nullable
                                         String errorMessageTemplate,
                                         @Nullable
                                         Object... errorMessageArgs)
        Checks the given boolean condition, and throws an IllegalArgumentException if the condition is not met (evaluates to false).
        Parameters:
        condition - The condition to check
        errorMessageTemplate - The message template for the IllegalArgumentException that is thrown if the check fails. The template substitutes its %s placeholders with the error message arguments.
        errorMessageArgs - The arguments for the error message, to be inserted into the message template for the %s placeholders.
        Throws:
        IllegalArgumentException - Thrown, if the condition is violated.
      • checkState

        public static void checkState​(boolean condition)
        Checks the given boolean condition, and throws an IllegalStateException if the condition is not met (evaluates to false).
        Parameters:
        condition - The condition to check
        Throws:
        IllegalStateException - Thrown, if the condition is violated.
      • checkState

        public static void checkState​(boolean condition,
                                      @Nullable
                                      Object errorMessage)
        Checks the given boolean condition, and throws an IllegalStateException if the condition is not met (evaluates to false). The exception will have the given error message.
        Parameters:
        condition - The condition to check
        errorMessage - The message for the IllegalStateException that is thrown if the check fails.
        Throws:
        IllegalStateException - Thrown, if the condition is violated.
      • checkState

        public static void checkState​(boolean condition,
                                      @Nullable
                                      String errorMessageTemplate,
                                      @Nullable
                                      Object... errorMessageArgs)
        Checks the given boolean condition, and throws an IllegalStateException if the condition is not met (evaluates to false).
        Parameters:
        condition - The condition to check
        errorMessageTemplate - The message template for the IllegalStateException that is thrown if the check fails. The template substitutes its %s placeholders with the error message arguments.
        errorMessageArgs - The arguments for the error message, to be inserted into the message template for the %s placeholders.
        Throws:
        IllegalStateException - Thrown, if the condition is violated.
      • checkElementIndex

        public static void checkElementIndex​(int index,
                                             int size)
        Ensures that the given index is valid for an array, list or string of the given size.
        Parameters:
        index - index to check
        size - size of the array, list or string
        Throws:
        IllegalArgumentException - Thrown, if size is negative.
        IndexOutOfBoundsException - Thrown, if the index negative or greater than or equal to size
      • checkElementIndex

        public static void checkElementIndex​(int index,
                                             int size,
                                             @Nullable
                                             String errorMessage)
        Ensures that the given index is valid for an array, list or string of the given size.
        Parameters:
        index - index to check
        size - size of the array, list or string
        errorMessage - The message for the IndexOutOfBoundsException that is thrown if the check fails.
        Throws:
        IllegalArgumentException - Thrown, if size is negative.
        IndexOutOfBoundsException - Thrown, if the index negative or greater than or equal to size
      • checkCompletedNormally

        public static void checkCompletedNormally​(CompletableFuture<?> future)
        Ensures that future has completed normally.
        Throws:
        IllegalStateException - Thrown, if future has not completed or it has completed exceptionally.