io.convergence_platform.common.validations.MaximumValueValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-lib Show documentation
Show all versions of service-lib Show documentation
Holds the common functionality needed by all Convergence Platform-based services written in Java.
The newest version!
package io.convergence_platform.common.validations;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.lang.reflect.Array;
import java.util.List;
import java.util.concurrent.Flow;
public class MaximumValueValidator implements ConstraintValidator {
private double value;
public static void pathVariable(String value, double constraint, String varName) {
boolean valid = true;
if ("".equals(value)) {
valid = false;
} else if (ValidationHelper.isValidDouble(value)) {
valid = validate(Double.parseDouble(value), constraint);
}
ValidationHelper.raiseValidationErrorForPathVariable(valid, "max", varName);
}
@Override
public void initialize(MaximumValue constraintAnnotation) {
ConstraintValidator.super.initialize(constraintAnnotation);
value = constraintAnnotation.value();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
return validate(value, this.value);
}
private static boolean validate(Object value, double constraint) {
if (value == null) {
return false;
}
if (value instanceof Integer) {
return ((Integer) value) <= constraint;
} else if (value instanceof Float) {
return ((Float) value) <= constraint;
} else if (value instanceof Double) {
return ((Double) value) <= constraint;
}
return false;
}
}