Module com.github.robtimus.servlet
Class StringParameter
- java.lang.Object
-
- com.github.robtimus.servlet.parameters.StringParameter
-
public final class StringParameter extends Object
Represents a parameter that should have a string value.- Author:
- Rob Spoor
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringParameter
atLeast(String minValue)
Validates that the parameter value is not too small.StringParameter
atMost(String maxValue)
Validates that the parameter value is not too large.StringParameter
between(String minValue, String maxValue)
Validates that the parameter value is not too small or large.StringParameter
greaterThan(String minValue)
Validates that the parameter value is not too small.boolean
isSet()
Returns whether or not the parameter is set.StringParameter
matching(Pattern pattern)
Validates that the parameter value matches a pattern.StringParameter
notBlank()
Validates that the parameter value is not blank.StringParameter
notEmpty()
Validates that the parameter value is not empty.static StringParameter
of(FilterConfig config, String name)
Returns a string init parameter for a filter.static StringParameter
of(ServletConfig config, String name)
Returns a string init parameter for a servlet.static StringParameter
of(ServletContext context, String name)
Returns a string init parameter for a servlet context.static StringParameter
of(ServletRequest request, String name)
Returns a string parameter for a servlet request.String
requiredValue()
Returns the parameter value.StringParameter
smallerThan(String maxValue)
Validates that the parameter value is not too large.String
toString()
StringParameter
transform(Function<? super String,String> f)
Applies a function to the parameter value.String
valueWithDefault(String 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 String requiredValue()
Returns the parameter value.- Returns:
- The parameter value.
- Throws:
IllegalStateException
- If the parameter is not set.
-
valueWithDefault
public String valueWithDefault(String 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 StringParameter atLeast(String 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 StringParameter atMost(String 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 StringParameter greaterThan(String 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 StringParameter smallerThan(String 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 StringParameter between(String minValue, String maxValue)
Validates that the parameter value is not too small or large. This method combinesatLeast(String)
andsmallerThan(String)
.- 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.
-
notEmpty
public StringParameter notEmpty()
Validates that the parameter value is not empty.- Returns:
- This object.
- Throws:
IllegalStateException
- If the parameter is set but its value is empty.
-
notBlank
public StringParameter notBlank()
Validates that the parameter value is not blank.- Returns:
- This object.
- Throws:
IllegalStateException
- If the parameter is set but its value is blank.
-
matching
public StringParameter matching(Pattern pattern)
Validates that the parameter value matches a pattern.- Parameters:
pattern
- The pattern to check against.- Returns:
- This object.
- Throws:
NullPointerException
- If the given pattern isnull
.IllegalStateException
- If the parameter is set but its value does not match the given pattern.
-
transform
public StringParameter transform(Function<? super String,String> f)
Applies a function to the parameter value. This allows applying operations likeString.trim()
orString.toLowerCase()
.If the parameter is not set, the function will not be called. If the parameter is set but function returns
null
, the returned parameter will behave like a parameter that is not set.- Parameters:
f
- The function to apply.- Returns:
- A string parameter with a value that is the result of applying the given function to the parameter value of this object.
- Throws:
NullPointerException
- If the given function isnull
.
-
of
public static StringParameter of(FilterConfig config, String name)
Returns a string init parameter for a filter.- Parameters:
config
- The filter config to read the init parameter from.name
- The name of the init parameter.- 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 or name isnull
.
-
of
public static StringParameter of(ServletConfig config, String name)
Returns a string init parameter for a servlet.- Parameters:
config
- The servlet config to read the init parameter from.name
- The name of the init parameter.- 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 or name isnull
.
-
of
public static StringParameter of(ServletContext context, String name)
Returns a string init parameter for a servlet context.- Parameters:
context
- The servlet context config to read the init parameter from.name
- The name of the init parameter.- Returns:
- An object representing the init parameter with the given name from the given servlet context. It may or may not be set.
-
of
public static StringParameter of(ServletRequest request, String name)
Returns a string parameter for a servlet request.- Parameters:
request
- The servlet request to read the parameter from.name
- The name of the parameter.- Returns:
- An object representing the parameter with the given name from the given servlet request. It may or may not be set.
-
-