All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.chobit.common.validation.EnumCollValidator Maven / Gradle / Ivy

The newest version!
package org.chobit.common.validation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.chobit.common.base.BaseEnum;
import org.chobit.common.utils.BaseEnumKit;

import java.util.Collection;

/**
 * 枚举值验证
 *
 * @author robin
 */
public class EnumCollValidator implements ConstraintValidator> {


    private Class> enumClass;


    @Override
    public void initialize(EnumColl enumVal) {
        this.enumClass = enumVal.enumClass();
        if (!BaseEnum.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) {
            BaseEnum enumVal = BaseEnumKit.enumOf(e, this.enumClass);
            if (null == enumVal) {
                return false;
            }
        }

        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy