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:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <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.Returns a description of the constraint imposed by this validator on the values.Serializes this constraint as XML attributes, that are part of the property element of a rule definition.default PropertyConstraint<Iterable<? extends T>>Returns a constraint that validates a collection of Ts by checking each component conforms to this validator.default PropertyConstraint<Optional<? extends T>>Returns a constraint that validates anOptional<T>by checking that the value conforms to this constraint if it is non-empty.voidChecks that the value conforms to this constraint.
-
Method Details
-
validate
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
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_MINSchemaConstants.PROPERTY_MAX
-
toOptionalConstraint
Returns a constraint that validates anOptional<T>by checking that the value conforms to this constraint if it is non-empty. -
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:
-