I need to use an Enum with a combobox (values shown below).
YES (shown as YES on UI, stored in DB as Y) NO (shown as NO on UI, stored in DB as N) DEFAULT (shown as "" on UI, stored in DB as null)
The Enum has methods to perform the following -
- toString() - to provide the custom String for UI. (showing the combo options)
- OptionToDB (static) - Convert a selected option to db value (on save / update)
DBToOption (static)- Convert a DB value to selcted option (while loading the screen)
static enum EnumOption{ YES,NO,DEFAULT; .... public static EnumOption DBToOption(String val){ if("Y".equals(val)){ return YES; } else if("N".equals(val)){ return NO; }else { return DEFAULT; } } .... }
It works pretty well, but the issue with above methods is that it uses if/else comparison to deduce which option / db value to be returned.
I thought of storing the dbValue as a field in enum but I was not able to reduce the if/else from DBToOption.
Can this if/else be avoided in any way using a better design??