- All Implemented Interfaces and Traits:
- Annotation
@Documented
@Repeatable(value: QuantityMaxConstraintList)
@Target(value: [FIELD, METHOD, PARAMETER, TYPE_USE, ANNOTATION_TYPE])
@Retention(value: RUNTIME)
@jakarta.validation.Constraint(validatedBy: [])
@interface QuantityMaxConstraint
Validates the allowed maximum of the annotated quantity (instance of javax.measure.Quantity
).
Example usage:
class SomeClassWithQuantities {
// Allowed maximum is 200.5 kilograms inclusively.
// Provided quantity must be greater or equal to 200.5 kilograms.
// The validated quantity must have a unit compatible with kilograms.
@QuantityMaxConstraint(maxQuantity = "200.5 kg")
Quantity someWeightQuantity
// Allowed maximum is 200.5 kilograms exclusively.
// Provided quantity must be strictly less than 200.5 kilograms.
// The validated quantity must have a unit compatible with kilograms.
@QuantityMaxConstraint(maxQuantity = "200.5 kg", inclusive = false)
Quantity someOtherWeight
// Allowed maximum is 100 kilograms inclusively.
// The validated quantity must have a unit strictly equal to kilograms.
@QuantityMaxConstraint(maxQuantity = "5 kg", 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).
Parameter maxQuantity
must be specified. By default, the unit of a validated quantity has to be compatible with the unit of a maxQuantity
. If the value of
maxQuantity
is not configured or the underlying JSR-385 implementation does not recognize it, AssertionError
is thrown during validator initialization.
The default value of the inclusive
parameter is true
. This means the validated quantity can be less than or equal to the configured maxQuantity
. If inclusive
is
set to false
, the validated quantity has to be strictly less than maxQuantity
.
The default value of acceptOnlyEqualUnit
parameter is false
. This means the unit of validated quantity has to be compatible with the unit of configured maxQuantity
. If
acceptOnlyEqualUnit
is set to true
, the unit of validated quantity hat to be equal to the unit of maxQuantity
.
For message interpolation, the default validator provides four types of message templates with corresponding resource bundle keys:
-
INVALID_UNIT_EQUAL_UNIT:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityMaxConstraint.invalidUnitEqualMessage
Used when equal unit matching is required but not satisfied.
-
INVALID_UNIT_COMPATIBLE_UNIT:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityMaxConstraint.invalidUnitCompatibleMessage
Used when compatible unit matching is required but not satisfied.
-
INVALID_QUANTITY_INCLUSIVE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityMaxConstraint.invalidQuantityInclusiveMessage
Used when the inclusive maximum is required but not satisfied.
-
INVALID_QUANTITY_EXCLUSIVE:
org.klokwrk.lib.lo.validation.constraint.uom.QuantityMaxConstraint.invalidQuantityExclusiveMessage
Used when the exclusive maximum is required but not satisfied.
For all message templates (including custom one provided via annotation's message
parameter), four expressions are provided:
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
'${}
'.