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