001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.spi.preferences;
003
004import java.util.Collection;
005import java.util.Collections;
006import java.util.List;
007import java.util.Map;
008import java.util.Set;
009
010import org.openstreetmap.josm.io.auth.CredentialsAgent;
011
012/**
013 * Interface for preference handling.
014 *
015 * Allows to save and retrieve user defined settings. The backend storage depends
016 * on the implementation.
017 * @since 12847
018 */
019public interface IPreferences {
020
021    /**
022     * Adds a new preferences listener.
023     * @param listener The listener to add
024     */
025    void addPreferenceChangeListener(PreferenceChangedListener listener);
026
027    /**
028     * Removes a preferences listener.
029     * @param listener The listener to remove
030     */
031    void removePreferenceChangeListener(PreferenceChangedListener listener);
032
033    /**
034     * Adds a listener that only listens to changes in one preference
035     * @param key The preference key to listen to
036     * @param listener The listener to add.
037     */
038    void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener);
039
040    /**
041     * Removes a listener that only listens to changes in one preference
042     * @param key The preference key to listen to
043     * @param listener The listener to add.
044     */
045    void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener);
046
047    /**
048     * Get settings value for a certain key and provide a default value.
049     * @param key the identifier for the setting
050     * @param def the default value. For each call of get() with a given key, the
051     * default value must be the same. {@code def} may be null.
052     * @return the corresponding value if the property has been set before, {@code def} otherwise
053     */
054    String get(String key, String def);
055
056    /**
057     * Get settings value for a certain key.
058     * @param key the identifier for the setting
059     * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
060     */
061    default String get(final String key) {
062        return get(key, "");
063    }
064
065    /**
066     * Set a value for a certain setting.
067     * @param key the unique identifier for the setting
068     * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
069     * @return {@code true}, if something has changed (i.e. value is different than before)
070     */
071    boolean put(String key, String value);
072
073    /**
074     * Gets a boolean preference
075     * @param key The preference key
076     * @param def The default value to use
077     * @return The boolean, <code>false</code> if it could not be parsed, the default value if it is unset
078     */
079    boolean getBoolean(String key, boolean def);
080
081    /**
082     * Gets a boolean preference
083     * @param key The preference key
084     * @return The boolean or <code>false</code> if it could not be parsed
085     */
086    default boolean getBoolean(String key) {
087        return getBoolean(key, false);
088    }
089
090    /**
091     * Set a boolean value for a certain setting.
092     * @param key the unique identifier for the setting
093     * @param value The new value
094     * @return {@code true}, if something has changed (i.e. value is different than before)
095     * @since 12840
096     */
097    boolean putBoolean(String key, boolean value);
098
099    /**
100     * Gets an integer preference
101     * @param key The preference key
102     * @param def The default value to use
103     * @return The integer
104     * @since 12840
105     */
106    int getInt(String key, int def);
107
108    /**
109     * Set an integer value for a certain setting.
110     * @param key the unique identifier for the setting
111     * @param value The new value
112     * @return {@code true}, if something has changed (i.e. value is different than before)
113     * @since 12840
114     */
115    boolean putInt(String key, int value);
116
117    /**
118     * Gets a long preference
119     * @param key The preference key
120     * @param def The default value to use
121     * @return The long value or the default value if it could not be parsed
122     */
123    long getLong(String key, long def);
124
125    /**
126     * Set a long value for a certain setting.
127     * @param key the unique identifier for the setting
128     * @param value The new value
129     * @return {@code true}, if something has changed (i.e. value is different than before)
130     */
131    boolean putLong(String key, long value);
132
133    /**
134     * Gets a double preference
135     * @param key The preference key
136     * @param def The default value to use
137     * @return The double value or the default value if it could not be parsed
138     */
139    double getDouble(String key, double def);
140
141    /**
142     * Set a boolean value for a certain setting.
143     * @param key the unique identifier for the setting
144     * @param value The new value
145     * @return {@code true}, if something has changed (i.e. value is different than before)
146     * @since 12840
147     */
148    boolean putDouble(String key, double value);
149
150    /**
151     * Get a list of values for a certain key
152     * @param key the identifier for the setting
153     * @param def the default value.
154     * @return the corresponding value if the property has been set before, {@code def} otherwise
155     * @since 12840
156     */
157    List<String> getList(String key, List<String> def);
158
159    /**
160     * Get a list of values for a certain key
161     * @param key the identifier for the setting
162     * @return the corresponding value if the property has been set before, an
163     * empty list otherwise.
164     * @since 12840
165     */
166    default List<String> getList(String key) {
167        List<String> val = getList(key, null);
168        return val == null ? Collections.emptyList() : val;
169    }
170
171    /**
172     * Set a list of values for a certain key.
173     * @param key the identifier for the setting
174     * @param value The new value
175     * @return {@code true}, if something has changed (i.e. value is different than before)
176     * @since 12840
177     */
178    boolean putList(String key, List<String> value);
179
180    /**
181     * Get an array of values (list of lists) for a certain key
182     * @param key the identifier for the setting
183     * @param def the default value.
184     * @return the corresponding value if the property has been set before, {@code def} otherwise
185     * @since 12840
186     */
187    List<List<String>> getListOfLists(String key, List<List<String>> def);
188
189    /**
190     * Get an array of values (list of lists) for a certain key
191     * @param key the identifier for the setting
192     * @return the corresponding value if the property has been set before, an
193     * empty list otherwise
194     * @since 12840
195     */
196    default List<List<String>> getListOfLists(String key) {
197        List<List<String>> val = getListOfLists(key, null);
198        return val == null ? Collections.emptyList() : val;
199    }
200
201    /**
202     * Set an array of values (list of lists) for a certain key.
203     * @param key the identifier for the setting
204     * @param value the new value
205     * @return {@code true}, if something has changed (i.e. value is different than before)
206     * @since 12840
207     */
208    boolean putListOfLists(String key, List<List<String>> value);
209
210    /**
211     * Gets a list of key/value maps.
212     * @param key the key to search at
213     * @param def the default value to use
214     * @return the corresponding value if the property has been set before, {@code def} otherwise
215     * @since 12840
216     */
217    List<Map<String, String>> getListOfMaps(String key, List<Map<String, String>> def);
218
219    /**
220     * Gets a list of key/value maps.
221     * @param key the key to search at
222     * @return the corresponding value if the property has been set before, an
223     * empty list otherwise
224     * @since 12840
225     */
226    default List<Map<String, String>> getListOfMaps(String key) {
227        List<Map<String, String>> val = getListOfMaps(key, null);
228        return val == null ? Collections.emptyList() : val;
229    }
230
231    /**
232     * Set an a list of key/value maps.
233     * @param key the key to store the list in
234     * @param value a list of key/value maps
235     * @return <code>true</code> if the value was changed
236     * @since 12840
237     */
238    boolean putListOfMaps(String key, List<Map<String, String>> value);
239
240    /**
241     * Get the set of all keys that are mapped to a value in this preferences.
242     * @return the set of all keys
243     */
244    Set<String> getKeySet();
245
246    /**
247     * Add sensitive keys
248     * @param caller The calling agent
249     * @param key The key that may contain sensitive information
250     * @since 18650
251     */
252    void addSensitive(CredentialsAgent caller, String key);
253
254    /**
255     * Get sensitive keys
256     * @return The sensitive keys
257     * @since 18650
258     */
259    Collection<String> getSensitive();
260
261    /**
262     * Remove sensitive keys. This removes the key from the sensitive list <i>and</i>
263     * removes the stored preference value.
264     * @param key The key to remove
265     * @since 18650
266     */
267    void removeSensitive(String key);
268}