Package com.gengoai

Class Validation


  • public final class Validation
    extends Object

    Convenience methods for validating method arguments.

    Author:
    David B. Bracewell
    • Method Detail

      • checkArgument

        public static void checkArgument​(boolean evaluation)
        Throws a IllegalArgumentException if the given boolean evaluates to false.
        Parameters:
        evaluation - the object to check
      • checkArgument

        public static void checkArgument​(boolean evaluation,
                                         String message)
        Throws a IllegalArgumentException if the given boolean evaluates to false.
        Parameters:
        evaluation - the object to check
        message - the message to use in the IllegalArgumentException
      • checkArgument

        public static void checkArgument​(boolean evaluation,
                                         @NonNull
                                         @NonNull Supplier<String> message)
        Check argument.
        Parameters:
        evaluation - the evaluation
        message - the message
      • checkArgumentIsInstanceOf

        public static void checkArgumentIsInstanceOf​(Object p,
                                                     Type... types)
      • checkElementIndex

        public static int checkElementIndex​(int index,
                                            int size)
        Checks that the given index is in the range [0, size)
        Parameters:
        index - the index to validate
        size - the size of the array, list, etc.
        Returns:
        the index
        Throws:
        IndexOutOfBoundsException - if index is negative or greater than equal to size
        IllegalArgumentException - if size is negative
      • checkElementIndex

        public static int checkElementIndex​(int index,
                                            int size,
                                            String message)
        Check element index int.
        Parameters:
        index - the index
        size - the size
        message - the message
        Returns:
        the int
      • checkElementIndex

        public static int checkElementIndex​(int index,
                                            int size,
                                            Supplier<String> message)
        Checks that the given index is in the range [0, size)
        Parameters:
        index - the index to validate
        size - the size of the array, list, etc.
        message - Message to prepend to the exception
        Returns:
        the index
        Throws:
        IndexOutOfBoundsException - if index is negative or greater than equal to size
        IllegalArgumentException - if size is negative
      • checkPositionIndex

        public static int checkPositionIndex​(int index,
                                             int size,
                                             String message)
        Checks that the given index is in the range [0, size]
        Parameters:
        index - the index to validate
        size - the size of the array, list, etc.
        message - Message to prepend to the exception
        Returns:
        the index
        Throws:
        IndexOutOfBoundsException - if index is negative or greater than size
        IllegalArgumentException - if size is negative
      • checkPositionIndex

        public static int checkPositionIndex​(int index,
                                             int size)
        Checks that the given index is in the range [0, size]
        Parameters:
        index - the index to validate
        size - the size of the array, list, etc.
        Returns:
        the index
        Throws:
        IndexOutOfBoundsException - if index is negative or greater than size
        IllegalArgumentException - if size is negative
      • checkState

        public static void checkState​(boolean evaluation)
        Throws a IllegalStateException if the given boolean evaluates to false.
        Parameters:
        evaluation - the object to check
      • checkState

        public static void checkState​(boolean evaluation,
                                      String message)
        Throws a IllegalStateException if the given boolean evaluates to false.
        Parameters:
        evaluation - the object to check
        message - the message to use in the IllegalStateException
      • checkState

        public static void checkState​(boolean evaluation,
                                      Supplier<String> messageSupplier)
        Check state.
        Parameters:
        evaluation - the evaluation
        messageSupplier - the message supplier
      • notNull

        public static <T> T notNull​(T object)
        Throws a NullPointerException if the given object is null.
        Type Parameters:
        T - the type of the given object
        Parameters:
        object - the object to check
        Returns:
        the object
      • notNull

        public static <T> T notNull​(T object,
                                    String message)
        Throws a NullPointerException if the given object is null.
        Type Parameters:
        T - the type of the given object
        Parameters:
        object - the object to check
        message - the message to use in the NullPointerException
        Returns:
        the object
      • notNullOrBlank

        public static String notNullOrBlank​(String string)
        Throws a IllegalArgumentException if the given string is null or blank.
        Parameters:
        string - the string to check
        Returns:
        the object
      • notNullOrBlank

        public static String notNullOrBlank​(String string,
                                            String message)
        Throws a IllegalArgumentException if the given string is null or blank.
        Parameters:
        string - the string to check
        message - the message to use in the IllegalArgumentException
        Returns:
        the object
      • notNullOrBlank

        public static String notNullOrBlank​(String string,
                                            @NonNull
                                            @NonNull Supplier<String> supplier)
        Throws a IllegalArgumentException if the given string is null or blank.
        Parameters:
        string - the string to check
        supplier - the message supplier to use in the IllegalArgumentException
        Returns:
        the object
      • validate

        public static void validate​(boolean evaluation,
                                    @NonNull
                                    @NonNull Supplier<RuntimeException> exceptionSupplier)
        Checks that the given evaluation or true and if it is not generates a RuntimeException using the given Supplier and throws it.
        Parameters:
        evaluation - the evaluation to test
        exceptionSupplier - the supplier providing the exception to throw.
      • validate

        public static <T> T validate​(boolean evaluation,
                                     @NonNull
                                     @NonNull Supplier<RuntimeException> exceptionSupplier,
                                     T returnValue)
        Checks that the evaluation is true. If evaluation is true, the given return value is returned, otherwise the exception Supplier is used to generate a RuntimeException to throw.
        Type Parameters:
        T - the return type parameter
        Parameters:
        evaluation - the evaluation test
        exceptionSupplier - the supplier to use to generate exceptions (null will cause a default RuntimeException to be thrown)
        returnValue - the return value to return if the evaluation is true
        Returns:
        the return value
      • validate

        public static <T> T validate​(T value,
                                     @NonNull
                                     @NonNull Predicate<T> evaluator,
                                     @NonNull
                                     @NonNull Supplier<RuntimeException> exceptionSupplier,
                                     boolean nullable)
        Uses the given Predicate to evaluate the given value. If evaluation is true, the given value is returned, otherwise the exception supplier is used to generate a RuntimeException to throw. When the value is null and it is not allowed to be nullable a NullPointerException wil be thrown.
        Type Parameters:
        T - the return type parameter
        Parameters:
        value - the value to test
        evaluator - the predicate to use to test the value
        exceptionSupplier - the supplier to use to generate exceptions
        nullable - True the value is able to be null, False it cannot be null
        Returns:
        the given value
      • validateArg

        public static <T> T validateArg​(T value,
                                        @NonNull
                                        @NonNull Predicate<T> predicate,
                                        String message,
                                        boolean nullable)
        Checks that the evaluation is true. If evaluation is true, the given value is returned, otherwise the exception supplier is used to generate an IllegalArgumentException to throw.
        Type Parameters:
        T - the return type parameter
        Parameters:
        value - the value to test
        predicate - the predicate
        message - the message to use when creating the runtime exception
        nullable - True the value is able to be null, False it cannot be null
        Returns:
        the given value