Package com.vaadin.flow.data.validator
Class RangeValidator<T>
java.lang.Object
com.vaadin.flow.data.validator.AbstractValidator<T>
com.vaadin.flow.data.validator.RangeValidator<T>
- Type Parameters:
T- the type to validate
- All Implemented Interfaces:
Validator<T>,Serializable,BiFunction<T,ValueContext, ValidationResult>
- 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) ReturnsResult.okif the value is within the specified bounds,Result.errorotherwise.Gets the maximum value of the range.Returns the minimum value of the range.booleanReturns whether the maximum value is part of the accepted range.booleanReturns whether the minimum value is part of the accepted range.protected booleanReturns whether the given value lies in the valid range.static <C extends Comparable<? super C>>
RangeValidator<C> Returns aRangeValidatorcomparing values of aComparabletype using their natural order.voidsetMaxValue(T maxValue) Sets the maximum value of the range.voidsetMaxValueIncluded(boolean maxValueIncluded) Sets whether the maximum value is part of the accepted range.voidsetMinValue(T minValue) Sets the minimum value of the range.voidsetMinValueIncluded(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, toResultMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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 eitherminValueormaxValuemeans 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 toapplydepends 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 aRangeValidatorcomparing values of aComparabletype using their natural order. Passing null to eitherminValueormaxValuemeans 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- theComparablevalue type- Parameters:
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 limit- Returns:
- the new validator
-
apply
ReturnsResult.okif the value is within the specified bounds,Result.errorotherwise. If null is passed toapply, the behavior depends on the used comparator.- Parameters:
value- the input value to validatecontext- the value context for validation- Returns:
- 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. UsesetMinValueIncluded(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. UsesetMaxValueIncluded(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 validate- Returns:
- true if the value is valid, false otherwise
-