简体   繁体   English

如果条件为真,则其他正在执行

[英]If condition is true even then else is executing

I have an enum defined as below, which contains a static function fromString(String s) which is like valueOf(String s) but case insensitive. 我有一个如下定义的enum ,它包含一个静态函数fromString(String s) ,类似于valueOf(String s)但不区分大小写。

enum Platform {
    TWITTER("TWITTER"), INSTAGRAM("INSTAGRAM"), UNKNOWN;

    private String value;

    Platform(String value) {
        this.value = value;
    }

    private static final Map<String, Platform> stringToEnumMap = new HashMap<>();

    static {
        for (Platform platform : values()) {
            stringToEnumMap.put(platform.toString().toLowerCase(), platform);
        }
    }

    public static Platform fromString(String symbol) {
        Platform platform = stringToEnumMap.get(symbol.toLowerCase());
        if (platform != null) {
            return platform;
        } else {
            return UNKNOWN;
        }
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return getValue();
    }

}

but when I execute the following code 但是当我执行以下代码时

Platform platform = Platform.fromString("twitter");

I get Platform.UNKNOWN returned on few devices on production. 我得到Platform.UNKNOWN在生产中的少数设备上退货。

Any Idea? 任何想法?

Update 更新资料

symbol.toString() gives twitter symbol.toString()twitter

stringToEnumMap.toString() gives this stringToEnumMap.toString()给出了

Platform StringToEnumMap: { twıtter=TWITTER, ınstagram=INSTAGRAM, unknown=UNKNOWN} 平台StringToEnumMap:{twıtter= TWITTER,ınstagram= INSTAGRAM,unknown = UNKNOWN}

If you look closely, the letter i is different in the keys of HashMap and thats why string comparison fails. 如果仔细看,字母i在HashMap的键中是不同的,这就是字符串比较失败的原因。

the hexvalue of letter ı in stringToEnumMap is 0131, whereas the it should be 0069 信hexvalue ıstringToEnumMap是0131,而应该是0069

Why is this happening on only few devices? 为什么只有少数设备会发生这种情况? How to avoid it? 如何避免呢?

It seems to be a dirty ide issue, try to clean up your project, build and run again. 这似乎是一个肮脏的想法,请尝试清理您的项目,然后重新构建并运行。

UPDATE Try replacing your if statement by getOrDefault method: 更新尝试用getOrDefault方法替换if语句:

public static Letter fromString(String symbol) {
    return stringToEnumMap.getOrDefault(symbol.toLowerCase(), UNKNOWN);
}

In my Opinion, there is no need for a map.... nor a new method for doing something that valueOf() is able to do... 在我看来,不需要映射....也不需要新方法来执行valueOf()能够执行的操作...

look this implemetation.. 看这个实现。

enum ECase {
    A, B, UNK;

    public static ECase resolveEnumFromString(final String string) {
        ECase r = null;
        try {
            r = ECase.valueOf(string.toUpperCase());
        } catch (final IllegalArgumentException e) {
            r = ECase.UNK;
        }
        return r;
    }
}

you can verify the results doing: 您可以执行以下操作来验证结果:

    ECase d = null;
    d = ECase.resolveEnumFromString("a");
    System.out.println(d);
    d = ECase.resolveEnumFromString("A");
    System.out.println(d);
    d = ECase.resolveEnumFromString("0");
    System.out.println(d);

I am able to find what the issue was. 我能够找到问题所在。 After some careful logging I found the issue was this http://mattryall.net/blog/2009/02/the-infamous-turkish-locale-bug . 经过仔细的日志记录后,我发现问题出在这个http://mattryall.net/blog/2009/02/the-infamous-turkish-locale-bug

Apparently toLowerCase() and toUpperCase() functions are locale dependent due to which you can't use them safely for case insensitive string comparison. 显然toLowerCase()toUpperCase()函数是与语言环境相关的,因此,您不能安全地将它们用于不区分大小写的字符串比较。 So all you need to do is pass the english locale in the parameter of these functions like this - 因此,您需要做的就是在这些函数的参数中传递英语语言环境,例如:

toLowerCase(Locale.ENGLISH) . toLowerCase(Locale.ENGLISH)

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

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