/** * Use null value to disable a bound. * <p> * Bounds could be any {@link Date} or {@link Calendar}. Bounds also support {@link Number} types whose long value * is recognised as number of milliseconds since January 1, 1970, 00:00:00 GMT. Bounds finally supports String * having YYYY-MM-DD format. * </p> * <p> * Invalid bound (wrong format) would be ignored with log warning. * </p> * * @param minDate The lower bound of the interval * @param includingMin true if the lower bound is included in the interval * @param maxDate The upper bound of the interval * @param includingMax true if the upper bound is included in the interval */ public DateIntervalConstraint(Object minDate, boolean includingMin, Object maxDate, boolean includingMax) { minTime = ConstraintUtils.objectToTimeMillis(minDate); this.includingMin = includingMin; maxTime = ConstraintUtils.objectToTimeMillis(maxDate); this.includingMax = includingMax; if (minTime != null && maxTime != null && minTime > maxTime) { log.warn("lower bound (" + minDate + ") is greater than upper bound (" + maxDate + "). No dates could be valid."); } if ((minTime == null && minDate != null) || (maxTime == null && maxDate != null)) { log.warn("some bound was ignored due to invalid date format (supported format is " + ConstraintUtils.DATE_FORMAT + " (min = " + minDate + " - max = " + maxDate + ")"); } }
@Override public boolean validate(Object object) { if (object == null) { return true; } Long timeValue = ConstraintUtils.objectToTimeMillis(object); if (timeValue == null) { return false; } if (minTime != null) { if (timeValue < minTime.longValue()) { return false; } if (!includingMin && timeValue == minTime.longValue()) { return false; } } if (maxTime != null) { if (timeValue > maxTime.longValue()) { return false; } if (!includingMax && timeValue == maxTime.longValue()) { return false; } } return true; }