javax.validation.CompareToValidator Maven / Gradle / Ivy
The newest version!
package javax.validation;
import java.lang.reflect.Field;
import javax.validation.constraints.CompareTo;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
public class CompareToValidator implements ConstraintValidator {
private String sourceFieldName;
private String destinationFieldName;
private String targetFieldName;
private boolean equals;
private String message;
@Override
public void initialize(CompareTo constraintAnnotation) {
this.sourceFieldName = constraintAnnotation.source();
this.destinationFieldName = constraintAnnotation.destination();
this.targetFieldName = constraintAnnotation.target();
this.equals = constraintAnnotation.equals();
this.message = constraintAnnotation.message();
}
/**
* @see ReflectionUtils#findField(Class, String)
* @see ReflectionUtils#makeAccessible(Field)
*/
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
Object source = getField(value, this.sourceFieldName);
Object destination = getField(value, this.destinationFieldName);
if (source instanceof Comparable && destination instanceof Comparable) {
@SuppressWarnings("unchecked")
int compareTo = ((Comparable