source: josm/trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveComparator.java@ 9970

Last change on this file since 9970 was 9929, checked in by Don-vip, 8 years ago

findbugs

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.Serializable;
5import java.util.Comparator;
6import java.util.HashMap;
7
8import 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 */
17public class OsmPrimitiveComparator implements Comparator<OsmPrimitive>, Serializable {
18
19 private static final long serialVersionUID = 1L;
20
21 private final HashMap<OsmPrimitive, String> cache = new HashMap<>();
22 private final boolean relationsFirst;
23 private final boolean quick;
24
25 /**
26 * Constructs a new {@code OsmPrimitiveComparator}.
27 */
28 public OsmPrimitiveComparator() {
29 this(false, false);
30 }
31
32 /**
33 * Constructs a new {@code OsmPrimitiveComparator}.
34 * @param quick if {@code true}, sorts by type and ids (fast), otherwise sort by type and display names (slower)
35 * @param relationsFirst if {@code true}, always list relations first
36 */
37 public OsmPrimitiveComparator(boolean quick, boolean relationsFirst) {
38 this.quick = quick;
39 this.relationsFirst = relationsFirst;
40 }
41
42 private String cachedName(OsmPrimitive p) {
43 String name = cache.get(p);
44 if (name == null) {
45 name = p.getDisplayName(DefaultNameFormatter.getInstance());
46 cache.put(p, name);
47 }
48 return name;
49 }
50
51 private int compareName(OsmPrimitive a, OsmPrimitive b) {
52 String an = cachedName(a);
53 String bn = cachedName(b);
54 // make sure display names starting with digits are the end of the list
55 if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
56 return an.compareTo(bn);
57 else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
58 return 1;
59 else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
60 return -1;
61 return an.compareTo(bn);
62 }
63
64 private static int compareId(OsmPrimitive a, OsmPrimitive b) {
65 long idA = a.getUniqueId();
66 long idB = b.getUniqueId();
67 if (idA < idB) return -1;
68 if (idA > idB) return 1;
69 return 0;
70 }
71
72 private int compareType(OsmPrimitive a, OsmPrimitive b) {
73 if (relationsFirst) {
74 // show relations before ways, then nodes
75 if (a.getType().equals(OsmPrimitiveType.RELATION)) return -1;
76 if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
77 // a is a way
78 if (b.getType().equals(OsmPrimitiveType.RELATION)) return 1;
79 // b is a node
80 } else {
81 // show ways before relations, then nodes
82 if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
83 if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
84 // a is a relation
85 if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
86 // b is a node
87 }
88 return -1;
89 }
90
91 @Override
92 public int compare(OsmPrimitive a, OsmPrimitive b) {
93 if (a.getType().equals(b.getType()))
94 return quick ? compareId(a, b) : compareName(a, b);
95 return compareType(a, b);
96 }
97}
Note: See TracBrowser for help on using the repository browser.