(披露:我是作者)
Saripaar v2允许您定义自定义注释.
这是你做的事情.
步骤1如下定义您的自定义注释.确保您有一个RUNTIME保留策略,并且您的注释必须针对FIELD元素类型.消息和messageResId属性是必需的,因此请注意名称和类型.
@ValidateUsing(HaggleRule.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Haggle {
public int messageResId() default -1; // Mandatory attribute
public String message() default "Oops... too pricey"; // Mandatory attribute
public int sequence() default -1; // Mandatory attribute
public double maximumAskingPrice(); // Your attributes
}
步骤2通过扩展AnnotationRule类定义您的规则.
public class HaggleRule extends AnnotationRule {
protected HaggleRule(Haggle haggle) {
super(haggle);
}
@Override
public boolean isValid(Double data) {
boolean isValid = false;
double maximumAskingPrice = mRuleAnnotation.maximumAskingPrice();
// Do some clever validation....
return isValid;
}
}
步骤3注册您的规则.
Validator.registerAnnotation(Haggle.class); // Your annotation class instance
就那么简单.看看源代码,如果你想的话. Saripaar v2现已在Maven Central上市.