java - 优化java枚举

标签 java enums

我的代码包含多个枚举,如下所示。基本上这有助于通过整数而不是枚举值来使用枚举。是否可以应用某种优化,如继承之类的,以便所有人都可以具有如下所示的行为。

public enum DeliveryMethods {

    STANDARD_DOMESTIC(1), STANDARD_INTERNATIONAL(2), EXPRESS_DOMESTIC(3), EXPRESS_INTERNATIONAL(4);

    private final int code;

    private DeliveryMethods(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    private static final HashMap<Integer, DeliveryMethods> valueMap = new HashMap<>(2);

    static {
        for (DeliveryMethods type : DeliveryMethods.values()) {
            valueMap.put(type.code, type);
        }
    }

    public static DeliveryMethods getValue(int code) {
        return valueMap.get(code);
    }
}

最佳答案

这是一个示例,展示了如何委托(delegate)给另一个类:

public interface Keyed<K> {
    /**
     * returns the key of the enum
     */
    K getKey();
}

public class KeyEnumMapping<K, E extends Enum<?> & Keyed<K>> {
    private Map<K, E> map = new HashMap<>();

    public KeyEnumMapping(Class<E> clazz) {
        E[] enumConstants = clazz.getEnumConstants();
        for (E e : enumConstants) {
            map.put(e.getKey(), e);
        }
    }

    public E get(K key) {
        return map.get(key);
    }
}

public enum Example implements Keyed<Integer> {
    A(1),
    B(3),
    C(7);

    private static final KeyEnumMapping<Integer, Example> MAPPING = new KeyEnumMapping<>(Example.class);
    private Integer value;

    Example(Integer value) {
        this.value = value;
    }

    @Override
    public Integer getKey() {
        return value;
    }

    public static Example getByValue(Integer value) {
        return MAPPING.get(value);
    }

    public static void main(String[] args) {
        System.out.println(Example.getByValue(3));
    }
}

您也可以避免实现 Keyed 接口(interface),而只需传递一个 Function<E, K>到 KeyEnumMapping 构造函数,它将枚举转换为其键:

public class KeyEnumMapping<K, E extends Enum<?>> {
    private Map<K, E> map = new HashMap<>();

    public KeyEnumMapping(Class<E> clazz, Function<E, K> keyExtractor) {
        E[] enumConstants = clazz.getEnumConstants();
        for (E e : enumConstants) {
            map.put(keyExtractor.apply(e), e);
        }
    }

    public E get(K key) {
        return map.get(key);
    }
}

public enum Example {
    A(1),
    B(3),
    C(7);

    private static final KeyEnumMapping<Integer, Example> MAPPING =
        new KeyEnumMapping<>(Example.class, Example::getValue);
    private Integer value;

    Example(Integer value) {
        this.value = value;
    }

    public Integer getValue() {
        return value;
    }

    public static Example getByValue(Integer value) {
        return MAPPING.get(value);
    }

    public static void main(String[] args) {
        System.out.println(Example.getByValue(3));
    }
}

关于java - 优化java枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31217437/

相关文章:

java - 为什么 ENUM 不能在 H2 的子查询中工作?

swift - 基于 Enum Swift 自动为对象赋值

用于将长 IPv6 地址转换为其压缩形式的 Java 库

java - 在 Hibernate 4 中创建 session 工厂

java - 映射到 JSON - 忽略特定键

ios - 将对象添加到枚举给我错误

java - 如何使用java提取字符串并存储到数据库

java - World Wind Java 无法检索 WMS 资源

python - 从现有 Enum 派生 Django 3.0 Choices 类?

enums - 使用 Rust 的枚举作为双向查找表