极其方便的枚举工具类

第一套

接口

public interface Typable<T> {
    T getCode();

    String getDesc();

    default boolean isEqual(T code) {
        return Objects.nonNull(code) && code.equals(this.getCode());
    }

    boolean isValid(T code);
}

实现类

Static 可以好好利用下

Integer

public enum CommonStatus implements Typable<Integer> {
    DEF(0, "默认"),
    ON(1, "启用"),
    OFF(4, "停用");

    private int code;
    private String desc;

    CommonStatus(Integer code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    public <T extends Typable> T getType(Integer code) {
        return (T) types.get(code);
    }
    @Override
    public boolean isValid(Integer code) {
        return types.containsKey(code);
    }


    private static Map<Integer, CommonStatus> types = new HashMap<>();

    static {
        for (CommonStatus type : CommonStatus.values()) {
            types.put(type.getCode(), type);
        }
    }
}

String

public enum  AlgorithmNameType implements Typable<String> {

    ALGO_SREP_01("ALGO_SREP_01","补货code码"),
    ALGO_SVF_01("ALGO_SVF_01","预测code码");

    private String code;
    private String desc;

    AlgorithmNameType(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    public <T extends Typable> T getType(String code) {
        return (T) types.get(code);
    }
    @Override
    public boolean isValid(String code) {
        return types.containsKey(code);
    }


    private static Map<String, AlgorithmNameType> types = new HashMap<>();

    static {
        for (AlgorithmNameType type : AlgorithmNameType.types.values()) {
            types.put(type.getCode(), type);
        }
    }
}

第二套

接口

public interface EnumType {
    static <E extends Enum<E> & EnumType> E getByName(String name, Class<E> clazz) {
        Objects.requireNonNull(name);
        Objects.requireNonNull(clazz);

        EnumSet<E> all = EnumSet.allOf(clazz);
        return all.stream().filter(e -> e.name().equals(name)).findFirst().orElse(null);
    }

    /**
     * @return
     */
    String getText();
}

实现类

Integer

public interface EnumTypeOfIntCode extends EnumType {
    static <E extends Enum<E> & EnumTypeOfIntCode> E getByCode(Integer code, Class<E> clazz) {
        Objects.requireNonNull(code);
        Objects.requireNonNull(clazz);

        EnumSet<E> all = EnumSet.allOf(clazz);
        return all.stream().filter(e -> Objects.equals(e.getCode(), code)).findFirst().orElse(null);
    }

    int getCode();
}

String

public interface EnumTypeOfStringCode extends EnumType {

    static <E extends Enum<E> & EnumTypeOfStringCode> E getByCode(String code, Class<E> clazz) {
        Objects.requireNonNull(code);
        Objects.requireNonNull(clazz);

        EnumSet<E> all = EnumSet.allOf(clazz);
        return all.stream().filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
    }

    String getCode();
}
上一篇:Delphi项目构成之单元文件PAS


下一篇:Oracle 常用的几个表 视图 的desc