| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.spi.preferences;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.Collection;
|
|---|
| 5 | import java.util.Collections;
|
|---|
| 6 | import java.util.List;
|
|---|
| 7 | import java.util.Map;
|
|---|
| 8 | import java.util.Set;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.io.auth.CredentialsAgent;
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Interface for preference handling.
|
|---|
| 14 | *
|
|---|
| 15 | * Allows to save and retrieve user defined settings. The backend storage depends
|
|---|
| 16 | * on the implementation.
|
|---|
| 17 | * @since 12847
|
|---|
| 18 | */
|
|---|
| 19 | public interface IPreferences {
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Adds a new preferences listener.
|
|---|
| 23 | * @param listener The listener to add
|
|---|
| 24 | */
|
|---|
| 25 | void addPreferenceChangeListener(PreferenceChangedListener listener);
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Removes a preferences listener.
|
|---|
| 29 | * @param listener The listener to remove
|
|---|
| 30 | */
|
|---|
| 31 | void removePreferenceChangeListener(PreferenceChangedListener listener);
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Adds a listener that only listens to changes in one preference
|
|---|
| 35 | * @param key The preference key to listen to
|
|---|
| 36 | * @param listener The listener to add.
|
|---|
| 37 | */
|
|---|
| 38 | void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener);
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Removes a listener that only listens to changes in one preference
|
|---|
| 42 | * @param key The preference key to listen to
|
|---|
| 43 | * @param listener The listener to add.
|
|---|
| 44 | */
|
|---|
| 45 | void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener);
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Get settings value for a certain key and provide a default value.
|
|---|
| 49 | * @param key the identifier for the setting
|
|---|
| 50 | * @param def the default value. For each call of get() with a given key, the
|
|---|
| 51 | * default value must be the same. {@code def} may be null.
|
|---|
| 52 | * @return the corresponding value if the property has been set before, {@code def} otherwise
|
|---|
| 53 | */
|
|---|
| 54 | String get(String key, String def);
|
|---|
| 55 |
|
|---|
| 56 | /**
|
|---|
| 57 | * Get settings value for a certain key.
|
|---|
| 58 | * @param key the identifier for the setting
|
|---|
| 59 | * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
|
|---|
| 60 | */
|
|---|
| 61 | default String get(final String key) {
|
|---|
| 62 | return get(key, "");
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Set a value for a certain setting.
|
|---|
| 67 | * @param key the unique identifier for the setting
|
|---|
| 68 | * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
|
|---|
| 69 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
|---|
| 70 | */
|
|---|
| 71 | boolean put(String key, String value);
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Gets a boolean preference
|
|---|
| 75 | * @param key The preference key
|
|---|
| 76 | * @param def The default value to use
|
|---|
| 77 | * @return The boolean, <code>false</code> if it could not be parsed, the default value if it is unset
|
|---|
| 78 | */
|
|---|
| 79 | boolean getBoolean(String key, boolean def);
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * Gets a boolean preference
|
|---|
| 83 | * @param key The preference key
|
|---|
| 84 | * @return The boolean or <code>false</code> if it could not be parsed
|
|---|
| 85 | */
|
|---|
| 86 | default boolean getBoolean(String key) {
|
|---|
| 87 | return getBoolean(key, false);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Set a boolean value for a certain setting.
|
|---|
| 92 | * @param key the unique identifier for the setting
|
|---|
| 93 | * @param value The new value
|
|---|
| 94 | * @return {@code true}, if something has changed (i.e. value is different than before)
|
|---|
| 95 | * @since 12840
|
|---|
| 96 | */
|
|---|
| 97 | boolean putBoolean(String key, boolean value);
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 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 | }
|
|---|