Interface PropertyConstraint<T>

  • Type Parameters:
    T - Type of value to handle

    @Experimental
    public interface PropertyConstraint<T>
    Validates the value of a property.

    This interface will change a lot with PMD 7.0.0, because of the switch to Java 8. Please use only the ready-made validators in NumericConstraints for now.

    Since:
    6.10.0
    Author:
    Clément Fournier
    • Method Detail

      • test

        default boolean test​(T t)
      • validate

        @Nullable String validate​(T value)
        Returns a diagnostic message if the value has a problem. Otherwise returns an empty optional.
        Parameters:
        value - The value to validate
        Returns:
        An optional diagnostic message
      • 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
      • toCollectionConstraint

        @Experimental
        default PropertyConstraint<Iterable<? extends T>> toCollectionConstraint()
        Returns a constraint that validates a collection of Ts by checking each component conforms to this constraint.
      • fromPredicate

        @Experimental
        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 problem
        constraintDescription - Description of the constraint, see getConstraintDescription().
        Returns:
        A new validator