- All Implemented Interfaces and Traits:
- Annotation
@Documented
@Repeatable(value: QuantityRangeConstraintList)
@Target(value: [FIELD, METHOD, PARAMETER, TYPE_USE, ANNOTATION_TYPE])
@Retention(value: RUNTIME)
@jakarta.validation.Constraint(validatedBy: [])
@interface QuantityRangeConstraint
Validates the quantity (instance of javax.measure.Quantity
) against specified quantity range.
Example usage:
class SomeClassWithQuantities {
// Allowed minimum is 10 kilograms inclusively and allowed maximum is 100 kg inclusively.
// Provided quantity must be greater than or equal to 10 kilograms and less than or equal to 100 kg.
// The validated quantity must have a unit compatible with kilograms.
@QuantityRangeConstraint(minQuantity = "10 kg", maxQuantity = "100 kg")
Quantity quantityMinAndMaxInclusive
// Allowed minimum is 10 kilograms exclusively and allowed maximum is 100 kg exclusively.
// Provided quantity must be strictly greater than 10 kilograms and strictly less than 100 kg.
// The validated quantity must have a unit compatible with kilograms.
@QuantityRangeConstraint(minQuantity = "10 kg", minInclusive = false, maxQuantity = "100 kg", maxInclusive = false)
Quantity quantityMinExclusiveAndMaxExclusive
// Allowed minimum is 10 kilograms inclusively and allowed maximum is 100 kg exclusively.
// The validated quantity must have a unit strictly equal to kilograms.
@QuantityRangeConstraint(minQuantity = "10 kg", maxQuantity = "100 kg", maxInclusive = false, acceptOnlyEqualUnit = true)
Quantity someUntypedWeightQuantity
Unit symbols corresponding to a particular unit are defined by units of measurement JSR-385 reference implementation and their extensions. Look at the
tech.units.indriya.unit.Units class for a base set of units and their
symbols (https://github.com/unitsofmeasurement/indriya).
Parameters minQuantity
and maxQuantity
must be specified. By default, the units of minQuantity
and maxQuantity
must be mutually compatible. If they are not mutually
compatible AssertionError
is thrown during validator initialization.
Parameter acceptOnlyEqualUnit
is optional (default value is false
). It cannot be specified individually for minimum or maximum. Instead, both boundaries must be set with equal
units when acceptOnlyEqualUnit
is true
. Otherwise, AssertionError
will be thrown during validator initialization.
The default value of the minInclusive
and maxInclusive
parameters is true
. This means the validated quantity can be greater than or equal to the configured
minQuantity
and less than or equal to maxQuantity
. On the other hand, if minInclusive
is set to false
, the validated quantity has to be strictly greater than
minQuantity
. Similarly, when maxInclusive
is set to false
, the validated quantity has to be strictly less than maxQuantity
.
For message interpolation, the default validator provides six types of message templates with corresponding resource bundle keys:
-
INVALID_UNIT_EQUAL:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityRangeConstraint.invalidUnitEqualMessage
Used when equal unit matching is required but not satisfied.
-
INVALID_UNIT_COMPATIBLE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityRangeConstraint.invalidUnitCompatibleMessage
Used when compatible unit matching is required but not satisfied.
-
INVALID_QUANTITY_MIN_INCLUSIVE_MAX_INCLUSIVE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityRangeConstraint.invalidQuantityMinInclusiveMaxInclusiveMessage
Used when the inclusive minimum and inclusive maximum are required but not satisfied.
-
INVALID_QUANTITY_MIN_INCLUSIVE_MAX_EXCLUSIVE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityRangeConstraint.invalidQuantityMinInclusiveMaxExclusiveMessage
Used when the inclusive minimum and exclusive maximum are required but not satisfied.
-
INVALID_QUANTITY_MIN_EXCLUSIVE_MAX_INCLUSIVE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityRangeConstraint.invalidQuantityMinExclusiveMaxInclusiveMessage
Used when the exclusive minimum and inclusive maximum are required but not satisfied.
-
INVALID_QUANTITY_MIN_EXCLUSIVE_MAX_EXCLUSIVE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityRangeConstraint.invalidQuantityMinExclusiveMaxExclusiveMessage
Used when the exclusive minimum and exclusive maximum are required but not satisfied.
For all message templates (including custom one provided via annotation's message
parameter), six expressions are provided:
expectedMinQuantity
- the value of the annotation's minQuantity
parameter
expectedMinQuantityUnit
- a string representation for a unit of the minQuantity
expectedMaxQuantity
- the value of the annotation's maxQuantity
parameter
expectedMaxQuantityUnit
- a string representation for a unit of the maxQuantity
providedQuantity
- a string representation of the validated quantity
providedQuantityUnit
- a string representation for a unit of the validated quantity
When the custom annotation's message
parameter value is specified, it can be either a reference to the resource bundle key (must be enclosed in curly braces '{}
') or a
hardcoded message. In resource bundle properties files and in the hardcoded message, exposed interpolation expressions must be enclosed in curly braces starting with a dollar sign
'${}
'. - See Also:
- QuantityMinConstraint
- QuantityMaxConstraint
- QuantityUnitConstraint