Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. @AfterStartDate({"startOfDilation","endOfDilation"})
  2. public class MyBean{
  3. private Date startDate,
  4. private Date stopDate,
  5. ...
  6. }
  7.  
  8. @Target({ ElementType.METHOD, ElementType.CONSTRUCTOR,ElementType.ANNOTATION_TYPE })
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @Constraint(validatedBy = AfterStartDateValidator.class)
  11. public @interface AfterStartDate {
  12. String message() default "{AfterStartDate.message}";
  13. Class<?>[] groups() default {};
  14. Class<? extends Payload>[] payload() default {};
  15. }
  16.  
  17. @SupportedValidationTarget(ValidationTarget.PARAMETERS)
  18. public class AfterStartDateValidator implements ConstraintValidator<AfterStartDate,Object[]> {
  19.  
  20. @Override
  21. public void initialize(AfterStartDate constraintAnnotation) {
  22. }
  23.  
  24. @Override
  25. public boolean isValid(Object[] value, ConstraintValidatorContext context){
  26. if ( value.length != 2 ) {
  27. throw new IllegalArgumentException( "Illegal method signature" );
  28. }
  29.  
  30. //leave null-checking to @NotNull on individual parameters
  31. if ( value[0] == null || value[1] == null ) {
  32. return true;
  33. }
  34.  
  35. if ( !( value[0] instanceof Date ) || !( value[1] instanceof Date ) ) {
  36. throw new IllegalArgumentException(
  37. "Illegal method signature, expected two " +
  38. "parameters of type Date."
  39. );
  40. }
  41.  
  42. return ( (Date) value[0] ).before( (Date) value[1] );
  43. }
  44. }
Add Comment
Please, Sign In to add comment