Base class for ConstraintValidator implementors

Description

For simpler ConstraintValidators which ignore null cases and don't need to initialise constraintAnnotation there is a lot of boilerplate that could be simplified with a base class or interface with default methods:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class FooValidator implements ConstraintValidator<Foo, Bar> { // boilerplate @Override public void initialize(Foo constraintAnnotation) { } @Override public boolean isValid(Bar value, ConstraintValidatorContext context) { // boilerplate if (value == null) { return true; } // validation logic } }

For this case it would be nice if there was something like:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @FunctionalInterface public interface SimpleConstraintValidator<A extends Annotation, T> extends ConstraintValidator<A, T> { @Override default void initialize(A constraintAnnotation) { } @Override default boolean isValid(T value, ConstraintValidatorContext context) { if(value == null) { return true; } return isValid(value); } boolean isValid(T value); }

In that case first implementation would be a lot more concise:

1 2 3 4 5 6 7 public class FooValidator implements SimpleConstraintValidator<Foo, Bar> { @Override public boolean isValid(Bar value) { // validation logic } }

Environment

None

Status

Assignee

Unassigned

Reporter

Lovro Pandzic

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Priority

Major