FileDocCategorySizeDatePackage
NumericConstraint.javaAPI DocExample3729Sun Dec 14 22:47:38 GMT 2003oreilly.hcj.datamodeling.constraints

NumericConstraint

public class NumericConstraint extends ObjectConstraint
Models a constraint on a numerical entity.

Objects of this class are immutable.

author
Robert Simmons jr.
version
$Revision: 1.1 $

Fields Summary
private static final String
ERR_NOT_NUMBER
Error message for a non number type passed.
private static final String
ERR_MISMATCH
Error message for a type mismatch.
private Number
maxValue
Holds value of property maxValue.
private Number
minValue
Holds value of property minValue.
Constructors Summary
public NumericConstraint(String name, boolean optional, Class dataType, Number minValue, Number maxValue)
Creates a new instance of NumericConstraint. Note that since the values for min and max have to be of type Number, the numberType class must likewise be a descendant of that class otherwise the construction of the constraint wotn work.

param
name Contains the name of the constraint.
param
optional Indicates if the property is optional.
param
dataType The type of number this class is validating.
param
minValue The minimum value for the number.
param
maxValue The maximum value for the number.
throws
IllegalArgumentException If dataType is not a subclass of java.lang.Number.
see
java.lang.Number
see
mirror.datamodel.constraints.Constraint


	                                                                                                         	 
	      
	                              
	                            
		super(name, optional, dataType);
		if (!Number.class.isAssignableFrom(dataType)) {
			throw new IllegalArgumentException(ERR_NOT_NUMBER);
		}
		if (!(minValue.getClass().equals(dataType))) {
			throw new IllegalArgumentException(ERR_MISMATCH);
		}
		if (!(maxValue.getClass().equals(dataType))) {
			throw new IllegalArgumentException(ERR_MISMATCH);
		}

		// ** validation done
		this.minValue = minValue;
		this.maxValue = maxValue;
	
Methods Summary
public java.lang.NumbergetMaxValue()
Getter for property maxValue.

return
Value of property maxValue.

		return this.maxValue;
	
public java.lang.NumbergetMinValue()
Getter for property minValue.

return
Value of property minValue.

		return this.minValue;
	
public voidvalidate(java.lang.Object obj)
Validate the given object against the constraint. Implements all the constraints of the superclass Constraint. The number is validated to make sure it is between the min and max values.

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

		super.validate(obj);
		if (obj != null) {
			if (((Comparable)obj).compareTo(minValue) < 0) {
				throw new ConstraintException(ConstraintExceptionType.VALUE_BELOW_MINIMUM);
			}
			if (((Comparable)obj).compareTo(maxValue) > 0) {
				throw new ConstraintException(ConstraintExceptionType.VALUE_ABOVE_MAXIMUM);
			}
		}