zxl
2025-06-11 861582390dc7c395897159077a547d3e7078727c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cn.lili.common.validation.impl;
 
import cn.lili.common.validation.EnumValue;
 
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
 
/**
 * 枚举之校验
 *
 * @author Bulbasaur
 * @since 2021/7/9 1:41 上午
 */
public class EnumValuesValidator implements ConstraintValidator<EnumValue, Object> {
 
    private String[] strValues;
    private int[] intValues;
 
    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        if (o instanceof String) {
            for (String s : strValues) {
                if (s.equals(o)) {
                    return true;
                }
            }
        } else if (o instanceof Integer) {
            for (int s : intValues) {
                if (s == ((Integer) o).intValue()) {
                    return true;
                }
            }
        }
        return false;
    }
 
    @Override
    public void initialize(EnumValue constraintAnnotation) {
        strValues = constraintAnnotation.strValues();
        intValues = constraintAnnotation.intValues();
    }
}