package com.face.global.validator.impl;

import com.face.global.validator.enums.MaxDouble;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
public class MaxDoubleConstraintValidator implements ConstraintValidator<MaxDouble, Object> {
    /**
     * 注解对象
     */
    private MaxDouble annotation;

    /**
     * 初始化方法
     *
     * @param constraintAnnotation 注解对象
     */
    @Override
    public void initialize(MaxDouble constraintAnnotation) {
        this.annotation = constraintAnnotation;
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (Objects.isNull(value)) {
            return true;
        }
        if (!(value instanceof Double)) {
            return true;
        }
        return ((Double) value).compareTo(annotation.value()) <= 0;
    }
}