Let's assume I have an enum type
enum ComponentState {
TURNED_OFF,
TURNED_ON,
SUSPENDED,
TO_REPAIR;
}
This enum describes a state of some component. Now let's assume I want to export my component to as XML file. Then, of course, I'm going to import my component. So:
//Export:
componentElement.setAttribute("state", getState.name()); // results in <element state="TURNED_ON">
//Import
try {
state = ComponentState.valueOf(element.getAttributeValue("state"));
} catch (IllegalArgumentException e) {
//...
}
Everything is fine, but problems starts when I change the name of some enum constants, because it sounds better:
enum ComponentState {
TURNED_OFF,
TURNED_ON,
SLEEPING, // <- the same sense, but
CRASHED; // <- different name (PM's "I want to have")
}
Note, that doing this I'm loosing a backward compatibility.
Question: How do you cope normally with such situations?
1
public String getXMLValue(){
// ... implementation independent from name()
}
and
static ComponentState parseXMLString(String s) {
// ... checking if s matches any name()
}
OR
2
My approach is OK - it should not be expected, that the name on enum will change just because it sounds better.