1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Comparator;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
9 |
|
---|
10 | /** Comparator, comparing by type and objects display names */
|
---|
11 | public class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
|
---|
12 | private final Map<OsmPrimitive, String> cache= new HashMap<>();
|
---|
13 | private final DefaultNameFormatter df = DefaultNameFormatter.getInstance();
|
---|
14 | public boolean relationsFirst = false;
|
---|
15 |
|
---|
16 | private String cachedName(OsmPrimitive p) {
|
---|
17 | String name = cache.get(p);
|
---|
18 | if (name == null) {
|
---|
19 | name = p.getDisplayName(df);
|
---|
20 | cache.put(p, name);
|
---|
21 | }
|
---|
22 | return name;
|
---|
23 | }
|
---|
24 |
|
---|
25 | private int compareName(OsmPrimitive a, OsmPrimitive b) {
|
---|
26 | String an = cachedName(a);
|
---|
27 | String bn = cachedName(b);
|
---|
28 | // make sure display names starting with digits are the end of the list
|
---|
29 | if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
|
---|
30 | return an.compareTo(bn);
|
---|
31 | else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
|
---|
32 | return 1;
|
---|
33 | else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
|
---|
34 | return -1;
|
---|
35 | return an.compareTo(bn);
|
---|
36 | }
|
---|
37 |
|
---|
38 | private int compareType(OsmPrimitive a, OsmPrimitive b) {
|
---|
39 | if(relationsFirst) {
|
---|
40 | // show relations before ways, then nodes
|
---|
41 | if (a.getType().equals(OsmPrimitiveType.RELATION)) return -1;
|
---|
42 | if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
|
---|
43 | // a is a way
|
---|
44 | if (b.getType().equals(OsmPrimitiveType.RELATION)) return 1;
|
---|
45 | // b is a node
|
---|
46 | } else {
|
---|
47 | // show ways before relations, then nodes
|
---|
48 | if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
|
---|
49 | if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
|
---|
50 | // a is a relation
|
---|
51 | if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
|
---|
52 | // b is a node
|
---|
53 | }
|
---|
54 | return -1;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public int compare(OsmPrimitive a, OsmPrimitive b) {
|
---|
59 | if (a.getType().equals(b.getType()))
|
---|
60 | return compareName(a, b);
|
---|
61 | return compareType(a, b);
|
---|
62 | }
|
---|
63 | }
|
---|