Changeset 3547 in josm


Ignore:
Timestamp:
2010-09-19T21:54:08+02:00 (14 years ago)
Author:
bastiK
Message:

fix preference: putCollection shifts the arguments left, when first ones are null; fix return value for putArray

File:
1 edited

Legend:

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

    r3531 r3547  
    2626import java.util.Map.Entry;
    2727import java.util.concurrent.CopyOnWriteArrayList;
    28 import java.util.regex.Matcher;
    29 import java.util.regex.Pattern;
    3028
    3129import javax.swing.JOptionPane;
     
    205203    synchronized public Map<String, String> getAllPrefix(final String prefix) {
    206204        final Map<String,String> all = new TreeMap<String,String>();
    207         for (final Entry<String,String> e : properties.entrySet())
     205        for (final Entry<String,String> e : properties.entrySet()) {
    208206            if (e.getKey().startsWith(prefix)) {
    209207                all.put(e.getKey(), e.getValue());
    210208            }
     209        }
    211210        return all;
    212211    }
     
    214213    synchronized private Map<String, String> getAllPrefixDefault(final String prefix) {
    215214        final Map<String,String> all = new TreeMap<String,String>();
    216         for (final Entry<String,String> e : defaults.entrySet())
     215        for (final Entry<String,String> e : defaults.entrySet()) {
    217216            if (e.getKey().startsWith(prefix)) {
    218217                all.put(e.getKey(), e.getValue());
    219218            }
     219        }
    220220        return all;
    221221    }
     
    223223    synchronized public TreeMap<String, String> getAllColors() {
    224224        final TreeMap<String,String> all = new TreeMap<String,String>();
    225         for (final Entry<String,String> e : defaults.entrySet())
     225        for (final Entry<String,String> e : defaults.entrySet()) {
    226226            if (e.getKey().startsWith("color.") && e.getValue() != null) {
    227227                all.put(e.getKey().substring(6), e.getValue());
    228228            }
    229         for (final Entry<String,String> e : properties.entrySet())
     229        }
     230        for (final Entry<String,String> e : properties.entrySet()) {
    230231            if (e.getKey().startsWith("color.")) {
    231232                all.put(e.getKey().substring(6), e.getValue());
    232233            }
     234        }
    233235        return all;
    234236    }
     
    579581        return def;
    580582    }
     583
    581584    synchronized public void removeFromCollection(String key, String value) {
    582585        List<String> a = new ArrayList<String>(getCollection(key, Collections.<String>emptyList()));
     
    584587        putCollection(key, a);
    585588    }
     589
    586590    synchronized public boolean putCollection(String key, Collection<String> val) {
    587591        String s = null;
     
    590594            for(String a : val)
    591595            {
     596                if (a == null) {
     597                    a = "";
     598                }
    592599                if(s != null) {
    593600                    s += "\u001e" + a;
     
    599606        return put(key, s);
    600607    }
     608   
    601609    synchronized private void putCollectionDefault(String key, Collection<String> val) {
    602610        String s = null;
     
    611619        putDefault(key, s);
    612620    }
     621   
    613622    synchronized public Collection<Collection<String>> getArray(String key,
    614623    Collection<Collection<String>> def) {
     
    618627        int num = 0;
    619628        Collection<Collection<String>> col = new LinkedList<Collection<String>>();
    620         while(properties.containsKey(key+num))
     629        while(properties.containsKey(key+num)) {
    621630            col.add(getCollection(key+num++, null));
     631        }
    622632        return num == 0 && def != null ? def : col;
    623633    }
     634   
    624635    synchronized public boolean putArray(String key, Collection<Collection<String>> val) {
    625         boolean res = true;
     636        boolean changed = false;
    626637        key += ".";
    627638        Collection<String> keys = getAllPrefix(key).keySet();
     
    630641            for(Collection<String> c : val) {
    631642                keys.remove(key+num);
    632                 if(!putCollection(key+num++, c))
    633                     res = false;
     643                changed |= putCollection(key+num++, c);
    634644            }
    635645        }
     
    638648            try {
    639649              Integer.valueOf(k.substring(l));
    640               put(k, null);
    641             } catch(Exception e) {
    642             }
    643         }
    644         return res;
     650              changed |= put(k, null);
     651            } catch(NumberFormatException e) {
     652                System.err.println("Warning: invalid preference.");
     653                e.printStackTrace();
     654            }
     655        }
     656        return changed;
    645657    }
    646658
Note: See TracChangeset for help on using the changeset viewer.