Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1122 Allowing to configure constraint behavior (isValid()) using L…
…ambda expressions in configuration DSL
- Loading branch information
1 parent
082f5d6
commit 9210102
Showing
5 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...ernate/validator/internal/engine/constraintvalidation/LambdaBasedValidatorDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.internal.engine.constraintvalidation; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.EnumSet; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
import javax.validation.ConstraintValidatorFactory; | ||
import javax.validation.constraintvalidation.ValidationTarget; | ||
|
||
import org.hibernate.validator.cfg.context.ConstraintDefinitionContext.ValidationCallable; | ||
|
||
/** | ||
* Represents a constraint validator based on a Lambda expression. | ||
* | ||
* @author Gunnar Morling | ||
*/ | ||
class LambdaBasedValidatorDescriptor<A extends Annotation> implements ConstraintValidatorDescriptor<A> { | ||
|
||
private static final long serialVersionUID = 5129757824081595723L; | ||
|
||
private final Type validatedType; | ||
private final ValidationCallable<?> lambda; | ||
|
||
public LambdaBasedValidatorDescriptor(Type validatedType, ValidationCallable<?> lambda) { | ||
this.validatedType = validatedType; | ||
this.lambda = lambda; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Class<? extends ConstraintValidator<A, ?>> getValidatorClass() { | ||
Class<?> clazz = LambdaExecutor.class; | ||
return (Class<? extends ConstraintValidator<A, ?>>) clazz; | ||
} | ||
|
||
@Override | ||
public EnumSet<ValidationTarget> getValidationTargets() { | ||
return EnumSet.of( ValidationTarget.ANNOTATED_ELEMENT ); | ||
} | ||
|
||
@Override | ||
public Type getValidatedType() { | ||
return validatedType; | ||
} | ||
|
||
@Override | ||
public ConstraintValidator<A, ?> newInstance(ConstraintValidatorFactory constraintFactory) { | ||
return new LambdaExecutor<>( lambda ); | ||
} | ||
|
||
private static class LambdaExecutor<A extends Annotation, T> implements ConstraintValidator<A, T> { | ||
|
||
private final ValidationCallable<T> lambda; | ||
|
||
public LambdaExecutor(ValidationCallable<T> lambda) { | ||
this.lambda = lambda; | ||
} | ||
|
||
@Override | ||
public void initialize(A constraintAnnotation) { | ||
} | ||
|
||
@Override | ||
public boolean isValid(T value, ConstraintValidatorContext context) { | ||
return lambda.isValid( value ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters