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?