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

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

Location:
trunk/src/org/openstreetmap/josm/data
Files:
2 edited

Legend:

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

    r12885 r12891  
    660660     * @param def The default value
    661661     * @return The boolean value or the default value if it could not be parsed
    662      */
     662     * @deprecated use {@link PreferencesUtils#getBoolean(IPreferences, String, String, boolean)}
     663     */
     664    @Deprecated
    663665    public synchronized boolean getBoolean(final String key, final String specName, final boolean def) {
    664666        boolean generic = getBoolean(key, def);
     
    10761078     * @param def The default value
    10771079     * @return The integer value or the default value if it could not be parsed
    1078      */
     1080     * @deprecated use {@link PreferencesUtils#getInteger(IPreferences, String, String, int)
     1081     */
     1082    @Deprecated
    10791083    public synchronized int getInteger(String key, String specName, int def) {
    10801084        String v = get(key+'.'+specName);
     
    11221126     * @param value The value that should be removed in the collection
    11231127     * @see #getList(String)
    1124      */
     1128     * @deprecated use {@link PreferencesUtils#removeFromCollection(IPreferences, String, String)}
     1129     */
     1130    @Deprecated
    11251131    public synchronized void removeFromCollection(String key, String value) {
    11261132        List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList()));
     
    12301236     * @param val value
    12311237     * @return {@code true}, if something has changed (i.e. value is different than before)
    1232      */
     1238     * @deprecated use {@link PreferencesUtils#putCollectionBounded(IPreferences, String, int, Collection)}
     1239     */
     1240    @Deprecated
    12331241    public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
    12341242        List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
     
    12481256     * @param def default array value
    12491257     * @return array value
    1250      */
     1258     * @deprecated use {@link #getListOfLists(java.lang.String, java.util.List)}
     1259     */
     1260    @Deprecated
    12511261    @SuppressWarnings({ "unchecked", "rawtypes" })
    12521262    public synchronized Collection<Collection<String>> getArray(String key, Collection<Collection<String>> def) {
  • 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.