package com.face.global.validator.impl;


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

import java.util.Objects;

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

    /**
     * 初始化方法
     *
     * @param constraintAnnotation 注解对象
     */
    @Override
    public void initialize(MinDouble 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;
    }
}