Class Parameters
- java.lang.Object
-
- com.github.m0nk3y2k4.thetvdb.internal.util.validation.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 anIllegalArgumentException
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 defaultIllegalArgumentException
typically indicates that resolving the failed validation lies within the responsibility of the calling instance. To check for additional, non-method parameter related preconditions usePreconditions
.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Predicate<String>
isPositiveInteger()
Provides a predicate used to check whether a String represents a positive (greater zero) numerical integer.static <T,X extends RuntimeException>
voidvalidateCondition(Predicate<T> condition, T value, X exception)
Checks the condition and throws the given runtime exception in case the condition is not met.static void
validateNotEmpty(String obj, String message)
Checks that the given String is neither null nor empty.static void
validateNotEmpty(Optional<String> obj, String message)
Checks that the given Optional contains a non-empty String.static void
validateNotNull(Object obj, String message)
Checks that the givenobj
is not null.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.static void
validateQueryParam(String paramName, QueryParameters params)
Checks if the givenparams
query parameter collection contains a non-emptyparamName
parameter.static void
validateQueryParam(String paramName, QueryParameters params, Predicate<String> condition)
Checks if the givenparams
query parameter collection contains a non-emptyparamName
parameter which matches the given condition.
-
-
-
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 checkX
- type of the runtime exception to be thrown- Parameters:
condition
- The condition to check forvalue
- The value to be checked against the conditionexception
- The exception to be thrown in case the condition is not met
-
validateNotNull
public static void validateNotNull(Object obj, String message)
Checks that the givenobj
is not null. Otherwise an exception with the given error message will be thrown.- Parameters:
obj
- The object to checkmessage
- Error message to be propagated to the exception in case of a failed validation- Throws:
IllegalArgumentException
- If the givenobj
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 checkmessage
- 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 checkmessage
- 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 checkparamValue
- The corresponding value of the path parametercondition
- 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 givenparams
query parameter collection contains a non-emptyparamName
parameter. Otherwise an exception will be thrown.- Parameters:
paramName
- The name of the URL query parameter to check forparams
- 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 givenparams
query parameter collection contains a non-emptyparamName
parameter which matches the given condition. Otherwise an exception will be thrown.- Parameters:
paramName
- The name of the URL query parameter to check forparams
- Query parameters object that should contain the given parametercondition
- 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
-
-