/** * Use null value to disable a bound. * <p> * Bounds could be any object having toString representating a number. * </p> * * @param min The lower bound of the interval * @param includingMin true if the lower bound is included in the interval * @param max The upper bound of the interval * @param includingMax true if the upper bound is included in the interval */ public NumericIntervalConstraint(Object min, boolean includingMin, Object max, boolean includingMax) { this.min = ConstraintUtils.objectToBigDecimal(min); this.includingMin = includingMin; this.max = ConstraintUtils.objectToBigDecimal(max); this.includingMax = includingMax; }
@Override public boolean validate(Object object) { BigDecimal val = ConstraintUtils.objectToBigDecimal(object); if (val == null) { return true; } if (min != null) { int test = min.compareTo(val); if (test > 0) { return false; } if (!includingMin && test == 0) { return false; } } if (max != null) { int test = max.compareTo(val); if (test < 0) { return false; } if (!includingMax && test == 0) { return false; } } return true; }