zoukankan      html  css  js  c++  java
  • 设定范围和步长的递增数验证器Validator

    1、接口注释

    @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
    @Retention(RUNTIME)
    @Documented
    @Constraint(validatedBy = {IncrementalValidator.class})
    public @interface IncrementalInteger {
    
        String message() default "{common.incrementalInteger.Pattern}";
    
        Class<?>[] groups() default {};
    
        Class<? extends Payload>[] payload() default {};
    
        /**
         * @return value the element must be larger or equal to
         */
        int min();
        
        /**
         * @return value the element must be smaller or equal to 
         */
        int max();
    
        /**
         * @return value must be incremental
         */
        int increment();
    
        /**
         * Defines several {@link IncrementalInteger} annotations on the same
         * element.
         *
         * @see IncrementalInteger
         */
        @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
        @Retention(RUNTIME)
        @Documented
        @interface List {
    
            IncrementalInteger[] value();
        }
    }

    2、Validator类

    public class IncrementalValidator implements ConstraintValidator<IncrementalInteger, Integer> {
    
        private IncrementalInteger constraintAnnotation;
    
        @Override
        public void initialize(IncrementalInteger constraintAnnotation) {
            this.constraintAnnotation = constraintAnnotation;
        }
    
        @Override
        public boolean isValid(Integer value, ConstraintValidatorContext context) {
            int min = constraintAnnotation.min();
            int increment = constraintAnnotation.increment();
            int max = constraintAnnotation.max();
            if (value < min) {
                return false;
            }
    
            if (value > max) {
                return false;
            }
    
            if ((value - min) % increment != 0) {
                return false;
            }
    
            return true;
        }
    }
  • 相关阅读:
    C# DES加密和解密
    SQL设计技巧优化
    MS15-034漏洞技术研究
    Vustudy靶场环境快速搭建
    FastJson<=1.2.47漏洞复现
    多台电脑共享一套鼠键--Mouse Without Borders
    {Java初级系列四}---继承、接口和抽象类
    {Java初级系列三}---面向对象和类
    {Java初级系列二}---Java类基础知识
    {Java初阶系列一}---Java基本简介
  • 原文地址:https://www.cnblogs.com/feika/p/4441593.html
Copyright © 2011-2022 走看看