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

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

see #15229 - move remaining classes to spi.preferences package, to make it self-contained

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