简体   繁体   中英

JDK1.7 - retrieving enum constant with dots in their values

I'm using a thirdparty library which has an enum class which is as shown below:

public enum Version { 
VERSION_2_0("v2.0"), 
VERSION_2_1("v2.1"), 
VERSION_2_2("v2.2")

private final String urlElement;

  Version(String urlElement) {
    this.urlElement = urlElement;
  }

  public String getUrlElement() {
    return this.urlElement;
  }

  public boolean isUrlElementRequired() {
    return null != this.urlElement;
  }


}

I'm reading version from props file which i want to pass to this enum to get respective enum constant like this:

String str="v2.2"; //this i get from a props file    
System.out.println("version enum: "+Version.valueOf(str));

I'm getting below exception:

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.pkg.Version.v2.2

It seems "v2.2" is not getting treated as single string. I tried couple of ways by escaping dot in the string, but none of them are working. I'm using JDK1.7. So anyone encountered this, please advise.

You will need to add a method to look up the enum constant based on your value property. It could look something like this:

public enum Version {
    VERSION_2_0("v2.0"),
    VERSION_2_1("v2.1"),
    VERSION_2_2("v2.2");

    String value;

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

    static Version byValue(String value) {
        for (Version version : Version.values()) {
            if (version.value.equals(value)) {
                return version;
            }
        }

        return null;
    }
}

And then you can do the lookup as follows:

Version version = Version.byValue("v2.2");

If performance is important, you might want to add a map to avoid the iteration over the enum values and speed up the lookup.

Update

If the enum itself cannot be modified, you can implement the same mechanism in another class that you do have access to. In line with your updated code, it would look like this:

static Version byUrlElement(String urlElement) {
    for (Version version : Version.values()) {
        if (version.getUrlElement().equals(urlElement)) {
            return version;
        }
    }

    return null;
}

Or, if you want to further optimize with a map:

private static final Map<String, Version> VERSION_INDEX = new HashMap<>();

static {
    for (Version version : Version.values()) {
        VERSION_INDEX.put(version.getUrlElement(), version);
    }
}

And then you can perform your lookups using the map:

Version version = VERSION_INDEX.get("v2.2");

Since you cannot update the Enum so write a program which return the Enum Name by value.

public static String getEnumNamebyValue(String str){
        for(Version v : Version.values()){
            if(v.getVersion().equals(str))
                return v.name();
        }

return null; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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