The values in my enum
are words that need to have spaces in them, but enums can't have spaces in their values so it's all bunched up. I want to override toString()
to add these spaces where I tell it to.
I also want the enum to provide the correct enum when I use valueOf()
on the same string that I added the spaces to.
For example:
public enum RandomEnum
{
StartHere,
StopHere
}
Call toString()
on RandomEnum
whose value is StartHere
returns string "Start Here"
. Call valueof()
on that same string ("Start Here"
) returns enum value StartHere
.
How can I do this?
This question is related to
java
enums
overriding
tostring
value-of
I don't think your going to get valueOf("Start Here") to work. But as far as spaces...try the following...
static private enum RandomEnum {
R("Start There"),
G("Start Here");
String value;
RandomEnum(String s) {
value = s;
}
}
System.out.println(RandomEnum.G.value);
System.out.println(RandomEnum.valueOf("G").value);
Start Here
Start Here
The following is a nice generic alternative to valueOf()
public static RandomEnum getEnum(String value) {
for (RandomEnum re : RandomEnum.values()) {
if (re.description.compareTo(value) == 0) {
return re;
}
}
throw new IllegalArgumentException("Invalid RandomEnum value: " + value);
}
You still have an option to implement in your enum this:
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name){...}
Try this, but i don't sure that will work every where :)
public enum MyEnum {
A("Start There"),
B("Start Here");
MyEnum(String name) {
try {
Field fieldName = getClass().getSuperclass().getDeclaredField("name");
fieldName.setAccessible(true);
fieldName.set(this, name);
fieldName.setAccessible(false);
} catch (Exception e) {}
}
}
You can use a static Map in your enum that maps Strings to enum constants. Use it in a 'getEnum' static method. This skips the need to iterate through the enums each time you want to get one from its String value.
public enum RandomEnum {
StartHere("Start Here"),
StopHere("Stop Here");
private final String strVal;
private RandomEnum(String strVal) {
this.strVal = strVal;
}
public static RandomEnum getEnum(String strVal) {
if(!strValMap.containsKey(strVal)) {
throw new IllegalArgumentException("Unknown String Value: " + strVal);
}
return strValMap.get(strVal);
}
private static final Map<String, RandomEnum> strValMap;
static {
final Map<String, RandomEnum> tmpMap = Maps.newHashMap();
for(final RandomEnum en : RandomEnum.values()) {
tmpMap.put(en.strVal, en);
}
strValMap = ImmutableMap.copyOf(tmpMap);
}
@Override
public String toString() {
return strVal;
}
}
Just make sure the static initialization of the map occurs below the declaration of the enum constants.
BTW - that 'ImmutableMap' type is from the Google guava API, and I definitely recommend it in cases like this.
EDIT - Per the comments:
How about a Java 8 implementation? (null can be replaced by your default Enum)
public static RandomEnum getEnum(String value) {
return Arrays.stream(RandomEnum.values()).filter(m -> m.value.equals(value)).findAny().orElse(null);
}
Or you could use:
...findAny().orElseThrow(NotFoundException::new);
Source: Stackoverflow.com