Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @AfterStartDate({"startOfDilation","endOfDilation"})
- public class MyBean{
- private Date startDate,
- private Date stopDate,
- ...
- }
- @Target({ ElementType.METHOD, ElementType.CONSTRUCTOR,ElementType.ANNOTATION_TYPE })
- @Retention(RetentionPolicy.RUNTIME)
- @Constraint(validatedBy = AfterStartDateValidator.class)
- public @interface AfterStartDate {
- String message() default "{AfterStartDate.message}";
- Class<?>[] groups() default {};
- Class<? extends Payload>[] payload() default {};
- }
- @SupportedValidationTarget(ValidationTarget.PARAMETERS)
- public class AfterStartDateValidator implements ConstraintValidator<AfterStartDate,Object[]> {
- @Override
- public void initialize(AfterStartDate constraintAnnotation) {
- }
- @Override
- public boolean isValid(Object[] value, ConstraintValidatorContext context){
- if ( value.length != 2 ) {
- throw new IllegalArgumentException( "Illegal method signature" );
- }
- //leave null-checking to @NotNull on individual parameters
- if ( value[0] == null || value[1] == null ) {
- return true;
- }
- if ( !( value[0] instanceof Date ) || !( value[1] instanceof Date ) ) {
- throw new IllegalArgumentException(
- "Illegal method signature, expected two " +
- "parameters of type Date."
- );
- }
- return ( (Date) value[0] ).before( (Date) value[1] );
- }
- }
Add Comment
Please, Sign In to add comment