I have this enum:
public enum View { "aaa", "bbb", "ccc" }
I use valueOf to get constant enum:
var v = view.valueOf(someString);
In case that someString value is different from “aaa”, “bbb”, “ccc” I need the valueof will return default value “xxx”.
My question is how to return the default value for example “xxx” if input not equal to one of the strings mentioned above?
Advertisement
Answer
You could create your version of valueOf()
:
public enum View { aaa,bbb,ccc; public static View valueOfOrElse(String name) { for (View value : values()) { if (value.name().equals(name)) { return value; } } return aaa; } }