FileDocCategorySizeDatePackage
StringConstraint.javaAPI DocExample2142Sun Dec 14 22:47:32 GMT 2003oreilly.hcj.datamodeling.constraints

StringConstraint

public class StringConstraint extends ObjectConstraint
Models a constraint on a String object.

Objects of this class are immutable.

author
Robert Simmons jr.
version
$Revision: 1.1 $

Fields Summary
private int
maxLength
Holds value of property maxLength.
Constructors Summary
public StringConstraint(String name, boolean optional, int maxLength)
Creates a new instance of StringConstraint.

param
name Contains the name of the constraint.
param
optional Indicates if the property is optional.
param
maxLength The maximum length of the String.
see
mirror.datamodel.constraints.Constraint

		super(name, optional, String.class);
		this.maxLength = maxLength;
	
Methods Summary
public intgetMaxLength()
Getter for property maxLength.

return
Value of property maxLength.

		return this.maxLength;
	
public voidvalidate(java.lang.Object obj)
Validate the given object against the constraint. Checks to make sure the string is not null if not allowed. If a non-null string is passed then the string is validated to make sure it isn't too long.

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

		super.validate(obj);

		if (obj != null) {
			String tgtString = (String)obj;

			if (tgtString.length() > maxLength) {
				throw new ConstraintException(ConstraintExceptionType.STRING_TOO_LONG);
			}
		}