The Constraint will co-operate with a custom annotation, which is used to mark which method argument to be linked to the Constraint. The annotation can also contain arguments, for instance to provide range checks.
The Constraint will be provided the annotation and the value that is passed in the method.
The Constraint simply returns true if the argument is Ok, and otherwise false. It should avoid throwing exceptions.
import java.lang.annotation.Annotation;
/**
* All Constraints must implement this interface, which
* is used for each value validation.
*/
public interface Constraint<A extends Annotation, P>
{
/**
* For each value or parameter which should be checked
* this method will be invoked.
* If the method returns true the value is valid. If it
* returns false the value is considered invalid.
* When all constraints have been checked a
* ConstraintViolationException will be thrown with all
* the constraint violations that were found.
*
* @param annotation the annotation to match
* @param value the value to be checked
* @return true if valid, false if invalid
* @throws NullPointerException NPE can be thrown if the value was null
*/
boolean isValid( A annotation, P value )
throws NullPointerException;
}