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

Last change on this file since 13207 was 12663, checked in by Don-vip, 7 years ago

see #15182 - move NameFormatter* from gui to data.osm

  • 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 static java.util.Comparator.comparing;
5import static java.util.Comparator.comparingInt;
6
7import java.util.Comparator;
8import java.util.HashMap;
9import java.util.Map;
10import java.util.function.Function;
11
12import org.openstreetmap.josm.tools.AlphanumComparator;
13
14/**
15 * Comparators for comparing primitives.
16 */
17public final class OsmPrimitiveComparator {
18
19 /**
20 * Returns a comparator comparing primitives by their name using {@link DefaultNameFormatter}.
21 *
22 * {@linkplain DefaultNameFormatter#format(OsmPrimitive) Formatted names} are cached.
23 *
24 * @return a comparator comparing primitives by their name using {@link DefaultNameFormatter}
25 */
26 public static Comparator<OsmPrimitive> comparingNames() {
27 final Comparator<String> digitsLast = comparing(str -> Character.isDigit(str.charAt(0)) ? 1 : 0);
28 return comparing(memoize(DefaultNameFormatter.getInstance()::format),
29 digitsLast.thenComparing(AlphanumComparator.getInstance()));
30 }
31
32 /**
33 * Returns a comparator comparing primitives by their {@linkplain OsmPrimitive#getUniqueId unique id}.
34 *
35 * @return a comparator comparing primitives by their {@linkplain OsmPrimitive#getUniqueId unique id}.
36 */
37 public static Comparator<OsmPrimitive> comparingUniqueId() {
38 return comparing(OsmPrimitive::getUniqueId);
39 }
40
41 /**
42 * Returns a comparator ordering the primitives by type in the order NODE, WAY, RELATION
43 *
44 * @return a comparator ordering the primitives by type in the order NODE, WAY, RELATION
45 */
46 public static Comparator<OsmPrimitive> orderingNodesWaysRelations() {
47 return comparingInt(osm -> osm.getType().ordinal());
48 }
49
50 /**
51 * Returns a comparator ordering the primitives by type in the order WAY, RELATION, NODE
52 *
53 * @return a comparator ordering the primitives by type in the order WAY, RELATION, NODE
54 */
55 public static Comparator<OsmPrimitive> orderingWaysRelationsNodes() {
56 return comparingInt(osm -> {
57 switch (osm.getType()) {
58 case WAY:
59 return 1;
60 case RELATION:
61 return 2;
62 case NODE:
63 return 3;
64 default:
65 throw new IllegalStateException();
66 }
67 });
68 }
69
70 /**
71 * Returns a comparator ordering the primitives by type in the order RELATION, WAY, NODE
72 *
73 * @return a comparator ordering the primitives by type in the order RELATION, WAY, NODE
74 * @since 11679
75 */
76 public static Comparator<OsmPrimitive> orderingRelationsWaysNodes() {
77 return comparingInt(osm -> {
78 switch (osm.getType()) {
79 case RELATION:
80 return 1;
81 case WAY:
82 return 2;
83 case NODE:
84 return 3;
85 default:
86 throw new IllegalStateException();
87 }
88 });
89 }
90
91 private static <T, R> Function<T, R> memoize(Function<T, R> base) {
92 final Map<T, R> cache = new HashMap<>();
93 return t -> cache.computeIfAbsent(t, base);
94 }
95
96 private OsmPrimitiveComparator() {
97 }
98}
Note: See TracBrowser for help on using the repository browser.