Changeset 12840 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2017-09-13T16:03:51+02:00 (7 years ago)
Author:
bastiK
Message:

see #15229 - extract interface for Preferences class

some changes to the method names and signatures

  • to be more in line with java.util.prefs.Preferences:
    put(String, boolean) -> putBoolean(String, boolean)
    getInteger(String) -> getInt(String)
    putInteger(String, Integer) -> putInt(String, int)
    putDouble(String, Double) -> putDouble(String, double)
    
  • for internal consistency with Setting classes:
    getCollection -> getList
    putCollection -> putList
    getArray -> getListOfLists
    putArray -> putListOfLists
    getListOfStructs -> getListOfMaps
    putListOfStructs -> putListOfMaps
    
Location:
trunk/src/org/openstreetmap/josm/data
Files:
3 added
2 edited

Legend:

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

    r12649 r12840  
    5656
    5757import org.openstreetmap.josm.Main;
     58import org.openstreetmap.josm.data.preferences.AbstractPreferences;
    5859import org.openstreetmap.josm.data.preferences.BooleanProperty;
    5960import org.openstreetmap.josm.data.preferences.ColorProperty;
    60 import org.openstreetmap.josm.data.preferences.DoubleProperty;
     61import org.openstreetmap.josm.data.preferences.IPreferences;
    6162import org.openstreetmap.josm.data.preferences.IntegerProperty;
    6263import org.openstreetmap.josm.data.preferences.ListListSetting;
     
    104105 * @since 74
    105106 */
    106 public class Preferences {
     107public class Preferences extends AbstractPreferences {
    107108
    108109    private static final String COLOR_PREFIX = "color.";
     
    255256     * @param listener The listener to add
    256257     */
     258    @Override
    257259    public void addPreferenceChangeListener(PreferenceChangedListener listener) {
    258260        if (listener != null) {
     
    265267     * @param listener The listener to remove
    266268     */
     269    @Override
    267270    public void removePreferenceChangeListener(PreferenceChangedListener listener) {
    268271        listeners.removeListener(listener);
     
    275278     * @since 10824
    276279     */
     280    @Override
    277281    public void addKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
    278282        listenersForKey(key).addListener(listener);
     
    303307     * @param listener The listener to add.
    304308     */
     309    @Override
    305310    public void removeKeyPreferenceChangeListener(String key, PreferenceChangedListener listener) {
    306311        Optional.ofNullable(keyListeners.get(key)).orElseThrow(
     
    480485
    481486    /**
    482      * Get settings value for a certain key.
    483      * @param key the identifier for the setting
    484      * @return "" if there is nothing set for the preference key, the corresponding value otherwise. The result is not null.
    485      */
    486     public synchronized String get(final String key) {
    487         String value = get(key, null);
    488         return value == null ? "" : value;
    489     }
    490 
    491     /**
    492      * Get settings value for a certain key and provide default a value.
    493      * @param key the identifier for the setting
    494      * @param def the default value. For each call of get() with a given key, the default value must be the same.
    495      * @return the corresponding value if the property has been set before, {@code def} otherwise
    496      */
    497     public synchronized String get(final String key, final String def) {
    498         return getSetting(key, new StringSetting(def), StringSetting.class).getValue();
    499     }
    500 
    501     /**
    502487     * Gets all normal (string) settings that have a key starting with the prefix
    503488     * @param prefix The start of the key
     
    552537
    553538    /**
    554      * Gets a boolean preference
    555      * @param key The preference key
    556      * @return The boolean or <code>false</code> if it could not be parsed
    557      * @see IntegerProperty#get()
    558      */
    559     public synchronized boolean getBoolean(final String key) {
    560         String s = get(key, null);
    561         return s != null && Boolean.parseBoolean(s);
    562     }
    563 
    564     /**
    565      * Gets a boolean preference
    566      * @param key The preference key
    567      * @param def The default value to use
    568      * @return The boolean, <code>false</code> if it could not be parsed, the default value if it is unset
    569      * @see IntegerProperty#get()
    570      */
    571     public synchronized boolean getBoolean(final String key, final boolean def) {
    572         return Boolean.parseBoolean(get(key, Boolean.toString(def)));
    573     }
    574 
    575     /**
    576539     * Gets an boolean that may be specialized
    577540     * @param key The basic key
     
    591554
    592555    /**
    593      * Set a value for a certain setting.
    594      * @param key the unique identifier for the setting
    595      * @param value the value of the setting. Can be null or "" which both removes the key-value entry.
    596      * @return {@code true}, if something has changed (i.e. value is different than before)
    597      */
    598     public boolean put(final String key, String value) {
    599         return putSetting(key, value == null || value.isEmpty() ? null : new StringSetting(value));
    600     }
    601 
    602     /**
    603556     * Set a boolean value for a certain setting.
    604557     * @param key the unique identifier for the setting
     
    606559     * @return {@code true}, if something has changed (i.e. value is different than before)
    607560     * @see BooleanProperty
    608      */
     561     * @deprecated use {@link IPreferences#putBoolean(String, boolean)}
     562     */
     563    @Deprecated
    609564    public boolean put(final String key, final boolean value) {
    610565        return put(key, Boolean.toString(value));
     
    617572     * @return {@code true}, if something has changed (i.e. value is different than before)
    618573     * @see IntegerProperty#put(Integer)
    619      */
     574     * @deprecated use {@link IPreferences#putInt(String, int)}
     575     */
     576    @Deprecated
    620577    public boolean putInteger(final String key, final Integer value) {
    621578        return put(key, Integer.toString(value));
     
    628585     * @return {@code true}, if something has changed (i.e. value is different than before)
    629586     * @see DoubleProperty#put(Double)
    630      */
     587     * @deprecated use {@link IPreferences#putDouble(java.lang.String, double)}
     588     */
     589    @Deprecated
    631590    public boolean putDouble(final String key, final Double value) {
    632591        return put(key, Double.toString(value));
     
    663622        if (!defaults) {
    664623            /* currently unused, but may help to fix configuration issues in future */
    665             putInteger("josm.version", Version.getInstance().getVersion());
     624            putInt("josm.version", Version.getInstance().getVersion());
    666625
    667626            updateSystemProperties();
     
    974933     * @return The integer
    975934     * @see IntegerProperty#get()
    976      */
     935     * @deprecated use {@link IPreferences#getInt(String, int)}
     936     */
     937    @Deprecated
    977938    public synchronized int getInteger(String key, int def) {
    978939        String v = get(key, Integer.toString(def));
     
    1034995
    1035996    /**
    1036      * Gets a double preference
    1037      * @param key The preference key
    1038      * @param def The default value to use
    1039      * @return The double value or the default value if it could not be parsed
    1040      * @see LongProperty#get()
    1041      */
    1042     public synchronized double getDouble(String key, double def) {
    1043         String v = get(key, Double.toString(def));
    1044         if (null == v)
    1045             return def;
    1046 
    1047         try {
    1048             return Double.parseDouble(v);
    1049         } catch (NumberFormatException e) {
    1050             // fall out
    1051             Logging.trace(e);
    1052         }
    1053         return def;
    1054     }
    1055 
    1056     /**
    1057997     * Get a list of values for a certain key
    1058998     * @param key the identifier for the setting
    1059999     * @param def the default value.
    10601000     * @return the corresponding value if the property has been set before, {@code def} otherwise
    1061      */
     1001     * @deprecated use {@link IPreferences#getList(java.lang.String, java.util.List)}
     1002     */
     1003    @Deprecated
    10621004    public Collection<String> getCollection(String key, Collection<String> def) {
    10631005        return getSetting(key, ListSetting.create(def), ListSetting.class).getValue();
     
    10681010     * @param key the identifier for the setting
    10691011     * @return the corresponding value if the property has been set before, an empty collection otherwise.
    1070      */
     1012     * @deprecated use {@link IPreferences#getList(java.lang.String)}
     1013     */
     1014    @Deprecated
    10711015    public Collection<String> getCollection(String key) {
    1072         Collection<String> val = getCollection(key, null);
     1016        Collection<String> val = getList(key, null);
    10731017        return val == null ? Collections.<String>emptyList() : val;
    10741018    }
     
    10781022     * @param key The preference key the collection is stored with
    10791023     * @param value The value that should be removed in the collection
    1080      * @see #getCollection(String)
     1024     * @see #getList(String)
    10811025     */
    10821026    public synchronized void removeFromCollection(String key, String value) {
    1083         List<String> a = new ArrayList<>(getCollection(key, Collections.<String>emptyList()));
     1027        List<String> a = new ArrayList<>(getList(key, Collections.<String>emptyList()));
    10841028        a.remove(value);
    1085         putCollection(key, a);
     1029        putList(key, a);
    10861030    }
    10871031
     
    10931037     * @return {@code true}, if something has changed (i.e. value is different than before)
    10941038     */
     1039    @Override
    10951040    public boolean putSetting(final String key, Setting<?> setting) {
    10961041        CheckParameterUtil.ensureParameterNotNull(key);
     
    11461091     */
    11471092    @SuppressWarnings("unchecked")
     1093    @Override
    11481094    public synchronized <T extends Setting<?>> T getSetting(String key, T def, Class<T> klass) {
    11491095        CheckParameterUtil.ensureParameterNotNull(key);
     
    11721118     * @param value value
    11731119     * @return {@code true}, if something has changed (i.e. value is different than before)
    1174      */
     1120     * @deprecated use {@link IPreferences#putList(java.lang.String, java.util.List)}
     1121     */
     1122    @Deprecated
    11751123    public boolean putCollection(String key, Collection<String> value) {
    11761124        return putSetting(key, value == null ? null : ListSetting.create(value));
     
    11851133     */
    11861134    public boolean putCollectionBounded(String key, int maxsize, Collection<String> val) {
    1187         Collection<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
     1135        List<String> newCollection = new ArrayList<>(Math.min(maxsize, val.size()));
    11881136        for (String i : val) {
    11891137            if (newCollection.size() >= maxsize) {
     
    11921140            newCollection.add(i);
    11931141        }
    1194         return putCollection(key, newCollection);
     1142        return putList(key, newCollection);
    11951143    }
    11961144
     
    12121160     * @param key The key
    12131161     * @return The collection of string collections or an empty collection as default
    1214      */
     1162     * @deprecated use {@link IPreferences#getListOfLists(java.lang.String)}
     1163     */
     1164    @Deprecated
    12151165    public Collection<Collection<String>> getArray(String key) {
    12161166        Collection<Collection<String>> res = getArray(key, null);
     
    12231173     * @param value value
    12241174     * @return {@code true}, if something has changed (i.e. value is different than before)
    1225      */
     1175     * @deprecated use {@link IPreferences#putListOfLists(java.lang.String, java.util.List)}
     1176     */
     1177    @Deprecated
    12261178    public boolean putArray(String key, Collection<Collection<String>> value) {
    12271179        return putSetting(key, value == null ? null : ListListSetting.create(value));
     
    12331185     * @param def The default value to use
    12341186     * @return The stored value or the default one if it could not be parsed
    1235      */
     1187     * @deprecated use {@link IPreferences#getListOfMaps(java.lang.String, java.util.List)}
     1188     */
     1189    @Deprecated
    12361190    public Collection<Map<String, String>> getListOfStructs(String key, Collection<Map<String, String>> def) {
    12371191        return getSetting(key, new MapListSetting(def == null ? null : new ArrayList<>(def)), MapListSetting.class).getValue();
     
    12431197     * @param value A list of key/value maps
    12441198     * @return <code>true</code> if the value was changed
    1245      * @see #getListOfStructs(String, Collection)
    1246      */
     1199     * @see #getListOfMaps(String, Collection)
     1200     * @deprecated use {@link IPreferences#putListOfMaps(java.lang.String, java.util.List)}
     1201     */
     1202    @Deprecated
    12471203    public boolean putListOfStructs(String key, Collection<Map<String, String>> value) {
    12481204        return putSetting(key, value == null ? null : new MapListSetting(new ArrayList<>(value)));
     
    12901246     */
    12911247    public <T> List<T> getListOfStructs(String key, Collection<T> def, Class<T> klass) {
    1292         Collection<Map<String, String>> prop =
    1293             getListOfStructs(key, def == null ? null : serializeListOfStructs(def, klass));
     1248        List<Map<String, String>> prop =
     1249            getListOfMaps(key, def == null ? null : serializeListOfStructs(def, klass));
    12941250        if (prop == null)
    12951251            return def == null ? null : new ArrayList<>(def);
     
    13141270     */
    13151271    public <T> boolean putListOfStructs(String key, Collection<T> val, Class<T> klass) {
    1316         return putListOfStructs(key, serializeListOfStructs(val, klass));
    1317     }
    1318 
    1319     private static <T> Collection<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
     1272        return putListOfMaps(key, serializeListOfStructs(val, klass));
     1273    }
     1274
     1275    private static <T> List<Map<String, String>> serializeListOfStructs(Collection<T> l, Class<T> klass) {
    13201276        if (l == null)
    13211277            return null;
    1322         Collection<Map<String, String>> vals = new ArrayList<>();
     1278        List<Map<String, String>> vals = new ArrayList<>();
    13231279        for (T struct : l) {
    13241280            if (struct != null) {
     
    15751531     */
    15761532    public Collection<String> getPluginSites() {
    1577         return getCollection("pluginmanager.sites", Collections.singleton(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
     1533        return getList("pluginmanager.sites", Collections.singletonList(Main.getJOSMWebsite()+"/pluginicons%<?plugins=>"));
    15781534    }
    15791535
     
    16021558     */
    16031559    public void setPluginSites(Collection<String> sites) {
    1604         putCollection("pluginmanager.sites", sites);
     1560        putList("pluginmanager.sites", new ArrayList<>(sites));
    16051561    }
    16061562
     
    17021658                }
    17031659                if (modified) {
    1704                     putListOfStructs(key, l);
     1660                    putListOfMaps(key, l);
    17051661                }
    17061662            }
     
    17181674                    l.add(helper.serialize(val.get()));
    17191675                }
    1720                 putListOfStructs(key, l);
     1676                putListOfMaps(key, l);
    17211677            }
    17221678        }
  • trunk/src/org/openstreetmap/josm/data/preferences/CollectionProperty.java

    r10824 r12840  
    88/**
    99 * A property containing a {@code Collection} of {@code String} as value.
     10 * @deprecated use {@link ListProperty}
    1011 */
     12@Deprecated
    1113public class CollectionProperty extends AbstractProperty<Collection<String>> {
    1214
Note: See TracChangeset for help on using the changeset viewer.