Turn inside out:
public enum UsageCode {
ANTIQUE ('A'),
FLOOD_DAMAGED ('F'),
NEW ('N');
UNKNOWN ('0')
private static final Map<Character, UsageCode> charToEnum
= new HashMap<Character, UsageCode>();
static {
for (UsageCode code : values())
charToEnum.put(code.getCode(), code);
}
public static UsageCode fromLegacyCode(char code) {
return charToEnum.get(code);
}
private final char code;
UsageCode(char code) {
this.code = code;
}
public char getCode() {
return code;
}
}
To convert incoming character codes to enumeration values, I added an internal Map<Character, UsageCode>and static conversion method.
Example adapted from Effective Java 2nd Edition , paragraph 30.
source
share