com.buschmais.xo.impl.validation.InstanceValidationService Maven / Gradle / Ivy
The newest version!
package com.buschmais.xo.impl.validation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import com.buschmais.xo.impl.cache.TransactionalCache;
public class InstanceValidationService {
private final ValidatorFactory validatorFactory;
private final TransactionalCache>[] caches;
public InstanceValidationService(ValidatorFactory validatorFactory, TransactionalCache>... caches) {
this.validatorFactory = validatorFactory;
this.caches = caches;
}
public Set> validate() {
Validator validator = getValidator();
if (validator == null) {
return Collections.emptySet();
}
Set> violations = new HashSet<>();
for (TransactionalCache> cache : caches) {
for (Object instance : new ArrayList<>(cache.writtenInstances())) {
violations.addAll(validator.validate(instance));
}
}
return violations;
}
public Set> validate(Object instance) {
Validator validator = getValidator();
return validator != null ? validator.validate(instance) : Collections.>emptySet();
}
private Validator getValidator() {
return validatorFactory != null ? validatorFactory.getValidator() : null;
}
}