I am creating a repository for user preferences, and there is a fixed number of preferences that users can set for values. Names of settings (settings) are saved as Enum:
public enum UserSettingName { FOO, BAR, ETC }
What I would like to do is save the value type with a name so that the service saves the user value with the correct Java type. For example, FOO may be Long , and BAR may be String . So far, we have saved all the values ββas String s, and then manually entered the values ββinto the appropriate Java type. This led to the fact that try / catch blocks are used everywhere, when it makes sense to have only one try / catch in the service. I understand that Enums cannot have common types, so I played with:
public enum UserSettingName { FOO(Long.class), BAR(String.class), ETC(Baz.class) private Class type; private UserSettingName(Class type) { this.type = type; } public Class getType() { return this.type; } }
I have a common UserSetting object that has the public T getSettingValue() and public void setSettingValue(T value) methods that should return and set the value with the correct type. My problem is trying to specify this generic type T when creating or retrieving a parameter, because I cannot do something like:
new UserSetting<UserSettingName.FOO.getType()>(UserSettingName.FOO, 123L)
Sorry, if this is not entirely clear, I can try to find out if he did not understand.
Thanks!
UPDATE
Both parameter names and values ββare included in the Spring MVC REST call:
public ResponseEntity<String> save(@PathVariable Long userId, @PathVariable UserSettingName settingName, @RequestBody String settingValue)
So, I used Enum because Spring automatically inputs the input.