source: josm/trunk/src/org/openstreetmap/josm/spi/preferences/IPreferences.java@ 12855

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

see #15229 - include support for the long type in IPreferences

File size: 8.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.spi.preferences;
3
4import java.util.Collections;
5import java.util.List;
6import java.util.Map;
7
8import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
9
10/**
11 * Interface for preference handling.
12 *
13 * Allows to save and retrieve user defined settings. The backend storage depends
14 * on the implementation.
15 * @since 12847
16 */
17public interface IPreferences {
18
19 /**
20 * Adds a new preferences listener.
21 * @param listener The listener to add
22 */
23 void addPreferenceChangeListener(PreferenceChangedListener listener);
24
25 /**
26 * Removes a preferences listener.
27 * @param listener The listener to remove
28 */
29 void removePreferenceChangeListener(PreferenceChangedListener listener);
30
31 /**
32 * Adds a listener that only listens to changes in one preference
33 * @param key The preference key to listen to
34 * @param listener The listener to add.
35 */
36 void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener);
37
38 /**
39 * Removes a listener that only listens to changes in one preference
40 * @param key The preference key to listen to
41 * @param listener The listener to add.
42 */
43 void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener);
44
45 /**
46 * Get settings value for a certain key and provide a default value.
47 * @param key the identifier for the setting
48 * @param def the default value. For each call of get() with a given key, the
49 * default value must be the same. {@code def} may be null.
50 * @return the corresponding value if the property has been set before, {@code def} otherwise
51 */
52 String get(String key, String def);
53
54 /**
55 * Get settings value for a certain key.
56 * @param key the identifier for the setting
57 * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
58 */
59 default String get(final String key) {
60 return get(key, "");
61 }
62
63 /**
64 * Set a value for a certain setting.
65 * @param key the unique identifier for the setting
66 * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
67 * @return {@code true}, if something has changed (i.e. value is different than before)
68 */
69 boolean put(String key, String value);
70
71 /**
72 * Gets a boolean preference
73 * @param key The preference key
74 * @param def The default value to use
75 * @return The boolean, <code>false</code> if it could not be parsed, the default value if it is unset
76 */
77 boolean getBoolean(String key, boolean def);
78
79 /**
80 * Gets a boolean preference
81 * @param key The preference key
82 * @return The boolean or <code>false</code> if it could not be parsed
83 */
84 default boolean getBoolean(String key) {
85 return getBoolean(key, false);
86 }
87
88 /**
89 * Set a boolean value for a certain setting.
90 * @param key the unique identifier for the setting
91 * @param value The new value
92 * @return {@code true}, if something has changed (i.e. value is different than before)
93 * @since 12840
94 */
95 boolean putBoolean(String key, boolean value);
96
97 /**
98 * Gets an integer preference
99 * @param key The preference key
100 * @param def The default value to use
101 * @return The integer
102 * @since 12840
103 */
104 int getInt(String key, int def);
105
106 /**
107 * Set an integer value for a certain setting.
108 * @param key the unique identifier for the setting
109 * @param value The new value
110 * @return {@code true}, if something has changed (i.e. value is different than before)
111 * @since 12840
112 */
113 boolean putInt(String key, int value);
114
115 /**
116 * Gets a long preference
117 * @param key The preference key
118 * @param def The default value to use
119 * @return The long value or the default value if it could not be parsed
120 */
121 long getLong(String key, long def);
122
123 /**
124 * Set a long value for a certain setting.
125 * @param key the unique identifier for the setting
126 * @param value The new value
127 * @return {@code true}, if something has changed (i.e. value is different than before)
128 */
129 boolean putLong(String key, long value);
130
131 /**
132 * Gets a double preference
133 * @param key The preference key
134 * @param def The default value to use
135 * @return The double value or the default value if it could not be parsed
136 */
137 double getDouble(String key, double def);
138
139 /**
140 * Set a boolean value for a certain setting.
141 * @param key the unique identifier for the setting
142 * @param value The new value
143 * @return {@code true}, if something has changed (i.e. value is different than before)
144 * @since 12840
145 */
146 boolean putDouble(String key, double value);
147
148 /**
149 * Get a list of values for a certain key
150 * @param key the identifier for the setting
151 * @param def the default value.
152 * @return the corresponding value if the property has been set before, {@code def} otherwise
153 * @since 12840
154 */
155 List<String> getList(String key, List<String> def);
156
157 /**
158 * Get a list of values for a certain key
159 * @param key the identifier for the setting
160 * @return the corresponding value if the property has been set before, an
161 * empty list otherwise.
162 * @since 12840
163 */
164 default List<String> getList(String key) {
165 List<String> val = getList(key, null);
166 return val == null ? Collections.emptyList() : val;
167 }
168
169 /**
170 * Set a list of values for a certain key.
171 * @param key the identifier for the setting
172 * @param value The new value
173 * @return {@code true}, if something has changed (i.e. value is different than before)
174 * @since 12840
175 */
176 boolean putList(String key, List<String> value);
177
178 /**
179 * Get an array of values (list of lists) for a certain key
180 * @param key the identifier for the setting
181 * @param def the default value.
182 * @return the corresponding value if the property has been set before, {@code def} otherwise
183 * @since 12840
184 */
185 List<List<String>> getListOfLists(String key, List<List<String>> def);
186
187 /**
188 * Get an array of values (list of lists) for a certain key
189 * @param key the identifier for the setting
190 * @return the corresponding value if the property has been set before, an
191 * empty list otherwise
192 * @since 12840
193 */
194 default List<List<String>> getListOfLists(String key) {
195 List<List<String>> val = getListOfLists(key, null);
196 return val == null ? Collections.emptyList() : val;
197 }
198
199 /**
200 * Set an array of values (list of lists) for a certain key.
201 * @param key the identifier for the setting
202 * @param value the new value
203 * @return {@code true}, if something has changed (i.e. value is different than before)
204 * @since 12840
205 */
206 boolean putListOfLists(String key, List<List<String>> value);
207
208 /**
209 * Gets a list of key/value maps.
210 * @param key the key to search at
211 * @param def the default value to use
212 * @return the corresponding value if the property has been set before, {@code def} otherwise
213 * @since 12840
214 */
215 List<Map<String, String>> getListOfMaps(String key, List<Map<String, String>> def);
216
217 /**
218 * Gets a list of key/value maps.
219 * @param key the key to search at
220 * @return the corresponding value if the property has been set before, an
221 * empty list otherwise
222 * @since 12840
223 */
224 default List<Map<String, String>> getListOfMaps(String key) {
225 List<Map<String, String>> val = getListOfMaps(key, null);
226 return val == null ? Collections.emptyList() : val;
227 }
228
229 /**
230 * Set an a list of key/value maps.
231 * @param key the key to store the list in
232 * @param value a list of key/value maps
233 * @return <code>true</code> if the value was changed
234 * @since 12840
235 */
236 boolean putListOfMaps(String key, List<Map<String, String>> value);
237
238}
Note: See TracBrowser for help on using the repository browser.