DateConstraintpublic class DateConstraint extends ObjectConstraint Models a constraint on Date objects.
Objects of this class are immutable.
|
Fields Summary |
---|
protected Date | maxDateHolds value of property maxSize. | private Date | minDateHolds value of property minDate. |
Constructors Summary |
---|
public DateConstraint(String name, boolean optional, Date minDate, Date maxDate)Creates a new instance of DateConstraint.
super(name, optional, Date.class);
this.minDate = minDate;
this.maxDate = maxDate;
| public DateConstraint(String name, boolean optional, String minDate, String maxDate, Locale locale)Creates a new instance of DateConstraint.
Allows the min and max dates to be given as short style dates in the given locale.
Note that the class uses java.text.DateFormat to parse the dates.
super(name, optional, Date.class);
if (locale == null) {
throw new NullPointerException("locale can't be null."); //$NON-NLS-1$
}
String parsingDate = null;
try {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
parsingDate = "minDate"; //$NON-NLS-1$
this.minDate = df.parse(minDate);
parsingDate = "maxDate"; //$NON-NLS-1$
this.maxDate = df.parse(maxDate);
} catch (ParseException ex) {
throw new IllegalArgumentException(parsingDate + " : " + ex.getMessage()); //$NON-NLS-1$
}
|
Methods Summary |
---|
public java.util.Date | getMaxDate()Getter for property maxSize.
return this.maxDate;
| public java.util.Date | getMinDate()Getter for property minDate.
return this.minDate;
| public void | validate(java.lang.Object obj)Validate the given object against the constraint.
Checks to make sure the array is within the size limitation and then passes
checking to the superclass.
super.validate(obj);
if (obj != null) {
Date d = (Date)obj;
if (d.compareTo(minDate) < 0) {
throw new ConstraintException(ConstraintExceptionType.VALUE_BELOW_MINIMUM);
}
if (d.compareTo(maxDate) > 0) {
throw new ConstraintException(ConstraintExceptionType.VALUE_ABOVE_MAXIMUM);
}
}
|
|