1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
5 | import org.openstreetmap.josm.tools.Logging;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Abstract base class for properties with {@link Enum} value, where the preference
|
---|
9 | * key is generated from a list of parameters.
|
---|
10 | * @param <T> the {@code Enum} class
|
---|
11 | */
|
---|
12 | public abstract class ParametrizedEnumProperty<T extends Enum<T>> {
|
---|
13 |
|
---|
14 | protected final T defaultValue;
|
---|
15 | protected final Class<T> enumClass;
|
---|
16 |
|
---|
17 | protected ParametrizedEnumProperty(Class<T> enumClass, T defaultValue) {
|
---|
18 | this.defaultValue = defaultValue;
|
---|
19 | this.enumClass = enumClass;
|
---|
20 | }
|
---|
21 |
|
---|
22 | protected abstract String getKey(String... params);
|
---|
23 |
|
---|
24 | public T get(String... params) {
|
---|
25 | return parse(Config.getPref().get(getKey(params), defaultValue.name()));
|
---|
26 | }
|
---|
27 |
|
---|
28 | public boolean put(T value, String... params) {
|
---|
29 | return Config.getPref().put(getKey(params), value.name());
|
---|
30 | }
|
---|
31 |
|
---|
32 | protected T parse(String s) {
|
---|
33 | try {
|
---|
34 | return Enum.valueOf(enumClass, s);
|
---|
35 | } catch (IllegalArgumentException e) {
|
---|
36 | Logging.trace(e);
|
---|
37 | return defaultValue;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|