package com.zd.common.core.enums;

import com.zd.common.core.exception.ServiceException;

import java.util.Arrays;

/**
 * 值转换枚举根接口
 *
 * @Author: zhoupan
 * @Date: 2021/09/10/10:12
 * @Description:
 */
public interface BaseEnum<T> {


    T getDictKey();


    static <T> BaseEnum valueOfEnum(Class<? extends BaseEnum> enumClass, T value) throws ServiceException {
        if (value == null) {
            return null;
        }
        String s = String.valueOf(value);
        BaseEnum[] enums = enumClass.getEnumConstants();
        return Arrays.asList(enums).stream().filter(baseEnum -> baseEnum.getDictKey().toString().equals(s)).findFirst().orElse(null);

//        return Arrays.asList(enums).stream().filter(baseEnum -> baseEnum.getDictKey().toString().equals((String) value)).findFirst().orElseThrow(()->new IllegalArgumentException("未找到："+ value + "对应的" + enumClass.getName()));

    }
}
