org.kiwiproject.validation.IntValueValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kiwi Show documentation
Show all versions of kiwi Show documentation
Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons.
But if they don't have something we need, and we think it is useful, this is where we put it.
package org.kiwiproject.validation;
import static java.util.Objects.isNull;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* Validates that a string value is an integer value, i.e. that it can be converted to an int or {@link Integer}.
*/
public class IntValueValidator implements ConstraintValidator {
private IntValue intValue;
@Override
public void initialize(IntValue constraintAnnotation) {
this.intValue = constraintAnnotation;
}
@Override
public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
if (isNull(value)) {
return intValue.allowNull();
}
try {
Integer.parseInt(value.toString());
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy