FileDocCategorySizeDatePackage
ObjectConstraint.javaAPI DocExample2829Sun Dec 14 22:47:42 GMT 2003oreilly.hcj.datamodeling.constraints

ObjectConstraint

public class ObjectConstraint extends Constraint
Models a constraint on a property.

This constraint enforces whether an object can be null as well as if the data type of the object beign validated is the same or subclass of the data type given.

Objects of this class are immutable.

author
Robert Simmons jr.
version
$Revision: 1.1 $

Fields Summary
private static final String
ERR_PRIMITIVE
Holds the error message for attempting to validate primitives.
private Class
dataType
Holds value of property dataType.
private boolean
optional
Holds value of property optional.
Constructors Summary
public ObjectConstraint(String name, boolean optional, Class dataType)
Creates a new instance of Constraint.

param
name Contains the name of the constraint.
param
optional Indicates if the property is optional.
param
dataType The data type of the object being constrained.
throws
IllegalArgumentException If dataType is a primitive.
see
mirror.datamodel.constraints.Constraint


	                                          	 
	      
	                           
		super(name);
		if (dataType.isPrimitive()) {
			throw new IllegalArgumentException(ERR_PRIMITIVE);
		}
		this.optional = optional;
		this.dataType = dataType;
	
Methods Summary
public java.lang.ClassgetDataType()
Getter for property dataType.

return
Value of property dataType.

		return this.dataType;
	
public booleanisOptional()
Getter for property optional.

return
Value of property optional.

		return this.optional;
	
public voidvalidate(java.lang.Object obj)
Validate the given object against the constraint.

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

		if (obj == null) {
			if (!optional) {
				throw new ConstraintException(ConstraintExceptionType.NULL_NOT_ALLOWED);
			}
		} else {
			if (!dataType.isAssignableFrom(obj.getClass())) {
				throw new ConstraintException(ConstraintExceptionType.INVALID_DATA_TYPE);
			}
		}