比较ENUM中的字符串

时间:2019-02-22 10:55:00

标签: java enums

我想实现将启用或禁用的功能存储到数据库行中。从网络上收到一些String值后,我想将其与ENUM进行比较。

ENUM:

public enum TerminalConfigurationFeatureBitString {
    Authorize("authorize", 0), // index 0 in bit string
    Authorize3d("authorize3d", 1), // index 1 in bit String
    Sale("sale", 2), // index 2 in bit String
    Sale3d("sale3d", 3), // index 3 in bit String
}

Map<TerminalConfigurationFeatureBitString, Boolean> featureMaps =
    config.initFromDatabaseValue(optsFromDatabase);

featureMaps.get(transaction.transactionType);

最好的方法是使用featureMaps.get(TerminalConfigurationFeatureBitString.Sale);

但是我不知道输入的字符串是什么。

现在我得到警告Unlikely argument type String for get(Object) on a Map<TerminalConfigurationFeatureBitString,Boolean>

还有其他方法可以在不知道键的情况下对ENUM进行查询吗?

3 个答案:

答案 0 :(得分:2)

在这种情况下,我经常发现自己添加了一个静态方法getByX,该方法基于枚举的属性进行查找:

public enum BitString {
    //...

    public static Optional<BitString> getByTransactionType(String transactionType)
    {
        return Arrays.stream(values())
            .filter(x -> x.transactionType.equals(transactionType))
            .findFirst();
    }
}

用法:

enum TransactionStatus
{
    ENABLED, NOT_ENABLED, NOT_SUPPORTED
}

TransactionStatus status = BitString.getBygetByTransactionType(transaction.transactionType)
    .map(bitString -> featureMaps.get(bitString))
    .map(enabled -> enabled ? TransactionStatus.ENABLED : TransactionStatus.NOT_ENABLED)
    .orElse(TransactionStatus.NOT_SUPPORTED);

答案 1 :(得分:2)

类似于@Michael's answer,您只需在static内部生成一个enum查找映射,即可将枚举事务类型映射到实际枚举:

private static final Map<String, TerminalConfigurationFeatureBitString> TRANSACTION_TYPE_TO_ENUM = 
   Arrays.stream(values()).collect(Collectors.toMap(
       TerminalConfigurationFeatureBitString::getTransactionType, 
       Function.identity()
   );

然后在枚举内也有一个查找方法:

public static TerminalConfigurationFeatureBitString getByTransactionType(String transactionType) {
    TerminalConfigurationFeatureBitString bitString = TRANSACTION_TYPE_TO_ENUM.get(transactionType);
    if(bitString == null) throw new NoSuchElementException(transactionType);
    return bitString;
}

这比上面提到的答案更有性能,因为Map是在第一次加载enum时创建的(因此是第一次引用时)。因此,迭代仅发生一次。此外,Map的查找时间非常快,因此您可以说以这种方式获取枚举可以使用O(1)(而忽略O(n)的初始计算时间)

答案 2 :(得分:2)

您可以使用额外的静态方法扩展enum,该方法将尝试转换String项目上给定的enum

enum TerminalConfigurationFeatureBitString {
    Authorize("authorize", 0), // index 0 in bit string
    Authorize3d("authorize3d", 1), // index 1 in bit String
    Sale("sale", 2), // index 2 in bit String
    Sale3d("sale3d", 3); // index 3 in bit String

    private final String value;
    private final int index;

    TerminalConfigurationFeatureBitString(String value, int index) {
        this.value = value;
        this.index = index;
    }

    public String getValue() {
        return value;
    }

    public int getIndex() {
        return index;
    }

    public static Optional<TerminalConfigurationFeatureBitString> fromValue(String value) {
        for (TerminalConfigurationFeatureBitString item : values()) {
            if (item.value.equals(value)) {
                return Optional.of(item);
            }
        }

        return Optional.empty();
    }
}

如果未找到选项,请返回Optional.empty()。如果特征不存在,则表示String表示不代表任何特征。用法:

public void test() {
    EnumMap<TerminalConfigurationFeatureBitString, Boolean> featureMaps = new EnumMap<>(
        TerminalConfigurationFeatureBitString.class);

    Optional<TerminalConfigurationFeatureBitString> feature = TerminalConfigurationFeatureBitString.fromValue("authorize");
    if (!feature.isPresent()) {
        System.out.println("Feature is not foudn!");
    } else {
        Boolean authorize = featureMaps.get(feature.get());
        if (authorize != null && authorize) {
            System.out.println("Feature is enabled!");
        } else {
            System.out.println("Feature is disabled!");
        }
    }
}