- All Implemented Interfaces and Traits:
- Annotation
@Documented
@Repeatable(value: QuantityUnitConstraintList)
@Target(value: [FIELD, METHOD, PARAMETER, TYPE_USE, ANNOTATION_TYPE])
@Retention(value: RUNTIME)
@jakarta.validation.Constraint(validatedBy: [])
@interface QuantityUnitConstraint
Validates the unit of the provided quantity (instance of javax.measure.Quantity
).
Example usage:
class SomeClassWithQuantities {
// The unit of validated quantity must be compatible with a unit with symbol kg.
@QuantityUnitConstraint(unitSymbol = "kg")
Quantity someWeightQuantity
@QuantityUnitConstraint(unitSymbol = "kg")
Quantity someUntypedWeightQuantity
// The unit of validated quantity must be equal with a unit with symbol kg.
@QuantityUnitConstraint(unitSymbol = "kg", acceptOnlyExactUnitSymbol = true)
Quantity someOtherWeight
// The unit of validated quantity must be compatible with a unit with symbol kg. If not,
// failed validation message will contain compatibleUnitSymbolsForMessage elements as a
// suggestion for a user.
@QuantityUnitConstraint(unitSymbols = "kg", compatibleUnitSymbolsForMessage = ["kg", "g"])
Quantity someOtherUntypedWeightQuantity
}
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 unitSymbol
must be specified. By default, it defines a compatible unit of a validated quantity. If the value of unitSymbol
not configured or it is not recognized
by the underlying JSR-385 implementation, AssertionError
is thrown during validator initialization.
For example, suppose we expect a mass quantity and a unitSymbol
is set to kg
. In that case, we will accept a validated quantity with units in kilograms (unit symbol kg
),
grams (unit symbol g
), or any other unit supported and used for mass quantities by our JSR-386 implementation.
By configuring acceptOnlyExactUnitSymbol
to true
(default is false
), we accept only quantities with units equal to kilograms. Compatible units are not accepted in
that case.
Parameter compatibleUnitSymbolsForMessage
is optional and is used only for creating failed validation messages. When configured, the failed validation message will contain all listed unit
symbols as a convenience for a user.
In default validator implementation, default message interpolation key is org.klokwrk.lib.lo.validation.constraint.uom.QuantityUnitConstraint.invalidUnitSymbolMessage
.
For custom message interpolation (when message
annotation param is defined), default validator implementation exposes expectedUnitSymbol
, providedUnitSymbol
, and
compatibleUnitSymbols
expressions.
When custom annotation 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 '${}
'.