1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.spi.preferences;
|
---|
3 |
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import 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 | */
|
---|
17 | public 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 a boolean 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 double preference
|
---|
117 | * @param key The preference key
|
---|
118 | * @param def The default value to use
|
---|
119 | * @return The double value or the default value if it could not be parsed
|
---|
120 | */
|
---|
121 | double getDouble(String key, double def);
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Set a boolean 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 | * @since 12840
|
---|
129 | */
|
---|
130 | boolean putDouble(String key, double value);
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Get a list of values for a certain key
|
---|
134 | * @param key the identifier for the setting
|
---|
135 | * @param def the default value.
|
---|
136 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
137 | * @since 12840
|
---|
138 | */
|
---|
139 | List<String> getList(String key, List<String> def);
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Get a list of values for a certain key
|
---|
143 | * @param key the identifier for the setting
|
---|
144 | * @return the corresponding value if the property has been set before, an
|
---|
145 | * empty list otherwise.
|
---|
146 | * @since 12840
|
---|
147 | */
|
---|
148 | default List<String> getList(String key) {
|
---|
149 | List<String> val = getList(key, null);
|
---|
150 | return val == null ? Collections.emptyList() : val;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Set a list of values for a certain key.
|
---|
155 | * @param key the identifier for the setting
|
---|
156 | * @param value The new value
|
---|
157 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
158 | * @since 12840
|
---|
159 | */
|
---|
160 | boolean putList(String key, List<String> value);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Get an array of values (list of lists) for a certain key
|
---|
164 | * @param key the identifier for the setting
|
---|
165 | * @param def the default value.
|
---|
166 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
167 | * @since 12840
|
---|
168 | */
|
---|
169 | List<List<String>> getListOfLists(String key, List<List<String>> def);
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Get an array of values (list of lists) for a certain key
|
---|
173 | * @param key the identifier for the setting
|
---|
174 | * @return the corresponding value if the property has been set before, an
|
---|
175 | * empty list otherwise
|
---|
176 | * @since 12840
|
---|
177 | */
|
---|
178 | default List<List<String>> getListOfLists(String key) {
|
---|
179 | List<List<String>> val = getListOfLists(key, null);
|
---|
180 | return val == null ? Collections.emptyList() : val;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Set an array of values (list of lists) for a certain key.
|
---|
185 | * @param key the identifier for the setting
|
---|
186 | * @param value the new value
|
---|
187 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
---|
188 | * @since 12840
|
---|
189 | */
|
---|
190 | boolean putListOfLists(String key, List<List<String>> value);
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Gets a list of key/value maps.
|
---|
194 | * @param key the key to search at
|
---|
195 | * @param def the default value to use
|
---|
196 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
---|
197 | * @since 12840
|
---|
198 | */
|
---|
199 | List<Map<String, String>> getListOfMaps(String key, List<Map<String, String>> def);
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Gets a list of key/value maps.
|
---|
203 | * @param key the key to search at
|
---|
204 | * @return the corresponding value if the property has been set before, an
|
---|
205 | * empty list otherwise
|
---|
206 | * @since 12840
|
---|
207 | */
|
---|
208 | default List<Map<String, String>> getListOfMaps(String key) {
|
---|
209 | List<Map<String, String>> val = getListOfMaps(key, null);
|
---|
210 | return val == null ? Collections.emptyList() : val;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Set an a list of key/value maps.
|
---|
215 | * @param key the key to store the list in
|
---|
216 | * @param value a list of key/value maps
|
---|
217 | * @return <code>true</code> if the value was changed
|
---|
218 | * @since 12840
|
---|
219 | */
|
---|
220 | boolean putListOfMaps(String key, List<Map<String, String>> value);
|
---|
221 |
|
---|
222 | }
|
---|