Ticket #7201: 7201.patch

File 7201.patch, 3.1 KB (added by simon04, 10 years ago)
  • src/org/openstreetmap/josm/data/osm/Tag.java

    diff --git a/src/org/openstreetmap/josm/data/osm/Tag.java b/src/org/openstreetmap/josm/data/osm/Tag.java
    index 8216955..7f7df4e 100644
    a b public class Tag { 
    7878        return this.key.equals(key);
    7979    }
    8080
    81     /**
    82      * Normalizes the key and the value of the tag by
    83      * <ul>
    84      *   <li>removing leading and trailing white space</li>
    85      * <ul>
    86      *
    87      */
    88     public void normalize() {
    89         key = key.trim();
    90         value = value.trim();
    91     }
    92 
    9381    @Override
    9482    public int hashCode() {
    9583        final int prime = 31;
  • src/org/openstreetmap/josm/data/osm/TagCollection.java

    diff --git a/src/org/openstreetmap/josm/data/osm/TagCollection.java b/src/org/openstreetmap/josm/data/osm/TagCollection.java
    index 0435382..442265a 100644
    a b import static org.openstreetmap.josm.tools.I18n.tr; 
    66import java.util.ArrayList;
    77import java.util.Arrays;
    88import java.util.Collection;
    9 import java.util.Collections;
    109import java.util.HashMap;
    1110import java.util.HashSet;
    1211import java.util.Iterator;
     12import java.util.LinkedHashMap;
     13import java.util.LinkedHashSet;
    1314import java.util.List;
    1415import java.util.Map;
    1516import java.util.Map.Entry;
    1617import java.util.Set;
     18import org.openstreetmap.josm.tools.Utils;
    1719
    1820/**
    1921 * TagCollection is a collection of tags which can be used to manipulate
    public class TagCollection implements Iterable<Tag> { 
    711713
    712714        // See #7201 combining ways screws up the order of ref tags
    713715        Set<String> originalValues = getValues(key);
    714         if (originalValues.size() == 1)
     716        if (originalValues.size() == 1) {
    715717            return originalValues.iterator().next();
     718        }
    716719
    717         StringBuilder buffer = new StringBuilder();
    718         Set<String> valSet = new HashSet<String>();
    719         for (String vs : originalValues) {
    720             valSet.addAll(Arrays.asList(vs.split(";")));
     720        Set<String> values = new LinkedHashSet<String>();
     721        Map<String, Collection<String>> originalSplitValues = new LinkedHashMap<String, Collection<String>>();
     722        for (String v : originalValues) {
     723            List<String> vs = Arrays.asList(v.split(";\\s*"));
     724            originalSplitValues.put(v, vs);
     725            values.addAll(vs);
    721726        }
    722         List<String> values = new ArrayList<String>(valSet);
    723727        values.remove("");
    724         Collections.sort(values);
    725         Iterator<String> iter = values.iterator();
    726         while (iter.hasNext()) {
    727             buffer.append(iter.next());
    728             if (iter.hasNext()) {
    729                 buffer.append(";");
     728        // try to retain an already existing key if it contains all needed values (remove this if it causes performance problems)
     729        for (Entry<String, Collection<String>> i : originalSplitValues.entrySet()) {
     730            if (i.getValue().containsAll(values)) {
     731                return i.getKey();
    730732            }
    731733        }
    732         return buffer.toString();
     734        return Utils.join(";", values);
    733735    }
    734736
    735737    @Override