1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.spi.preferences;
|
---|
3 |
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Map;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.preferences.ListListSetting;
|
---|
8 | import org.openstreetmap.josm.data.preferences.ListSetting;
|
---|
9 | import org.openstreetmap.josm.data.preferences.MapListSetting;
|
---|
10 | import org.openstreetmap.josm.data.preferences.Setting;
|
---|
11 | import org.openstreetmap.josm.data.preferences.StringSetting;
|
---|
12 | import org.openstreetmap.josm.tools.Logging;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Abstract implementation of the {@link IPreferences} interface.
|
---|
16 | * @since 12847
|
---|
17 | */
|
---|
18 | public abstract class AbstractPreferences implements IPreferences {
|
---|
19 |
|
---|
20 | @Override
|
---|
21 | public synchronized String get(final String key, final String def) {
|
---|
22 | return getSetting(key, new StringSetting(def), StringSetting.class).getValue();
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public boolean put(final String key, String value) {
|
---|
27 | return putSetting(key, value == null || value.isEmpty() ? null : new StringSetting(value));
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public boolean getBoolean(final String key, final boolean def) {
|
---|
32 | return Boolean.parseBoolean(get(key, Boolean.toString(def)));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public boolean putBoolean(final String key, final boolean value) {
|
---|
37 | return put(key, Boolean.toString(value));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public synchronized int getInt(String key, int def) {
|
---|
42 | String v = get(key, Integer.toString(def));
|
---|
43 | if (v.isEmpty())
|
---|
44 | return def;
|
---|
45 |
|
---|
46 | try {
|
---|
47 | return Integer.parseInt(v);
|
---|
48 | } catch (NumberFormatException e) {
|
---|
49 | // fall out
|
---|
50 | Logging.trace(e);
|
---|
51 | }
|
---|
52 | return def;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public boolean putInt(String key, int value) {
|
---|
57 | return put(key, Integer.toString(value));
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public synchronized double getDouble(String key, double def) {
|
---|
62 | String v = get(key, Double.toString(def));
|
---|
63 | if (null == v)
|
---|
64 | return def;
|
---|
65 |
|
---|
66 | try {
|
---|
67 | return Double.parseDouble(v);
|
---|
68 | } catch (NumberFormatException e) {
|
---|
69 | // fall out
|
---|
70 | Logging.trace(e);
|
---|
71 | }
|
---|
72 | return def;
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public boolean putDouble(final String key, final double value) {
|
---|
77 | return put(key, Double.toString(value));
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public List<String> getList(String key, List<String> def) {
|
---|
82 | return getSetting(key, new ListSetting(def), ListSetting.class).getValue();
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public boolean putList(String key, List<String> value) {
|
---|
87 | return putSetting(key, value == null ? null : new ListSetting(value));
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | public List<List<String>> getListOfLists(String key, List<List<String>> def) {
|
---|
92 | return getSetting(key, new ListListSetting(def), ListListSetting.class).getValue();
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public boolean putListOfLists(String key, List<List<String>> value) {
|
---|
97 | return putSetting(key, value == null ? null : new ListListSetting(value));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public List<Map<String, String>> getListOfMaps(String key, List<Map<String, String>> def) {
|
---|
102 | return getSetting(key, new MapListSetting(def), MapListSetting.class).getValue();
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public boolean putListOfMaps(String key, List<Map<String, String>> value) {
|
---|
107 | return putSetting(key, value == null ? null : new MapListSetting(value));
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Set a value for a certain setting. The changed setting is saved to the preference file immediately.
|
---|
112 | * Due to caching mechanisms on modern operating systems and hardware, this shouldn't be a performance problem.
|
---|
113 | * @param key the unique identifier for the setting
|
---|
114 | * @param setting the value of the setting. In case it is null, the key-value entry will be removed.
|
---|
115 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
116 | */
|
---|
117 | public abstract boolean putSetting(String key, Setting<?> setting);
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Get settings value for a certain key and provide default a value.
|
---|
121 | * @param <T> the setting type
|
---|
122 | * @param key the identifier for the setting
|
---|
123 | * @param def the default value. For each call of getSetting() with a given key, the default value must be the same.
|
---|
124 | * <code>def</code> must not be null, but the value of <code>def</code> can be null.
|
---|
125 | * @param klass the setting type (same as T)
|
---|
126 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
127 | */
|
---|
128 | public abstract <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass);
|
---|
129 | }
|
---|