FileDocCategorySizeDatePackage
MapConstraint.javaAPI DocExample4124Sun Dec 14 22:47:38 GMT 2003oreilly.hcj.datamodeling.constraints

MapConstraint

public class MapConstraint extends ObjectConstraint
Models a constraint on Map objects.

Objects of this class are immutable.

author
Robert Simmons jr.
version
$Revision: 1.1 $

Fields Summary
private Class
keyType
Holds value of property keyType.
private Class
valueType
Holds value of property valueClass.
private boolean
valueNullable
Holds value of property valueNullable.
Constructors Summary
public MapConstraint(String name, boolean optional, Class dataType, Class keyType, Class valueType, boolean valueNullable)
Creates a new instance of MapConstraint.

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.
param
keyType Type to use for keys; null if it doesnt matter.
param
valueType Type to use for values; null if it doesnt matter.
param
valueNullable True if a value can be null for a key, otherwise false.
throws
IllegalArgumentException If dataType isn't a subclass of Map.
see
mirror.datamodel.constraints.Constraint

		super(name, optional, dataType);

		if (!Map.class.isAssignableFrom(dataType)) {
			throw new IllegalArgumentException("The dataType must be a Map");  //$NON-NLS-1$
		}

		this.keyType = keyType;
		this.valueType = valueType;
		this.valueNullable = valueNullable;
	
Methods Summary
public java.lang.ClassgetKeyType()
Getter for property keyType.

return
Value of property keyType.

		return this.keyType;
	
public java.lang.ClassgetValueType()
Getter for property valueClass.

return
Value of property valueClass.

		return this.valueType;
	
public booleanisValueNullable()
Getter for property valueNullable.

return
Value of property valueNullable.

		return this.valueNullable;
	
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) {
			throw new ConstraintException(ConstraintExceptionType.NULL_NOT_ALLOWED);
		}

		if (!getDataType()
		         .isAssignableFrom(obj.getClass())) {
			throw new ConstraintException(ConstraintExceptionType.INVALID_DATA_TYPE);
		}

		Map tgtMap = (Map)obj;

		if (tgtMap.isEmpty()) {
			if (!isOptional()) {
				throw new ConstraintException(ConstraintExceptionType.MAP_CANNOT_BE_EMPTY);
			}
		}

		Set keys = tgtMap.keySet();
		Iterator iter = keys.iterator();
		Object value = null;
		Object key = null;

		while (iter.hasNext()) {
			key = iter.next();

			if (keyType != null) {
				if (!(key.getClass().equals(keyType))) {
					throw new ConstraintException(ConstraintExceptionType.INVALID_COLLECTION_MEMBER);
				}
			}

			value = tgtMap.get(key);

			if (valueType != null) {
				if (value == null) {
					if (!valueNullable) {
						throw new ConstraintException(ConstraintExceptionType.MAP_CANNOT_HAVE_NULL_VALUE);
					}
				} else {
					if (!(value.getClass().equals(valueType))) {
						throw new ConstraintException(ConstraintExceptionType.MAP_CONTAINS_INVALID_VALUE);
					}
				}
			}
		}