Yes, add a string to the enumeration constructor (specify the constructor in the enumeration that accepts the string) and create the enumerations with the text value a, b, c. Etc. Then we implement the static factory method in the enumeration, which takes a string and returns an instance of enum. I call this method textValueOf. The existing valueOf method in the enumeration cannot be used for this. Something like that:
public enum EnumWithValueOf { VALUE_1("A"), VALUE_2("B"), VALUE_3("C"); private String textValue; EnumWithValueOf(String textValue) { this.textValue = textValue; } public static EnumWithValueOf textValueOf(String textValue){ for(EnumWithValueOf value : values()) { if(value.textValue.equals(textValue)) { return value; } } throw new IllegalArgumentException("No EnumWithValueOf for value: " + textValue); } }
This case is case insensitive, and the text value may differ from the enumeration name β ideally if the database codes are esoteric or non-descriptive, but you need better names in Java code. Then execute the client code:
EnumWithValueOf enumRepresentation = EnumWithValueOf.textValueOf("a");
source share