use of org.grails.validation.MaxConstraint in project grails-core by grails.
the class ConstrainedProperty method setMax.
/**
* @param max The max to set.
*/
@SuppressWarnings("rawtypes")
public void setMax(Comparable max) {
if (max == null) {
appliedConstraints.remove(MAX_CONSTRAINT);
return;
}
if (!propertyType.equals(max.getClass())) {
throw new MissingPropertyException(MAX_CONSTRAINT, propertyType);
}
Range r = getRange();
if (r != null) {
LOG.warn("Range constraint already set ignoring constraint [" + MAX_CONSTRAINT + "] for value [" + max + "]");
return;
}
Constraint c = appliedConstraints.get(MAX_CONSTRAINT);
if (c == null) {
c = new MaxConstraint();
c.setOwningClass(owningClass);
c.setPropertyName(propertyName);
appliedConstraints.put(MAX_CONSTRAINT, c);
}
c.setParameter(max);
}
use of org.grails.validation.MaxConstraint in project grails-core by grails.
the class ConstrainedProperty method getMax.
/**
* @return Returns the max.
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Comparable getMax() {
Comparable maxValue = null;
MaxConstraint maxConstraint = (MaxConstraint) appliedConstraints.get(MAX_CONSTRAINT);
RangeConstraint rangeConstraint = (RangeConstraint) appliedConstraints.get(RANGE_CONSTRAINT);
if (maxConstraint != null || rangeConstraint != null) {
Comparable maxConstraintValue = maxConstraint == null ? null : maxConstraint.getMaxValue();
Comparable rangeConstraintHighValue = rangeConstraint == null ? null : rangeConstraint.getRange().getTo();
if (maxConstraintValue != null && rangeConstraintHighValue != null) {
maxValue = (maxConstraintValue.compareTo(rangeConstraintHighValue) < 0) ? maxConstraintValue : rangeConstraintHighValue;
} else if (maxConstraintValue == null && rangeConstraintHighValue != null) {
maxValue = rangeConstraintHighValue;
} else if (maxConstraintValue != null && rangeConstraintHighValue == null) {
maxValue = maxConstraintValue;
}
}
return maxValue;
}
Aggregations