Explore constraint implementation simplifications via lambdas

Description

David explored the idea of method literal as constraint implementations and how to work around limitations

https://www.youtube.com/watch?v=5wX8RmBHMVY&feature=youtu.be&t=3907

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public interface ConstraintValidator<A extends Annotation, T> { default void initialize(A constraintAnnotation) { } default boolean isValid(T value, ConstraintValidatorContext context) { return isValid(value); } //TODO breaks backward compatibility by adding a new method to ConstraintValidator boolean isValid(T value); } public interface ConstraintValidatorFactory { <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key); void releaseInstance(ConstraintValidator<?, ?> instance); // enlist a new validator for a given constraint programmatically <A extends Annotation> void register(Class<A> annotation, ConstraintValidator<A,?> validator); // resolve validator for a given annotation // TODO: to fully resolve, one need the actual return or parameter type as there can be several validators per constraint annotation <A extends Annotation> ConstraintValidator<? extends Annotation, ?> resolve(Class<A> annotation); } ConstraintValidatorFactory cvf = factory.getConstraintValidatorFactory(); cvf.register(Directory.class, (ConstraintValidator<Directory, File) File::isDirectory); cvf.register(Writable.class, (ConstraintValidator<Writable, File) File::canWrite);

Environment

None

Status

Assignee

Unassigned

Reporter

Emmanuel Bernard

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

Components

Fix versions

Priority

Major