@Override public boolean isNillable() { return ConstraintUtils.getConstraint(constraints, NotNullConstraint.class) == null; }
/** * 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; }
/** * For a fixed length, use min = max values. * <p> * Bounds could be any object having toString representating an integer. * </p> * * @param min Minimum length for the validated String, use a null value to get unbounded length. * @param max Maximum length for the validated String, use a null value to get unbounded length. */ public LengthConstraint(Object min, Object max) { this.min = ConstraintUtils.objectToPostiveLong(min); this.max = ConstraintUtils.objectToPostiveLong(max); }
/** * 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 + ")"); } }
/** * Supports {@link Date}, {@link Calendar}, {@link Number} and {@link String} formatted as YYYY-MM-DD * * @param object Any object * @return a date represented as number of milliseconds since january 1 1970 if the object is supported, null * otherwise. * @since 7.1 */ public static Long objectToTimeMillis(Object object) { Long timeValue = null; if (object == null) { return null; } if (object instanceof Date) { timeValue = ((Date) object).getTime(); } else if (object instanceof Calendar) { timeValue = ((Calendar) object).getTimeInMillis(); } else if (object instanceof Number) { timeValue = ((Number) object).longValue(); } else { SimpleDateFormat dateParser = ConstraintUtils.formatter(); try { timeValue = dateParser.parse(object.toString()).getTime(); } catch (ParseException e) { return null; } } return timeValue; }
@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; }
LengthConstraint lc = ConstraintUtils.getConstraint(field.getConstraints(), LengthConstraint.class); if (lc != null && lc.getMax() != null) { field.setMaxLength(lc.getMax().intValue());
@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; }