java enum中,通过域值返回对应的enum对象

想用enum表示订单的状态:


/**
 * 订单状态 enum
 */
public enum OrderStatus {
    NONE_PAY("c010101","待付款"),
    WAIT_SEND("c010102","待发货"),
    CANCEL("c010110","已取消");

    private final String code;
    private final String explain;

    OrderStatus(String code, String explain) {
        this.code = code;
        this.explain = explain;
    }


    public static OrderStatus conversion(String code){

    }

    public String getCode() {
        return code;
    }

    public String getExplain() {
        return explain;
    }
}




现在有一个问题,前台传过来的参数对应的是enum中的code值(如待付款的订单,前台调用接口时,会传入"c010101"),我不知道怎么将这个字符串转换成对应的OrderStatus枚举对象。
想过使用switch判断:

    /**
     * 通过code返回对应的enum实例
     * @param code
     * @return
     * @throws Exception
     */
    public static OrderStatus fromCode(String code) throws Exception {
        switch (code){
            case "c010101" :return NONE_PAY;
            case "c010102" :return WAIT_SEND;
            case "c010110" :return CANCEL;
        }
        throw new Exception();
    }


但总感觉需要维护多一个方法比较麻烦,请问有没有更好的实现方法

//根据key获取value的值
public static String getValueByKey(Sring key){
for (OrderStatus s : OrderStatus .values()) {
if(s.getKey().equals(key)){
return s.getValue();
}
}
return "";
}

    //根据匹配value的值获取key
    public static String getKeyByValue(String channelName){
        for (OrderStatus s : OrderStatus.values()) {
            if(channelName.equals(s.getValue())){
                return s.getKey();
            }
        }
        return "";
    }

这我之前写的定时任务状态枚举,可供参考

public enum JobState {
    NONE("无", "NONE"),
    NORMAL("正常", "NORMAL"),
    PAUSED("暂停", "PAUSED"),
    COMPLETE("完成", "COMPLETE"),
    ERROR("错误", "ERROR"),
    BLOCKED("堵塞", "BLOCKED");

    private String stateName;
    private String stateValue;

    private JobState(String stateName, String stateValue) {
        this.stateName = stateName;
        this.stateValue = stateValue;
    }

    public static String getJobState(String stateValue) {
        for (JobState temp : JobState.values()) {
            if (temp.getStateValue().equals(stateValue)) {
                return temp.getStateName();
            }
        }
        return null;
    }

    public String getStateName() {
        return stateName;
    }

    public String getStateValue() {
        return stateValue;
    }

}

        for (OrderStatus orderStatus : OrderStatus.values()) {
            if(code.equals(orderStatus.getCode())){
                return orderStatus;
            }
        }
        return null;
public static OrderStatus conversion(String code){
        if(code==null){
                return null;
        }
        for (OrderStatus orderStatus : OrderStatus.values()) {
                    if(code.equals(orderStatus.getCode())){
                            return orderStatus;
                    }
            }
        return null;
}

在楼上基础上完善了非空判断

java enum有一个valueOf方法,可以根据给定的属性返回enum对象

https://blog.csdn.net/daweibalang717/article/details/41747503/

/**
 * 枚举类
 */
Class enumClass;

    /**
 * 按照枚举数值找到对应枚举
 *
 * @param index     下标
 * @param type      下标
 * @param name      名字
 * @param enumClass 枚举类
 */
EnumType(Integer index, String type, String name, Class enumClass) {
    this.index = index;
    this.type = type;
    this.name = name;
    this.enumClass = enumClass;
}

public static Class getEnumByIndex(Integer index) {
    for (EnumType s : EnumType.values()) {
        if (s.getIndex().equals(index)) {
            return s.getEnumClass();
        }
    }
    return null;
}

重载下set方法就可以了;
如:

/**
* 文件类型(枚举)
*/
private FileTypeEnum type;

public FileTypeEnum getType() {return type;}
public void setType(FileTypeEnum type) {this.type = type;}
public void setType(String type) {this.type = FileTypeEnum.valueOf(type);}

https://blog.csdn.net/HXNLYW/article/details/80266575