/*
* file: ArrayConstraint.java
* package: oreilly.hcj.datamodeling.constraints
*
* This software is granted under the terms of the Common Public License,
* CPL, which may be found at the following URL:
* http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
*
* Copyright(c) 2003-2005 by the authors indicated in the @author tags.
* All Rights are Reserved by the various authors.
*
########## DO NOT EDIT ABOVE THIS LINE ########## */
package oreilly.hcj.datamodeling.constraints;
import java.lang.reflect.Array;
/**
* Models a constraint on objects that are arrays.
*
* <p>
* Objects of this class are immutable.
* </p>
*
* @author <a href="mailto:worderisor@yahoo.com">Robert Simmons jr.</a>
* @version $Revision: 1.1 $
*/
public class ArrayConstraint extends ObjectConstraint {
/** Holds value of property maxSize. */
protected long maxSize;
/**
* Creates a new instance of ArrayConstraint.
*
* @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 maxSize The maximum size for the byte array.
*
* @throws IllegalArgumentException If the dataType isn't an array.
*
* @see mirror.datamodel.constraints.Constraint
*/
public ArrayConstraint(final String name, final boolean optional,
final Class dataType, final long maxSize) {
super(name, optional, dataType);
if (!dataType.isArray()) {
throw new IllegalArgumentException("The dataType must be an Array"); //$NON-NLS-1$
}
this.maxSize = maxSize;
}
/**
* Getter for property maxSize.
*
* @return Value of property maxSize.
*/
public long getMaxSize() {
return this.maxSize;
}
/**
* 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.
*/
public void validate(final Object obj) {
super.validate(obj);
if (obj != null) {
if (!obj.getClass()
.isArray()) {
throw new ConstraintException(ConstraintExceptionType.INVALID_DATA_TYPE);
}
if (Array.getLength(obj) > maxSize) {
throw new ConstraintException(ConstraintExceptionType.ARRAY_TOO_LARGE);
}
}
}
}
/* ########## End of File ########## */
|