Class Parameters


  • public final class Parameters
    extends Object
    Collection of simple checks to be used for method parameter validation


    The default behavior for parameter checks provided by this class is to throw an IllegalArgumentException in case the given arguments do not match the requirements. Some of the methods may support extended control over the actual type of exception that will be thrown by allowing the calling instance to provide it's onw runtime exception instance.


    Please note that this class should primarily be used for method parameter validation as the default IllegalArgumentException typically indicates that resolving the failed validation lies within the responsibility of the calling instance. To check for additional, non-method parameter related preconditions use Preconditions.

    • Method Detail

      • validateCondition

        public static <T,X extends RuntimeException> void validateCondition​(Predicate<T> condition,
                                                                            T value,
                                                                            X exception)
        Checks the condition and throws the given runtime exception in case the condition is not met.
        Type Parameters:
        T - the type of the value to check
        X - type of the runtime exception to be thrown
        Parameters:
        condition - The condition to check for
        value - The value to be checked against the condition
        exception - The exception to be thrown in case the condition is not met
      • validateNotNull

        public static void validateNotNull​(Object obj,
                                           String message)
        Checks that the given obj is not null. Otherwise an exception with the given error message will be thrown.
        Parameters:
        obj - The object to check
        message - Error message to be propagated to the exception in case of a failed validation
        Throws:
        IllegalArgumentException - If the given obj is null
      • validateNotEmpty

        public static void validateNotEmpty​(String obj,
                                            String message)
        Checks that the given String is neither null nor empty. Otherwise an exception with the given error message will be thrown.
        Parameters:
        obj - The String to check
        message - Error message to be propagated to the exception in case of a failed validation
        Throws:
        IllegalArgumentException - If the given String is either null or empty
      • validateNotEmpty

        public static void validateNotEmpty​(@Nonnull
                                            Optional<String> obj,
                                            String message)
        Checks that the given Optional contains a non-empty String. Otherwise an exception with the given error message will be thrown.
        Parameters:
        obj - The String optional to check
        message - Error message to be propagated to the exception in case of a failed validation
        Throws:
        IllegalArgumentException - If no value is present or the value is an empty String
      • validatePathParam

        public static <T> void validatePathParam​(String paramName,
                                                 T paramValue,
                                                 Predicate<T> condition)
        Checks that a given URL path parameter name is not empty and the corresponding value matches the given condition. Otherwise an exception will be thrown.
        Type Parameters:
        T - Type of the parameter value to check
        Parameters:
        paramName - The name of the URL path parameter to check
        paramValue - The corresponding value of the path parameter
        condition - Condition to be matched by the given parameter value
        Throws:
        IllegalArgumentException - If the given parameter name is null or the parameter value does not match the given condition
      • validateQueryParam

        public static void validateQueryParam​(String paramName,
                                              QueryParameters params)
        Checks if the given params query parameter collection contains a non-empty paramName parameter. Otherwise an exception will be thrown.
        Parameters:
        paramName - The name of the URL query parameter to check for
        params - Query parameters object that should contain the given parameter
        Throws:
        IllegalArgumentException - If the parameter collection does not contain a non-empty parameter with the given name
      • validateQueryParam

        public static void validateQueryParam​(String paramName,
                                              QueryParameters params,
                                              Predicate<String> condition)
        Checks if the given params query parameter collection contains a non-empty paramName parameter which matches the given condition. Otherwise an exception will be thrown.
        Parameters:
        paramName - The name of the URL query parameter to check for
        params - Query parameters object that should contain the given parameter
        condition - Condition to be matched by the query parameter value
        Throws:
        IllegalArgumentException - If the parameter collection does not contain a non-empty parameter with the given name or the parameter does not match the given condition
      • isPositiveInteger

        public static Predicate<String> isPositiveInteger()
        Provides a predicate used to check whether a String represents a positive (greater zero) numerical integer.
        Returns:
        String predicate to check for a positive numerical integer