springboot枚举工具类

记录一个枚举工具类,这里可以直接传汉字(text)获取value。也可以传value获取value。

  • 贴代码
@EnumTag(type = Constant.FILE_ENUM_TYPE,value = "fileLocationType")
public enum FileType {
    
    

    LOCAL("local","本地"),
    ALIOSS("alioss","阿里云oss")
    ;

  // 定义valueMap 将value全部初始化到map里,方便get
    private static final Map<String, FileType> valueMap = new HashMap<>();

// 初始化map值
    static {
    
    
        for (FileType value : values()) {
    
    
            valueMap.put(value.getValue(), value);
        }
    }

    @EnumValue
    private final String value;
    @JsonValue
    private final String text;

    FileType(String value, String text) {
    
    
        this.value = value;
        this.text = text;
    }

    public String getValue() {
    
    
        return value;
    }

    public String getText() {
    
    
        return text;
    }


// 可以直接传一个value,得到存在的value值
    public static FileType fromValue(String value) {
    
    
        return valueMap.get(value);
    }
    
    public static FileType getFileTypeByValue(String code){
    
    
        for (FileType e:FileType.values()) {
    
    
            if (e.getValue().equals(code)) {
    
    
                return e;
            }
        }
        return null;
    }
    
    @Override
    public String toString() {
    
    
        return text;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31424825/article/details/126935472