Last change
on this file since 11710 was 10420, checked in by Don-vip, 8 years ago |
sonar - squid:S1166 - Exception handlers should preserve the original exceptions
|
-
Property svn:eol-style
set to
native
|
File size:
954 bytes
|
Line | |
---|
1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.Main;
|
---|
5 |
|
---|
6 | public abstract class ParametrizedEnumProperty<T extends Enum<T>> {
|
---|
7 |
|
---|
8 | protected final T defaultValue;
|
---|
9 | protected final Class<T> enumClass;
|
---|
10 |
|
---|
11 | public ParametrizedEnumProperty(Class<T> enumClass, T defaultValue) {
|
---|
12 | this.defaultValue = defaultValue;
|
---|
13 | this.enumClass = enumClass;
|
---|
14 | }
|
---|
15 |
|
---|
16 | protected abstract String getKey(String... params);
|
---|
17 |
|
---|
18 | public T get(String... params) {
|
---|
19 | return parse(Main.pref.get(getKey(params), defaultValue.name()));
|
---|
20 | }
|
---|
21 |
|
---|
22 | public boolean put(T value, String... params) {
|
---|
23 | return Main.pref.put(getKey(params), value.name());
|
---|
24 | }
|
---|
25 |
|
---|
26 | protected T parse(String s) {
|
---|
27 | try {
|
---|
28 | return Enum.valueOf(enumClass, s);
|
---|
29 | } catch (IllegalArgumentException e) {
|
---|
30 | Main.trace(e);
|
---|
31 | return defaultValue;
|
---|
32 | }
|
---|
33 | }
|
---|
34 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.