com.vaadin.flow.data.validator.
Class RangeValidator<T>
Type Parameters:
T
- the type to validate
All Implemented Interfaces:
Validator<T>
, Serializable
, BiFunction<T,
Direct Known Subclasses:
BigDecimalRangeValidator
, BigIntegerRangeValidator
, ByteRangeValidator
, DateRangeValidator
, DateTimeRangeValidator
, DoubleRangeValidator
, FloatRangeValidator
, IntegerRangeValidator
, LongRangeValidator
, ShortRangeValidator
Verifies that a value is within the given range.
Since:
1.0.
Author:
Vaadin Ltd
See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRangeValidator
(String errorMessage, Comparator<? super T> comparator, T minValue, T maxValue) Creates a new range validator of the given type.
-
Method Summary
Modifier and TypeMethodDescriptionapply
(T value, ValueContext context) Returns
Result.ok
if the value is within the specified bounds,Result.error
otherwise.Gets the maximum value of the range.
Returns the minimum value of the range.
boolean
Returns whether the maximum value is part of the accepted range.
boolean
Returns whether the minimum value is part of the accepted range.
protected boolean
Returns whether the given value lies in the valid range.
static <C extends Comparable<? super C>>
RangeValidator<C>Returns a
RangeValidator
comparing values of aComparable
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.
toString()
Methods inherited from class com.vaadin.flow.data.validator.AbstractValidator
getMessage, toResult
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.function.BiFunction
andThen
-
Constructor Details
-
RangeValidator
public RangeValidator(String errorMessage, Comparator<? super T> comparator, T minValue, T maxValue) Creates a new range validator of the given type. Passing null to either
minValue
ormaxValue
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 toapply
depends on the given comparator.Parameters:
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 limit
-
-
Method Details
-
of
public static <C extends Comparable<? super C>> RangeValidator<C> of(String errorMessage, C minValue, C maxValue) Returns a
RangeValidator
comparing values of aComparable
type using their natural order. Passing null to eitherminValue
ormaxValue
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.
Type Parameters:
C
- theComparable
value typeParameters:
errorMessage
- 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 limitReturns:
the new validator
-
apply
Returns
Result.ok
if the value is within the specified bounds,Result.error
otherwise. If null is passed toapply
, the behavior depends on the used comparator.Parameters:
value
- the input value to validatecontext
- the value context for validationReturns:
the validation result
-
isMinValueIncluded
public boolean isMinValueIncluded()Returns whether the minimum value is part of the accepted range.
Returns:
true if the minimum value is part of the range, false otherwise
-
setMinValueIncluded
public void setMinValueIncluded(boolean minValueIncluded) Sets whether the minimum value is part of the accepted range.
Parameters:
minValueIncluded
- true if the minimum value should be part of the range, false otherwise -
isMaxValueIncluded
public boolean isMaxValueIncluded()Returns whether the maximum value is part of the accepted range.
Returns:
true if the maximum value is part of the range, false otherwise
-
setMaxValueIncluded
public void setMaxValueIncluded(boolean maxValueIncluded) Sets whether the maximum value is part of the accepted range.
Parameters:
maxValueIncluded
- true if the maximum value should be part of the range, false otherwise -
getMinValue
Returns the minimum value of the range.
Returns:
the minimum value
-
setMinValue
Sets the minimum value of the range. Use
setMinValueIncluded(boolean)
to control whether this value is part of the range or not.Parameters:
minValue
- the minimum value -
getMaxValue
Gets the maximum value of the range.
Returns:
the maximum value
-
setMaxValue
Sets the maximum value of the range. Use
setMaxValueIncluded(boolean)
to control whether this value is part of the range or not.Parameters:
maxValue
- the maximum value -
toString
-
isValid
Returns whether the given value lies in the valid range.
Parameters:
value
- the value to validateReturns:
true if the value is valid, false otherwise
-