source: josm/trunk/src/org/openstreetmap/josm/data/preferences/AbstractPreferences.java@ 12840

Last change on this file since 12840 was 12840, checked in by bastiK, 7 years ago

see #15229 - extract interface for Preferences class

some changes to the method names and signatures

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