Ignore:
Timestamp:
2017-09-23T23:20:03+02:00 (7 years ago)
Author:
bastiK
Message:

see #15229 - move non-essential helper methods from Preferences to PreferencesUtils

File:
1 edited

Legend:

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

    r12881 r12891  
    66import java.util.ArrayList;
    77import java.util.Collection;
     8import java.util.Collections;
    89import java.util.HashMap;
    910import java.util.Iterator;
     
    1819
    1920import org.openstreetmap.josm.Main;
     21import org.openstreetmap.josm.spi.preferences.IPreferences;
    2022import org.openstreetmap.josm.spi.preferences.ListListSetting;
    2123import org.openstreetmap.josm.spi.preferences.ListSetting;
     
    476478        engine.eval(init);
    477479    }
     480
     481    /**
     482     * Gets an boolean that may be specialized
     483     * @param prefs the preferences
     484     * @param key The basic key
     485     * @param specName The sub-key to append to the key
     486     * @param def The default value
     487     * @return The boolean value or the default value if it could not be parsed
     488     * @since 12891
     489     */
     490    public static boolean getBoolean(IPreferences prefs, final String key, final String specName, final boolean def) {
     491        synchronized (prefs) {
     492            boolean generic = prefs.getBoolean(key, def);
     493            String skey = key+'.'+specName;
     494            String svalue = prefs.get(skey, null);
     495            if (svalue != null)
     496                return Boolean.parseBoolean(svalue);
     497            else
     498                return generic;
     499        }
     500    }
     501
     502    /**
     503     * Gets an integer that may be specialized
     504     * @param prefs the preferences
     505     * @param key The basic key
     506     * @param specName The sub-key to append to the key
     507     * @param def The default value
     508     * @return The integer value or the default value if it could not be parsed
     509     * @since 12891
     510     */
     511    public static int getInteger(IPreferences prefs, String key, String specName, int def) {
     512        synchronized (prefs) {
     513            String v = prefs.get(key+'.'+specName);
     514            if (v.isEmpty())
     515                v = prefs.get(key, Integer.toString(def));
     516            if (v.isEmpty())
     517                return def;
     518
     519            try {
     520                return Integer.parseInt(v);
     521            } catch (NumberFormatException e) {
     522                // fall out
     523                Logging.trace(e);
     524            }
     525            return def;
     526        }
     527    }
     528
     529    /**
     530     * Removes a value from a given String collection
     531     * @param prefs the preferences
     532     * @param key The preference key the collection is stored with
     533     * @param value The value that should be removed in the collection
     534     * @see #getList(String)
     535     * @since 12891
     536     */
     537    public static void removeFromCollection(IPreferences prefs, String key, String value) {
     538        synchronized (prefs) {
     539            List<String> a = new ArrayList<>(prefs.getList(key, Collections.<String>emptyList()));
     540            a.remove(value);
     541            prefs.putList(key, a);
     542        }
     543    }
     544
     545    /**
     546     * Saves at most {@code maxsize} items of collection {@code val}.
     547     * @param prefs the preferences
     548     * @param key key
     549     * @param maxsize max number of items to save
     550     * @param val value
     551     * @return {@code true}, if something has changed (i.e. value is different than before)
     552     * @since 12891
     553     */
     554    public static boolean putCollectionBounded(IPreferences prefs, String key, int maxsize, Collection<String> val) {
     555        List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
     556        for (String i : val) {
     557            if (newCollection.size() >= maxsize) {
     558                break;
     559            }
     560            newCollection.add(i);
     561        }
     562        return prefs.putList(key, newCollection);
     563    }
     564
    478565}
Note: See TracChangeset for help on using the changeset viewer.