Package net.sourceforge.pmd.properties
Interface PropertyConstraint<T>
-
- Type Parameters:
T
- Type of value to handle
public interface PropertyConstraint<T>
Validates the value of a property.- Author:
- Clément Fournier
- Since:
- 6.10.0
- See Also:
PropertyBuilder.require(PropertyConstraint)
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <U> PropertyConstraint<U>
fromPredicate(Predicate<? super U> pred, String constraintDescription)
Builds a new validator from a predicate, and description.static <U> PropertyConstraint<U>
fromPredicate(Predicate<? super U> pred, String constraintDescription, Map<String,String> xmlConstraint)
Builds a new constraint from a predicate, a description and xml attributes to serialize the constraint.String
getConstraintDescription()
Returns a description of the constraint imposed by this validator on the values.default Map<String,String>
getXmlConstraint()
Serializes this constraint as XML attributes, that are part of the property element of a rule definition.default PropertyConstraint<Iterable<? extends T>>
toCollectionConstraint()
Returns a constraint that validates a collection of Ts by checking each component conforms to this validator.default PropertyConstraint<Optional<? extends T>>
toOptionalConstraint()
Returns a constraint that validates anOptional<T>
by checking that the value conforms to this constraint if it is non-empty.void
validate(T value)
Checks that the value conforms to this constraint.
-
-
-
Method Detail
-
validate
void validate(T value)
Checks that the value conforms to this constraint. Throws if that is not the case.- Parameters:
value
- The value to validate- Throws:
ConstraintViolatedException
- If this constraint is violated
-
getConstraintDescription
String getConstraintDescription()
Returns a description of the constraint imposed by this validator on the values. E.g. "Should be positive", or "Should be one of A | B | C."This is used to generate documentation.
- Returns:
- A description of the constraint
-
getXmlConstraint
default Map<String,String> getXmlConstraint()
Serializes this constraint as XML attributes, that are part of the property element of a rule definition.Note: This is only used for constraints, which can be defined in a rule definition in a ruleset (e.g. for XPath rules).
- Returns:
- a map with attribute name and attribute value, suitable to be used in XML.
- See Also:
SchemaConstants.PROPERTY_MIN
,SchemaConstants.PROPERTY_MAX
-
toOptionalConstraint
default PropertyConstraint<Optional<? extends T>> toOptionalConstraint()
Returns a constraint that validates anOptional<T>
by checking that the value conforms to this constraint if it is non-empty.
-
toCollectionConstraint
default PropertyConstraint<Iterable<? extends T>> toCollectionConstraint()
Returns a constraint that validates a collection of Ts by checking each component conforms to this validator.- Returns:
- A collection validator
-
fromPredicate
static <U> PropertyConstraint<U> fromPredicate(Predicate<? super U> pred, String constraintDescription)
Builds a new validator from a predicate, and description.- Type Parameters:
U
- Type of value to validate- Parameters:
pred
- The predicate. If it returns false on a value, then the value is deemed to have a problemconstraintDescription
- Description of the constraint, seegetConstraintDescription()
.- Returns:
- A new validator
-
fromPredicate
static <U> PropertyConstraint<U> fromPredicate(Predicate<? super U> pred, String constraintDescription, Map<String,String> xmlConstraint)
Builds a new constraint from a predicate, a description and xml attributes to serialize the constraint.- See Also:
fromPredicate(Predicate, String)
-
-