package org.sky.scientific.excel.annotation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.sky.core.tool.utils.StringUtil;

public class NumericValidator implements ConstraintValidator<ValidNumeric, String> {

	private boolean nullable;
	private String defaultValue;

	@Override
	public void initialize(ValidNumeric constraintAnnotation) {
		this.nullable = constraintAnnotation.nullable();
		this.defaultValue = constraintAnnotation.defaultValue();
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (nullable && StringUtil.isBlank(value)) {
			return true;
		}

		if (!nullable && StringUtil.isBlank(value)) {
			return false;
		}

		try {
			Double.parseDouble(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

}
