All Downloads are FREE. Search and download functionalities are using the official Maven repository.

love.keeping.starter.web.components.validation.NumberPrecisionValidator Maven / Gradle / Ivy

The newest version!
package love.keeping.starter.web.components.validation;

import love.keeping.starter.common.utils.NumberUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 小数位数校验 如果参数是null或不是Number,则不进行校验
 *
 * @author Keeping
 */
public class NumberPrecisionValidator implements ConstraintValidator {

  private int precision;

  @Override
  public void initialize(IsNumberPrecision constraintAnnotation) {

    this.precision = constraintAnnotation.value();
  }

  @Override
  public boolean isValid(Object charSequence,
      ConstraintValidatorContext constraintValidatorContext) {
    if (charSequence == null) {
      return true;
    }
    if (charSequence instanceof Number) {
      return NumberUtil.isNumberPrecision((Number) charSequence, this.precision);
    }

    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy