package com.chushang.common.core.web;

import lombok.experimental.UtilityClass;

import java.io.Serializable;

/**
 * by zhaowenyuan create 2022/1/10 14:28
 * 枚举获取帮助类 , 要求 enum 类必须 实现 CodeEnum, 包含有 Code , 并且 Code 唯一
 */
@UtilityClass
public class EnumUtils {
    public static  <T extends CodeEnum<?>> T getByCode(Serializable code, Class<T> tClass) {
        if (null == code){
            return null;
        }
        for (T each : tClass.getEnumConstants()) {
            if (code.equals(each.getCode())) {
                return each;
            }
        }
        return null;
    }

    public interface CodeEnum<S extends Serializable> {
        Integer getCode();
        S getMsg();
    }
}


