Changeset 9755 in josm


Ignore:
Timestamp:
2016-02-08T00:01:48+01:00 (8 years ago)
Author:
bastiK
Message:

add preference type MultiMap (closes #12437)

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r9717 r9755  
    7272import org.openstreetmap.josm.tools.ColorHelper;
    7373import org.openstreetmap.josm.tools.I18n;
     74import org.openstreetmap.josm.tools.MultiMap;
    7475import org.openstreetmap.josm.tools.Utils;
    7576import org.xml.sax.SAXException;
     
    13971398                Entry e = (Entry) o;
    13981399                Object evalue = e.getValue();
    1399                 if (evalue instanceof Collection) {
    1400                     JsonArrayBuilder a = Json.createArrayBuilder();
    1401                     for (Object evo: (Collection) evalue) {
    1402                         a.add(evo.toString());
    1403                     }
    1404                     object.add(e.getKey().toString(), a.build());
    1405                 } else {
    1406                     object.add(e.getKey().toString(), evalue.toString());
    1407                 }
     1400                object.add(e.getKey().toString(), evalue.toString());
    14081401            }
    14091402            writer.writeObject(object.build());
     
    14201413            for (Entry<String, JsonValue> e: object.entrySet()) {
    14211414                JsonValue value = e.getValue();
     1415                if (value instanceof JsonString) {
     1416                    // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
     1417                    ret.put(e.getKey(), ((JsonString) value).getString());
     1418                } else {
     1419                    ret.put(e.getKey(), e.getValue().toString());
     1420                }
     1421            }
     1422        }
     1423        return ret;
     1424    }
     1425
     1426    @SuppressWarnings("rawtypes")
     1427    private static String multiMapToJson(MultiMap map) {
     1428        StringWriter stringWriter = new StringWriter();
     1429        try (JsonWriter writer = Json.createWriter(stringWriter)) {
     1430            JsonObjectBuilder object = Json.createObjectBuilder();
     1431            for (Object o: map.entrySet()) {
     1432                Entry e = (Entry) o;
     1433                Set evalue = (Set) e.getValue();
     1434                JsonArrayBuilder a = Json.createArrayBuilder();
     1435                for (Object evo: (Collection) evalue) {
     1436                    a.add(evo.toString());
     1437                }
     1438                object.add(e.getKey().toString(), a.build());
     1439            }
     1440            writer.writeObject(object.build());
     1441        }
     1442        return stringWriter.toString();
     1443    }
     1444
     1445    @SuppressWarnings({ "rawtypes", "unchecked" })
     1446    private static MultiMap multiMapFromJson(String s) {
     1447        MultiMap ret = null;
     1448        try (JsonReader reader = Json.createReader(new StringReader(s))) {
     1449            JsonObject object = reader.readObject();
     1450            ret = new MultiMap(object.size());
     1451            for (Entry<String, JsonValue> e: object.entrySet()) {
     1452                JsonValue value = e.getValue();
    14221453                if (value instanceof JsonArray) {
    1423                     List<String> finalList = new ArrayList<String>();
    14241454                    for (JsonString js: ((JsonArray) value).getValuesAs(JsonString.class)) {
    1425                         finalList.add(js.getString());
     1455                        ret.put(e.getKey(), js.getString());
    14261456                    }
    1427                     ret.put(e.getKey(), finalList);
    14281457                } else if (value instanceof JsonString) {
    14291458                    // in some cases, when JsonValue.toString() is called, then additional quotation marks are left in value
     
    14751504                        if (fieldValue instanceof Map) {
    14761505                            hash.put(key, mapToJson((Map) fieldValue));
     1506                        } else if (fieldValue instanceof MultiMap) {
     1507                            hash.put(key, multiMapToJson((MultiMap) fieldValue));
    14771508                        } else {
    14781509                            hash.put(key, fieldValue.toString());
     
    15391570            } else if (f.getType().isAssignableFrom(Map.class)) {
    15401571                value = mapFromJson(key_value.getValue());
     1572            } else if (f.getType().isAssignableFrom(MultiMap.class)) {
     1573                value = multiMapFromJson(key_value.getValue());
    15411574            } else
    15421575                throw new RuntimeException("unsupported preference primitive type");
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java

    r9658 r9755  
    1313import java.util.Map;
    1414import java.util.Objects;
     15import java.util.Set;
    1516import java.util.TreeSet;
    1617import java.util.regex.Matcher;
     
    3233import org.openstreetmap.josm.tools.ImageProvider;
    3334import org.openstreetmap.josm.tools.LanguageInfo;
     35import org.openstreetmap.josm.tools.MultiMap;
    3436
    3537/**
     
    219221        @pref String icon;
    220222        @pref String description;
    221         @pref Map<String, List<String>> noTileHeaders;
    222         @pref Map<String, List<String>> noTileChecksums;
     223        @pref MultiMap<String, String> noTileHeaders;
     224        @pref MultiMap<String, String> noTileChecksums;
    223225        @pref int tileSize = -1;
    224226        @pref Map<String, String> metadataHeaders;
     
    279281            }
    280282            if (i.noTileHeaders != null && !i.noTileHeaders.isEmpty()) {
    281                 noTileHeaders = i.noTileHeaders;
     283                noTileHeaders = new MultiMap<>(i.noTileHeaders);
    282284            }
    283285
    284286            if (i.noTileChecksums != null && !i.noTileChecksums.isEmpty()) {
    285                 noTileChecksums = i.noTileChecksums;
     287                noTileChecksums = new MultiMap<>(i.noTileChecksums);
    286288            }
    287289
     
    412414        icon = e.icon;
    413415        if (e.noTileHeaders != null) {
    414             noTileHeaders = e.noTileHeaders;
     416            noTileHeaders = e.noTileHeaders.toMap();
    415417        }
    416418        if (e.noTileChecksums != null) {
    417             noTileChecksums = e.noTileChecksums;
     419            noTileChecksums = e.noTileChecksums.toMap();
    418420        }
    419421        setTileSize(e.tileSize);
     
    978980     * @since 9613
    979981     */
    980     public void setNoTileHeaders(Map<String, List<String>> noTileHeaders) {
    981        this.noTileHeaders = noTileHeaders;
    982     }
    983 
    984     @Override
    985     public Map<String, List<String>> getNoTileHeaders() {
     982    public void setNoTileHeaders(MultiMap<String, String> noTileHeaders) {
     983       if (noTileHeaders == null) {
     984           this.noTileHeaders = null;
     985       } else {
     986            this.noTileHeaders = noTileHeaders.toMap();
     987       }
     988    }
     989
     990    @Override
     991    public Map<String, Set<String>> getNoTileHeaders() {
    986992        return noTileHeaders;
    987993    }
     
    9941000     * @since 9613
    9951001     */
    996     public void setNoTileChecksums(Map<String, List<String>> noTileChecksums) {
    997        this.noTileChecksums = noTileChecksums;
    998     }
    999 
    1000     @Override
    1001     public Map<String, List<String>> getNoTileChecksums() {
     1002    public void setNoTileChecksums(MultiMap<String, String> noTileChecksums) {
     1003        if (noTileChecksums == null) {
     1004            this.noTileChecksums = null;
     1005        } else {
     1006            this.noTileChecksums = noTileChecksums.toMap();
     1007        }
     1008    }
     1009
     1010    @Override
     1011    public Map<String, Set<String>> getNoTileChecksums() {
    10021012        return noTileChecksums;
    10031013    }
  • trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java

    r9658 r9755  
    2323import org.openstreetmap.josm.tools.HttpClient;
    2424import org.openstreetmap.josm.tools.LanguageInfo;
     25import org.openstreetmap.josm.tools.MultiMap;
    2526import org.openstreetmap.josm.tools.Utils;
    2627import org.xml.sax.Attributes;
     
    99100        private String lang;
    100101        private List<String> projections;
    101         private Map<String, List<String>> noTileHeaders;
    102         private Map<String, List<String>> noTileChecksums;
     102        private MultiMap<String, String> noTileHeaders;
     103        private MultiMap<String, String> noTileChecksums;
    103104        private Map<String, String> metadataHeaders;
    104105
     
    132133                    skipEntry = false;
    133134                    newState = State.ENTRY;
    134                     noTileHeaders = new HashMap<>();
    135                     noTileChecksums = new HashMap<>();
     135                    noTileHeaders = new MultiMap<>();
     136                    noTileChecksums = new MultiMap<>();
    136137                    metadataHeaders = new HashMap<>();
    137138                }
     
    196197                    mirrorEntry = new ImageryInfo();
    197198                } else if ("no-tile-header".equals(qName)) {
    198                     String name = atts.getValue("name");
    199                     List<String> l;
    200                     if (noTileHeaders.containsKey(name)) {
    201                         l = noTileHeaders.get(name);
    202                     } else {
    203                         l = new ArrayList<String>();
    204                         noTileHeaders.put(atts.getValue("name"), l);
    205                     }
    206                     l.add(atts.getValue("value"));
     199                    noTileHeaders.put(atts.getValue("name"), atts.getValue("value"));
    207200                    newState = State.NO_TILE;
    208201                } else if ("no-tile-checksum".equals(qName)) {
    209202                    String type = atts.getValue("type");
    210                     List<String> l;
    211                     if (noTileChecksums.containsKey(type)) {
    212                         l = noTileChecksums.get(type);
    213                     } else {
    214                         l = new ArrayList<String>();
    215                         noTileChecksums.put(type, l);
    216                     }
    217                     l.add(atts.getValue("value"));
     203                    noTileChecksums.put(atts.getValue("type"), atts.getValue("value"));
    218204                    newState = State.NO_TILESUM;
    219205                } else if ("metadata-header".equals(qName)) {
  • trunk/src/org/openstreetmap/josm/tools/MultiMap.java

    r9371 r9755  
    44import java.util.ArrayList;
    55import java.util.Collection;
     6import java.util.Collections;
    67import java.util.HashMap;
    78import java.util.LinkedHashSet;
     
    4344
    4445    /**
     46     * Constructs a new {@code MultiMap} from an ordinary {@code Map}.
     47     * @param map0 the {@code Map}
     48     */
     49    public MultiMap(Map<A, Set<B>> map0) {
     50        if (map0 == null) {
     51            map = new HashMap<>();
     52        } else {
     53            map = new HashMap<>(Utils.hashMapInitialCapacity(map0.size()));
     54            for (Entry<A, Set<B>> e : map0.entrySet()) {
     55                map.put(e.getKey(), new LinkedHashSet<>(e.getValue()));
     56            }
     57        }
     58    }
     59
     60    /**
    4561     * Map a key to a value.
    4662     *
     
    216232    public int hashCode() {
    217233        return Objects.hash(map);
     234    }
     235
     236    /**
     237     * Converts this {@code MultiMap} to a {@code Map} with {@code Set} values.
     238     * @return the converted {@code Map}
     239     */
     240    public Map<A, Set<B>> toMap() {
     241        Map<A, Set<B>> result = new HashMap<>();
     242        for (Entry<A, Set<B>> e : map.entrySet()) {
     243            result.put(e.getKey(), Collections.unmodifiableSet(e.getValue()));
     244        }
     245        return result;
    218246    }
    219247
Note: See TracChangeset for help on using the changeset viewer.