org.chobit.commons.validation.EnumCollValidator Maven / Gradle / Ivy
package org.chobit.commons.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.chobit.commons.contract.CodeEnum;
import org.chobit.commons.utils.EnumKit;
import java.util.Collection;
/**
* 枚举值验证
*
* @author robin
*/
public class EnumCollValidator implements ConstraintValidator> {
private Class extends Enum extends CodeEnum>> enumClass;
@Override
public void initialize(EnumColl enumVal) {
this.enumClass = enumVal.enumClass();
if (!CodeEnum.class.isAssignableFrom(this.enumClass)) {
throw new RuntimeException(this.enumClass + "不是继承自BaseEnum");
}
}
@Override
public boolean isValid(Collection coll, ConstraintValidatorContext context) {
if (null == coll || coll.isEmpty()) {
return true;
}
for (Integer e : coll) {
CodeEnum enumVal = EnumKit.enumOf(e, this.enumClass);
if (null == enumVal) {
return false;
}
}
return true;
}
}