package com.pig4cloud.pig.marketing.api.valid;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.HashSet;
import java.util.Set;

public class ValueRangeValidator implements ConstraintValidator<ValueRange, Integer> {
    private Set<Integer> allowedValues;
    
    @Override
    public void initialize(ValueRange constraintAnnotation) {
        // 初始化允许的值集合
        int[] values = constraintAnnotation.values();
        allowedValues = new HashSet<>();
        for (int value : values) {
            allowedValues.add(value);
        }
    }
    
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        // 如果值为null，由@NotNull注解来处理，这里不做判断
        if (value == null) {
            return true;
        }
        // 检查值是否在允许的范围内
        return allowedValues.contains(value);
    }
}
