比较ENUM中的字符串

问题描述 投票:0回答:3

我想实现将启用或禁用的功能存储到数据库行中。当从它们收到一些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?

java enums
3个回答
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);

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时创建的(所以当它第一次被引用时)。因此迭代只发生一次。 Maps也有相当快的查找时间,所以你可以说以这种方式获取枚举是有效的O(1)(当忽略O(n)的初始计算时间时)


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!");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.