package rocks.inspectit.shared.cs.cmr.property.configuration.validator.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import org.hamcrest.Matchers;
import rocks.inspectit.shared.cs.cmr.property.configuration.SingleProperty;
import rocks.inspectit.shared.cs.cmr.property.configuration.validation.PropertyValidation;
import rocks.inspectit.shared.cs.cmr.property.configuration.validation.ValidationError;
import rocks.inspectit.shared.cs.cmr.property.configuration.validator.AbstractComparingValidator;
/**
* Is less or equal than validator.
*
* @author Ivan Senic
*
* @param <T>
* Type of values to compare.
*/
@XmlRootElement(name = "isLessOrEqual")
public class LessOrEqualValidator<T extends Number> extends AbstractComparingValidator<T> {
/**
* {@inheritDoc}
*/
@Override
protected void compare(SingleProperty<? extends T> property, SingleProperty<? extends T> againstProperty, T value, T against, PropertyValidation propertyValidation) {
if (!matches(value, against)) {
List<SingleProperty<?>> properties = new ArrayList<>(2);
properties.add(property);
properties.add(againstProperty);
ValidationError validationError = new ValidationError(properties,
"Value of property '" + property.getName() + "' must be less or equal than value of property '" + againstProperty.getName() + "'");
propertyValidation.addValidationError(validationError);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void compare(SingleProperty<? extends T> property, T value, T against, PropertyValidation propertyValidation) {
if (!matches(value, against)) {
ValidationError validationError = new ValidationError(Collections.<SingleProperty<?>> singletonList(property),
"Value of property '" + property.getName() + "' must be less or equal than " + against);
propertyValidation.addValidationError(validationError);
}
}
/**
* Executes compare.
*
* @param value
* Value.
* @param against
* Value to compare against.
* @return Returns true if match was valid.
*/
private boolean matches(T value, T against) {
return Matchers.lessThanOrEqualTo(against.doubleValue()).matches(value.doubleValue());
}
}