Module com.github.robtimus.servlet
Class ComparableParameter<T extends Comparable<? super T>>
- java.lang.Object
-
- com.github.robtimus.servlet.parameters.ComparableParameter<T>
-
- Type Parameters:
T
- The comparable type.
public final class ComparableParameter<T extends Comparable<? super T>> extends Object
Represents a parameter that should have a value of a specific comparable type. This class can be used for types likeBigInteger
orBigDecimal
.- Author:
- Rob Spoor
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ComparableParameter<T>
atLeast(T minValue)
Validates that the parameter value is not too small.ComparableParameter<T>
atMost(T maxValue)
Validates that the parameter value is not too large.ComparableParameter<T>
between(T minValue, T maxValue)
Validates that the parameter value is not too small or large.ComparableParameter<T>
greaterThan(T minValue)
Validates that the parameter value is not too small.boolean
isSet()
Returns whether or not the parameter is set.static <T extends Comparable<? super T>>
ComparableParameter<T>of(FilterConfig config, String name, Function<String,? extends T> converter)
Returns a comparable init parameter for a filter.static <T extends Comparable<? super T>>
ComparableParameter<T>of(ServletConfig config, String name, Function<String,? extends T> converter)
Returns a comparable init parameter for a servlet.static <T extends Comparable<? super T>>
ComparableParameter<T>of(ServletContext context, String name, Function<String,? extends T> converter)
Returns a comparable init parameter for a servlet context.static <T extends Comparable<? super T>>
ComparableParameter<T>of(ServletRequest request, String name, Function<String,? extends T> converter)
Returns a comparable parameter for a servlet request.T
requiredValue()
Returns the parameter value.ComparableParameter<T>
smallerThan(T maxValue)
Validates that the parameter value is not too large.String
toString()
T
valueWithDefault(T defaultValue)
Returns the parameter if it is set.
-
-
-
Method Detail
-
isSet
public boolean isSet()
Returns whether or not the parameter is set.- Returns:
true
if the parameter is set, orfalse
otherwise.
-
requiredValue
public T requiredValue()
Returns the parameter value.- Returns:
- The parameter value.
- Throws:
IllegalStateException
- If the parameter is not set.
-
valueWithDefault
public T valueWithDefault(T defaultValue)
Returns the parameter if it is set.- Parameters:
defaultValue
- The value to return if the parameter is not set.- Returns:
- The parameter value, or the given default value if the parameter is not set.
-
atLeast
public ComparableParameter<T> atLeast(T minValue)
Validates that the parameter value is not too small.- Parameters:
minValue
- The minimum value for the parameter, inclusive.- Returns:
- This object.
- Throws:
NullPointerException
- If the given minimum isnull
.IllegalStateException
- If the parameter is set but its value is smaller than the given minimum.
-
atMost
public ComparableParameter<T> atMost(T maxValue)
Validates that the parameter value is not too large.- Parameters:
maxValue
- The maximum value for the parameter, inclusive.- Returns:
- This object.
- Throws:
NullPointerException
- If the given maximum isnull
.IllegalStateException
- If the parameter is set but its value is larger than the given maximum.
-
greaterThan
public ComparableParameter<T> greaterThan(T minValue)
Validates that the parameter value is not too small.- Parameters:
minValue
- The minimum value for the parameter, exclusive.- Returns:
- This object.
- Throws:
NullPointerException
- If the given minimum isnull
.IllegalStateException
- If the parameter is set but its value is smaller than or equal to the given minimum.
-
smallerThan
public ComparableParameter<T> smallerThan(T maxValue)
Validates that the parameter value is not too large.- Parameters:
maxValue
- The maximum value for the parameter, exclusive.- Returns:
- This object.
- Throws:
NullPointerException
- If the given maximum isnull
.IllegalStateException
- If the parameter is set but its value is larger than or equal to the given maximum.
-
between
public ComparableParameter<T> between(T minValue, T maxValue)
Validates that the parameter value is not too small or large. This method combinesatLeast(Comparable)
andsmallerThan(Comparable)
.- Parameters:
minValue
- The minimum value for the parameter, inclusive.maxValue
- The maximum value for the parameter, exclusive.- Returns:
- This object.
- Throws:
NullPointerException
- If the given minimum or maximum isnull
.IllegalStateException
- If the parameter is set but its value is not between the given minimum and maximum.
-
of
public static <T extends Comparable<? super T>> ComparableParameter<T> of(FilterConfig config, String name, Function<String,? extends T> converter)
Returns a comparable init parameter for a filter.- Type Parameters:
T
- The comparable type.- Parameters:
config
- The filter config to read the init parameter from.name
- The name of the init parameter.converter
- A function for converting string values to comparable values. It will never be called with anull
value.- Returns:
- An object representing the init parameter with the given name from the given filter config. It may or may not be set.
- Throws:
NullPointerException
- If the given filter config, name or converter isnull
.IllegalStateException
- If the init parameter is set but the converter throws an exception.
-
of
public static <T extends Comparable<? super T>> ComparableParameter<T> of(ServletConfig config, String name, Function<String,? extends T> converter)
Returns a comparable init parameter for a servlet.- Type Parameters:
T
- The comparable type.- Parameters:
config
- The servlet config to read the init parameter from.name
- The name of the init parameter.converter
- A function for converting string values to comparable values. It will never be called with anull
value.- Returns:
- An object representing the init parameter with the given name from the given servlet config. It may or may not be set.
- Throws:
NullPointerException
- If the given servlet config, name or converter isnull
.IllegalStateException
- If the init parameter is set but the converter throws an exception.
-
of
public static <T extends Comparable<? super T>> ComparableParameter<T> of(ServletContext context, String name, Function<String,? extends T> converter)
Returns a comparable init parameter for a servlet context.- Type Parameters:
T
- The comparable type.- Parameters:
context
- The servlet context config to read the init parameter from.name
- The name of the init parameter.converter
- A function for converting string values to comparable values. It will never be called with anull
value.- Returns:
- An object representing the init parameter with the given name from the given servlet context. It may or may not be set.
- Throws:
NullPointerException
- If the given servlet context, name or converter isnull
.IllegalStateException
- If the init parameter is set but the converter throws an exception.
-
of
public static <T extends Comparable<? super T>> ComparableParameter<T> of(ServletRequest request, String name, Function<String,? extends T> converter)
Returns a comparable parameter for a servlet request.- Type Parameters:
T
- The comparable type.- Parameters:
request
- The servlet request config to read the parameter from.name
- The name of the parameter.converter
- A function for converting string values to comparable values. It will never be called with anull
value.- Returns:
- An object representing the parameter with the given name from the given servlet request. It may or may not be set.
- Throws:
NullPointerException
- If the given servlet request, name or converter isnull
.IllegalStateException
- If the parameter is set but the converter throws an exception.
-
-