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 | /**
|
---|
11 | * Comparator, comparing pritimives by:<ul>
|
---|
12 | * <li>type and ids in "quick" mode</li>
|
---|
13 | * <li>type and objects display names instead</li>
|
---|
14 | * </ul>
|
---|
15 | * @since 4113
|
---|
16 | */
|
---|
17 | public class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
|
---|
18 | private final Map<OsmPrimitive, String> cache = new HashMap<>();
|
---|
19 | private final boolean relationsFirst;
|
---|
20 | private final boolean quick;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Constructs a new {@code OsmPrimitiveComparator}.
|
---|
24 | */
|
---|
25 | public OsmPrimitiveComparator() {
|
---|
26 | this(false, false);
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code OsmPrimitiveComparator}.
|
---|
31 | * @param quick if {@code true}, sorts by type and ids (fast), otherwise sort by type and display names (slower)
|
---|
32 | * @param relationsFirst if {@code true}, always list relations first
|
---|
33 | */
|
---|
34 | public OsmPrimitiveComparator(boolean quick, boolean relationsFirst) {
|
---|
35 | this.quick = quick;
|
---|
36 | this.relationsFirst = relationsFirst;
|
---|
37 | }
|
---|
38 |
|
---|
39 | private String cachedName(OsmPrimitive p) {
|
---|
40 | String name = cache.get(p);
|
---|
41 | if (name == null) {
|
---|
42 | name = p.getDisplayName(DefaultNameFormatter.getInstance());
|
---|
43 | cache.put(p, name);
|
---|
44 | }
|
---|
45 | return name;
|
---|
46 | }
|
---|
47 |
|
---|
48 | private int compareName(OsmPrimitive a, OsmPrimitive b) {
|
---|
49 | String an = cachedName(a);
|
---|
50 | String bn = cachedName(b);
|
---|
51 | // make sure display names starting with digits are the end of the list
|
---|
52 | if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
|
---|
53 | return an.compareTo(bn);
|
---|
54 | else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
|
---|
55 | return 1;
|
---|
56 | else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
|
---|
57 | return -1;
|
---|
58 | return an.compareTo(bn);
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static int compareId(OsmPrimitive a, OsmPrimitive b) {
|
---|
62 | long idA = a.getUniqueId();
|
---|
63 | long idB = b.getUniqueId();
|
---|
64 | if (idA < idB) return -1;
|
---|
65 | if (idA > idB) return 1;
|
---|
66 | return 0;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private int compareType(OsmPrimitive a, OsmPrimitive b) {
|
---|
70 | if (relationsFirst) {
|
---|
71 | // show relations before ways, then nodes
|
---|
72 | if (a.getType().equals(OsmPrimitiveType.RELATION)) return -1;
|
---|
73 | if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
|
---|
74 | // a is a way
|
---|
75 | if (b.getType().equals(OsmPrimitiveType.RELATION)) return 1;
|
---|
76 | // b is a node
|
---|
77 | } else {
|
---|
78 | // show ways before relations, then nodes
|
---|
79 | if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
|
---|
80 | if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
|
---|
81 | // a is a relation
|
---|
82 | if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
|
---|
83 | // b is a node
|
---|
84 | }
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public int compare(OsmPrimitive a, OsmPrimitive b) {
|
---|
90 | if (a.getType().equals(b.getType()))
|
---|
91 | return quick ? compareId(a, b) : compareName(a, b);
|
---|
92 | return compareType(a, b);
|
---|
93 | }
|
---|
94 | }
|
---|