Changeset 3708 in josm for trunk/src/org


Ignore:
Timestamp:
2010-12-08T22:02:20+01:00 (13 years ago)
Author:
bastiK
Message:

doc & trivial code changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r3696 r3708  
    3838 * saved upon set-access.
    3939 *
     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 *
    4049 * @author imi
    4150 */
     
    5059    private File preferencesDirFile = null;
    5160
     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
    5267    public interface PreferenceChangeEvent{
    5368        String getKey();
     
    106121        }
    107122    }
    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>();
    114123
    115124    /**
     
    186195    }
    187196
     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     */
    188203    synchronized public String get(final String key) {
    189204        putDefault(key, null);
     
    193208    }
    194209
     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     */
    195218    synchronized public String get(final String key, final String def) {
    196219        putDefault(key, def);
     
    258281    }
    259282
     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     */
    260292    public boolean put(final String key, String value) {
    261293        boolean changed = false;
     
    267299                value = null;
    268300            }
    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)) {
    272311                if (value == null) {
    273312                    properties.remove(key);
     
    290329    }
    291330
    292     synchronized public boolean put(final String key, final boolean value) {
     331    public boolean put(final String key, final boolean value) {
    293332        return put(key, Boolean.toString(value));
    294333    }
    295334
    296     synchronized public boolean putInteger(final String key, final Integer value) {
     335    public boolean putInteger(final String key, final Integer value) {
    297336        return put(key, Integer.toString(value));
    298337    }
    299338
    300     synchronized public boolean putDouble(final String key, final Double value) {
     339    public boolean putDouble(final String key, final Double value) {
    301340        return put(key, Double.toString(value));
    302341    }
    303342
    304     synchronized public boolean putLong(final String key, final Long value) {
     343    public boolean putLong(final String key, final Long value) {
    305344        return put(key, Long.toString(value));
    306345    }
     
    380419                    continue;
    381420                }
    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                }
    383426            }
    384427            if (!errLines.isEmpty())
     
    625668     */
    626669    synchronized public Collection<Collection<String>> getArray(String key,
    627     Collection<Collection<String>> def) {
     670            Collection<Collection<String>> def)
     671    {
    628672        if(def != null)
    629673            putArrayDefault(key, def);
Note: See TracChangeset for help on using the changeset viewer.