Changeset 3708 in josm for trunk/src/org/openstreetmap/josm/data
- Timestamp:
- 2010-12-08T22:02:20+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r3696 r3708 38 38 * saved upon set-access. 39 39 * 40 * Each property is a simple key=value pair of Strings. 41 * In addition, each key has a unique default value that is set when the value is first 42 * accessed using one of the get...() methods. You can use the same preference 43 * key in different parts of the code, but the default value must be the same 44 * everywhere. null is a legitimate default value. 45 * 46 * At the moment, there is no such thing as an empty value. 47 * If you put "" or null as value, the property is removed. 48 * 40 49 * @author imi 41 50 */ … … 50 59 private File preferencesDirFile = null; 51 60 61 /** 62 * Map the property name to the property object. Does not contain null or "" values. 63 */ 64 protected final SortedMap<String, String> properties = new TreeMap<String, String>(); 65 protected final SortedMap<String, String> defaults = new TreeMap<String, String>(); 66 52 67 public interface PreferenceChangeEvent{ 53 68 String getKey(); … … 106 121 } 107 122 } 108 109 /**110 * Map the property name to the property object.111 */112 protected final SortedMap<String, String> properties = new TreeMap<String, String>();113 protected final SortedMap<String, String> defaults = new TreeMap<String, String>();114 123 115 124 /** … … 186 195 } 187 196 197 /** 198 * Get settings value for a certain key. 199 * @param key the identifier for the setting 200 * @return "" if there is nothing set for the preference key, 201 * the corresponding value otherwise. The result is not null. 202 */ 188 203 synchronized public String get(final String key) { 189 204 putDefault(key, null); … … 193 208 } 194 209 210 /** 211 * Get settings value for a certain key and provide default a value. 212 * @param key the identifier for the setting 213 * @param def the default value. For each call of get() with a given key, the 214 * default value must be the same. 215 * @return the corresponding value if the property has been set before, 216 * def otherwise 217 */ 195 218 synchronized public String get(final String key, final String def) { 196 219 putDefault(key, def); … … 258 281 } 259 282 283 /** 284 * Set a value for a certain setting. The changed setting is saved 285 * to the preference file immediately. Due to caching mechanisms on modern 286 * operating systems and hardware, this shouldn't be a performance problem. 287 * @param key the unique identifier for the setting 288 * @param value the value of the setting. Can be null or "" wich both removes 289 * the key-value entry. 290 * @return if true, something has changed (i.e. value is different than before) 291 */ 260 292 public boolean put(final String key, String value) { 261 293 boolean changed = false; … … 267 299 value = null; 268 300 } 269 if(!((oldValue == null && (value == null || value.equals(defaults.get(key)))) 270 || (value != null && oldValue != null && oldValue.equals(value)))) 271 { 301 // value is the same as before - no need to save anything 302 boolean equalValue = oldValue != null && oldValue.equals(value); 303 // The setting was previously unset and we are supposed to put a 304 // value that equals the default value. This is not necessary because 305 // the default value is the same throughout josm. In addition we like 306 // to have the possibility to change the default value from version 307 // to version, which would not work if we wrote it to the preference file. 308 boolean unsetIsDefault = oldValue == null && (value == null || value.equals(defaults.get(key))); 309 310 if (!(equalValue || unsetIsDefault)) { 272 311 if (value == null) { 273 312 properties.remove(key); … … 290 329 } 291 330 292 synchronizedpublic boolean put(final String key, final boolean value) {331 public boolean put(final String key, final boolean value) { 293 332 return put(key, Boolean.toString(value)); 294 333 } 295 334 296 synchronizedpublic boolean putInteger(final String key, final Integer value) {335 public boolean putInteger(final String key, final Integer value) { 297 336 return put(key, Integer.toString(value)); 298 337 } 299 338 300 synchronizedpublic boolean putDouble(final String key, final Double value) {339 public boolean putDouble(final String key, final Double value) { 301 340 return put(key, Double.toString(value)); 302 341 } 303 342 304 synchronizedpublic boolean putLong(final String key, final Long value) {343 public boolean putLong(final String key, final Long value) { 305 344 return put(key, Long.toString(value)); 306 345 } … … 380 419 continue; 381 420 } 382 properties.put(line.substring(0,i), line.substring(i+1)); 421 String key = line.substring(0,i); 422 String value = line.substring(i+1); 423 if (!value.isEmpty()) { 424 properties.put(key, value); 425 } 383 426 } 384 427 if (!errLines.isEmpty()) … … 625 668 */ 626 669 synchronized public Collection<Collection<String>> getArray(String key, 627 Collection<Collection<String>> def) { 670 Collection<Collection<String>> def) 671 { 628 672 if(def != null) 629 673 putArrayDefault(key, def);
Note:
See TracChangeset
for help on using the changeset viewer.