org.chobit.commons.validation.StringCollValidator Maven / Gradle / Ivy
package org.chobit.commons.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.chobit.commons.funcs.Checker;
import java.util.Collection;
/**
* 字符串集合检查校验
*
* @author robin
*/
public class StringCollValidator implements ConstraintValidator> {
private Checker checker;
@Override
public void initialize(StringColl anno) {
Class extends Checker> checkerClass = anno.checker();
try {
this.checker = checkerClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean isValid(Collection coll, ConstraintValidatorContext context) {
if (null == coll || coll.isEmpty()) {
return true;
}
for (String s : coll) {
boolean r = this.checker.check(s);
if (!r) {
return false;
}
}
return true;
}
}