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

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

There is a newer version: 0.1.3.4
Show newest version
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> 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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy