View Single Post
  #3  
Old Jul 5th, 2005, 05:58 AM
Colin Yates Colin Yates is offline
Senior Member
Spring Team
 
Join Date: Aug 2004
Posts: 1,905
Default Here's one

Code:
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * This validator will delegate each of it's child validators.
 *
 */
public final class CompositeValidator implements Validator {
	private final Validator[] validators;

	public CompositeValidator(final Validator[] theValidators) {
		this.validators = theValidators;
	}

	/**
	 * Will return true if this class is in the specified map.
	 */
	public boolean supports(final Class clazz) {
		for (Validator v: validators) {
			if (v.supports(clazz)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Validate the specified object using the validator registered for the object's class.
	 */
	public void validate(final Object obj, final Errors errors) {
		for (Validator v: validators) {
			if (v.supports(obj.getClass())) {
				v.validate(obj, errors);
			}
		}
	}
}
BTW; what is the difference between a chain and composite?
Reply With Quote