Class ParameterValidation


  • public class ParameterValidation
    extends java.lang.Object
    A class to validate method parameters. these methods throw IllegalArgumentException is the validation fails.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] doesNotContainNull​(T[] array, java.lang.String name)
      Ensures that the given array does not contain a null value.
      static <T extends java.lang.Iterable<?>>
      T
      doesNotContainNull​(T iterable, java.lang.String name)
      Ensures that the given iterable does not contain a null value.
      static <T extends java.util.Collection<?>>
      T
      isNotEmpty​(T collection, java.lang.String name)
      Ensures that the given cllection is not empty.
      static <T> T nonNull​(T instance, java.lang.String name)
      Validates that the given instance is not null.
      static <T> T nonNullNpe​(T instance, java.lang.String name)
      Validates that the given instance is not null.
      static int positive​(int amount, java.lang.String name)
      Validates that the passed amount is strictly positive.
      static long positive​(long amount, java.lang.String name)
      Validates that the passed amount is strictly positive.
      static int positiveOrZero​(int amount, java.lang.String name)
      Validates that the passed amount is positive (including 0).
      static long positiveOrZero​(long amount, java.lang.String name)
      Validates that the passed amount is positive (including 0).
      static <T extends java.util.Collection<?>>
      T
      size​(T instance, int expectedSize, java.lang.String name)
      Validates that the given collection instance has size matching the expectedSize
      static java.time.Duration validate​(java.time.Duration duration, java.lang.String name)
      Validates that the passed duration is not null and strictly positive.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SUPPLIER_PRODUCED_NULL

        public static final java.lang.String SUPPLIER_PRODUCED_NULL
        See Also:
        Constant Field Values
      • MAPPER_RETURNED_NULL

        public static final java.lang.String MAPPER_RETURNED_NULL
        See Also:
        Constant Field Values
    • Method Detail

      • validate

        public static java.time.Duration validate​(java.time.Duration duration,
                                                  java.lang.String name)
        Validates that the passed duration is not null and strictly positive.
        Parameters:
        duration - the duration
        name - the name of the parameter, must not be null
        Returns:
        the duration is the validation passes.
      • nonNull

        public static <T> T nonNull​(T instance,
                                    java.lang.String name)
        Validates that the given instance is not null.
        Type Parameters:
        T - the type of the instance
        Parameters:
        instance - the instance
        name - the name of the parameter, must not be null
        Returns:
        the instance if the validation passes
      • nonNullNpe

        public static <T> T nonNullNpe​(T instance,
                                       java.lang.String name)
        Validates that the given instance is not null. Unlike nonNull(Object, String), this method throw a NullPointerException. It's generally used to be compliant with the Reactive Streams specification expecting NullPointerException.
        Type Parameters:
        T - the type of the instance
        Parameters:
        instance - the instance
        name - the name of the parameter, must not be null
        Returns:
        the instance if the validation passes
      • positive

        public static long positive​(long amount,
                                    java.lang.String name)
        Validates that the passed amount is strictly positive.
        Parameters:
        amount - the amount to be checked
        name - the name of the parameter, must not be null
        Returns:
        the amount is the validation passes.
      • positive

        public static int positive​(int amount,
                                   java.lang.String name)
        Validates that the passed amount is strictly positive.
        Parameters:
        amount - the amount to be checked
        name - the name of the parameter, must not be null
        Returns:
        the amount is the validation passes.
      • positiveOrZero

        public static int positiveOrZero​(int amount,
                                         java.lang.String name)
        Validates that the passed amount is positive (including 0).
        Parameters:
        amount - the amount to be checked
        name - the name of the parameter, must not be null
        Returns:
        the amount is the validation passes.
      • positiveOrZero

        public static long positiveOrZero​(long amount,
                                          java.lang.String name)
        Validates that the passed amount is positive (including 0).
        Parameters:
        amount - the amount to be checked
        name - the name of the parameter, must not be null
        Returns:
        the amount is the validation passes.
      • doesNotContainNull

        public static <T extends java.lang.Iterable<?>> T doesNotContainNull​(T iterable,
                                                                             java.lang.String name)
        Ensures that the given iterable does not contain a null value.
        Type Parameters:
        T - the type of the instance
        Parameters:
        iterable - the iterable
        name - the name of the parameter, must not be null
        Returns:
        the instance if the validation passes
      • doesNotContainNull

        public static <T> T[] doesNotContainNull​(T[] array,
                                                 java.lang.String name)
        Ensures that the given array does not contain a null value.
        Type Parameters:
        T - the type of the item contained in the array
        Parameters:
        array - the array
        name - the name of the parameter, must not be null
        Returns:
        the instance if the validation passes
      • isNotEmpty

        public static <T extends java.util.Collection<?>> T isNotEmpty​(T collection,
                                                                       java.lang.String name)
        Ensures that the given cllection is not empty.
        Type Parameters:
        T - the type of the item contained in the array
        Parameters:
        collection - the collection to check
        name - the name of the parameter, must not be null
        Returns:
        the instance if the validation passes
      • size

        public static <T extends java.util.Collection<?>> T size​(T instance,
                                                                 int expectedSize,
                                                                 java.lang.String name)
        Validates that the given collection instance has size matching the expectedSize
        Type Parameters:
        T - the type of the instance
        Parameters:
        instance - the instance
        expectedSize - the expected size
        name - the name of the parameter, must not be null
        Returns:
        the instance if the validation passes