简体   繁体   English

如何从Java中分配给它们的值中检索枚举常量?

[英]How to retrieve Enum Constants from values assigned to them in Java?

I am trying to get Enum constant through value assigned to it but don't know if there is any built in API to do this. 我试图通过分配给它的值来获取Enum常量,但是不知道是否有内置的API可以做到这一点。 My enum looks like this : 我的枚举看起来像这样:

public enum VideoBandwidth {

    VIDEO_BW_AUTO(-1),
    VIDEO_BW_OFF(0),
    VIDEO_BW_2_MBPS(2000000),
    VIDEO_BW_500_KBPS(500000),
    VIDEO_BW_250_KBPS(250000);

    private final int bandwidth;

    private VideoBandwidth (final int value) {
        bandwidth = value;
    }

    public int getValue() {
        return bandwidth;
    }
}

How do I get enum constant VIDEO_BW_2_MBPS through value "2000000" assigned to it ? 如何通过为其分配值“ 2000000”来获取枚举常量VIDEO_BW_2_MBPS? I understand that if values are sequential like 0,1,2,3, I can use VideoBandwidth.values()[index] but how do I get the constant in case when values cannot be used as Index ? 我知道如果值是连续的(如0、1、2、3),则可以使用VideoBandwidth.values()[index],但是当值不能用作Index时,如何获取常数呢?

public static VideoBandwidth withValue(int value) {
    for (VideoBandwidth v : values()) {
        if (v.bandwidth == value) {
             return v;
        }
    }
    throw new IllegalArgumentException("no VideoBandwidth with value " + value);
}

Of course, you can also store the enum values in an internal Map, for example, if you want to avoid the iteration and the array creation. 当然,例如,如果要避免迭代和数组创建,也可以将枚举值存储在内部Map中。

Implement your own method that iterate over all the constants and returns the appropriate one or null /some exception. 实现自己的方法,该方法遍历所有常量并返回适当的一个或null / some异常。

public VideoBandwidth valueOf(int bandwidth) {
    for (VideoBandwidth videoBandwidth : values()) {
        if (videoBandwidth.bandwidth == bandwidth)
            return videoBandwidth;
    }
    throw new RuntimeException();
}

Iterate just once! 重复一次! define a static Map and fill it in a static block at load time. 定义静态地图,并在加载时将其填充到静态块中。

final static Map<Integer, VideoBandwidth> cache = new HashMap<>();
static {
    for(VideoBandwidth e: VideoBandwidth.values()) {
        cache.put(e.getValue(), e);
    }
}

public static VideoBandwidth fromValue(int value) {
    VideoBandwidth videoBandwidth = cache.get(value);
    if(videoBandwidth == null) {
        throw new RuntimeException("No such enum for value: " + value);
    }
    return videoBandwidth;
}

Use a map: 使用地图:

public enum VideoBandwidth {

    VIDEO_BW_AUTO(-1),
    VIDEO_BW_OFF(0),
    VIDEO_BW_2_MBPS(2000000),
    VIDEO_BW_500_KBPS(500000),
    VIDEO_BW_250_KBPS(250000);

    private final int bandwidth;
    private static final Map<Integer, VideoBandwidth> map = new HashMap<Integer, VideoBandwidth>();

    private VideoBandwidth (final int value) {
        bandwidth = value;
        map.put(value, this);
    }

    public int getValue() {
        return bandwidth;
    }

    public static VideoBandwidth valueOf(int bandWidth) {
        return map.get(bandWidth);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM