/*
 * Created on Dec 31, 2004
 * Constraint.java
 */
package uml;

/**
 * This abstract class provides the basic mechanism for
 * creating a constraint. Subclasses of Constraint should
 * implement {@link #condition(Object...objects)}, and
 * call objectName.check(Param1, Param2) to verify the
 * constriant. If the constraint is not met this class
 * will automatically throw a {@link ConstraintException}.
 */
public abstract class Constraint {
	
	private String description;
	
	/**
	 * Creates a new Constraint object. Subclasses of
	 * Constraint should call this constructor.
	 * 
	 * @param description the textual description to show
	 * in the error message. This should state what the
	 * constraint is supposed to check.
	 */
	public Constraint(String description) {
		this.description = description;
	}
	
	/**
	 * Call this method to verify the constraint as
	 * implemented in {@link #condition(Object...objects)}
	 * 
	 * @param objects the items that must be validated
	 * in the constrant.
	 */
	public final void check(Object...objects) {
		boolean checkValue = false;
		try {
			checkValue = condition(objects);
		} catch (Exception e) {
			throw new ConstraintException(description, e);
		}
		if (!checkValue) {
			throw new ConstraintException(description);
		}
	}

	/**
	 * This method should return true if the condition
	 * of this constraint has passed and false otherwise.
	 * 
	 * @param objects The items that must be validated
	 * in the constraint.
	 * @return returns true if this condition is met
	 * and false otherwise.
	 */
	protected abstract boolean condition(Object...objects);
}
