枚举enum类

打开Settings - Editor - File And Code Templates 右侧选择Enum,修改为以下内容:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import lombok.Getter;
import java.util.Objects;

#parse("File Header.java")
@Getter
public enum ${NAME} {

    ;

    private String code;
    private String name;

    ${NAME}(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public static ${NAME} getByCode(String code) {
        for (${NAME} e : ${NAME}.values()) {
            if (Objects.equals(code, e.getCode())) {
                return e;
            }
        }
        return null;
    }

    public static String getNameByCode(String code) {
        ${NAME} e = getByCode(code);
        return e == null ? "" : e.getName();
    }

}
文档更新时间: 2019-12-31 13:27   作者:nick