是否可以创建一个自定义JSR-380验证,该验证在实现Comparable Interface的对象List上运行?我想创建一个像“@Ordered”这样的验证,以确保传递给方法的列表按特定顺序排列。
示例:
public void foo (@Ordered List<BigDecimal> aList) {
}
答案 0 :(得分:0)
想出来:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=SequenceValidator.class)
@Documented
public @interface Sequence {
String message () default "Sequence is not valid.";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
然后......
public class SequenceValidator implements ConstraintValidator<Sequence, List<? extends Comparable<?>>> {
@Override
public void initialize(Sequence seq) {
}
@Override
public boolean isValid(List<? extends Comparable<?>> values, ConstraintValidatorContext arg1) {
System.out.println ("Validating: " + values);
return values == null || Ordering.natural().isOrdered(values);
}