FileDocCategorySizeDatePackage
DateConstraint.javaAPI DocExample4155Sun Dec 14 22:47:40 GMT 2003oreilly.hcj.datamodeling.constraints

DateConstraint

public class DateConstraint extends ObjectConstraint
Models a constraint on Date objects.

Objects of this class are immutable.

author
Robert Simmons jr.
version
$Revision: 1.1 $

Fields Summary
protected Date
maxDate
Holds value of property maxSize.
private Date
minDate
Holds value of property minDate.
Constructors Summary
public DateConstraint(String name, boolean optional, Date minDate, Date maxDate)
Creates a new instance of DateConstraint.

param
name Contains the name of the constraint.
param
optional Indicates if the property is optional.
param
minDate The minimum date to allow.
param
maxDate The maximum date to allow.
see
mirror.datamodel.constraints.Constraint

		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.

param
name Contains the name of the constraint.
param
optional Indicates if the property is optional.
param
minDate The minimum date to allow; Short Date style.
param
maxDate The maximum date to allow; Stort Date Style.
param
locale The locale to use for parsing the date.
throws
NullPointerException If locale is null.
throws
IllegalArgumentException If the minDate or maxDate don't parse correctly.
see
java.text.DateFormat
see
mirror.datamodel.constraints.Constraint

		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.DategetMaxDate()
Getter for property maxSize.

return
Value of property maxSize.

		return this.maxDate;
	
public java.util.DategetMinDate()
Getter for property minDate.

return
Value of property minDate.

		return this.minDate;
	
public voidvalidate(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.

param
obj The object to validate.
throws
ConstraintException If the constraint is violated.

		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);
			}
		}