T
- the type to validatepublic class RangeValidator<T> extends AbstractValidator<T>
Constructor and Description |
---|
RangeValidator(String errorMessage,
Comparator<? super T> comparator,
T minValue,
T maxValue)
Creates a new range validator of the given type.
|
Modifier and Type | Method and Description |
---|---|
ValidationResult |
apply(T value,
ValueContext context)
Returns
Result.ok if the value is within the specified bounds,
Result.error otherwise. |
T |
getMaxValue()
Gets the maximum value of the range.
|
T |
getMinValue()
Returns the minimum value of the range.
|
boolean |
isMaxValueIncluded()
Returns whether the maximum value is part of the accepted range.
|
boolean |
isMinValueIncluded()
Returns whether the minimum value is part of the accepted range.
|
protected boolean |
isValid(T value)
Returns whether the given value lies in the valid range.
|
static <C extends Comparable<? super C>> |
of(String errorMessage,
C minValue,
C maxValue)
Returns a
RangeValidator comparing values of a Comparable
type using their natural order. |
void |
setMaxValue(T maxValue)
Sets the maximum value of the range.
|
void |
setMaxValueIncluded(boolean maxValueIncluded)
Sets whether the maximum value is part of the accepted range.
|
void |
setMinValue(T minValue)
Sets the minimum value of the range.
|
void |
setMinValueIncluded(boolean minValueIncluded)
Sets whether the minimum value is part of the accepted range.
|
String |
toString() |
getMessage, toResult
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
alwaysPass, from, from, from, from
andThen
public RangeValidator(String errorMessage, Comparator<? super T> comparator, T minValue, T maxValue)
minValue
or maxValue
means there is no limit in that
direction. Both limits may be null; this can be useful if the limits are
resolved programmatically. The result of passing null to apply
depends on the given comparator.errorMessage
- the error message to return if validation fails, not nullcomparator
- the comparator to compare with, not nullminValue
- the least value of the accepted range or null for no limitmaxValue
- the greatest value of the accepted range or null for no limitpublic static <C extends Comparable<? super C>> RangeValidator<C> of(String errorMessage, C minValue, C maxValue)
RangeValidator
comparing values of a Comparable
type using their natural order. Passing null to either
minValue
or maxValue
means there is no limit in that
direction. Both limits may be null; this can be useful if the limits are
resolved programmatically.
Null is considered to be less than any non-null value. This means null never passes validation if a minimum value is specified.
C
- the Comparable
value typeerrorMessage
- the error message to return if validation fails, not nullminValue
- the least value of the accepted range or null for no limitmaxValue
- the greatest value of the accepted range or null for no limitpublic ValidationResult apply(T value, ValueContext context)
Result.ok
if the value is within the specified bounds,
Result.error
otherwise. If null is passed to apply
, the
behavior depends on the used comparator.value
- the input value to validatecontext
- the value context for validationpublic boolean isMinValueIncluded()
public void setMinValueIncluded(boolean minValueIncluded)
minValueIncluded
- true if the minimum value should be part of the range, false
otherwisepublic boolean isMaxValueIncluded()
public void setMaxValueIncluded(boolean maxValueIncluded)
maxValueIncluded
- true if the maximum value should be part of the range, false
otherwisepublic T getMinValue()
public void setMinValue(T minValue)
setMinValueIncluded(boolean)
to control whether this value is
part of the range or not.minValue
- the minimum valuepublic T getMaxValue()
public void setMaxValue(T maxValue)
setMaxValueIncluded(boolean)
to control whether this value is
part of the range or not.maxValue
- the maximum valueprotected boolean isValid(T value)
value
- the value to validateCopyright © 2025. All rights reserved.