source: josm/trunk/src/org/openstreetmap/josm/data/osm/PrimitiveComparator.java@ 19050

Last change on this file since 19050 was 19050, checked in by taylor.smock, 11 months ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

  • Property svn:eol-style set to native
File size: 3.9 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 {@link IPrimitive}.
16 * @since 13803
17 */
18public final class PrimitiveComparator {
19
20 /**
21 * Returns a comparator comparing primitives by their name using {@link DefaultNameFormatter}.
22 *
23 * {@linkplain DefaultNameFormatter#format(IPrimitive) Formatted names} are cached.
24 *
25 * @return a comparator comparing primitives by their name using {@link DefaultNameFormatter}
26 */
27 public static Comparator<IPrimitive> comparingNames() {
28 return doComparingNames();
29 }
30
31 static <T extends IPrimitive> Comparator<T> doComparingNames() {
32 final Comparator<String> digitsLast = comparing(str -> !str.isEmpty() && Character.isDigit(str.charAt(0)) ? 1 : 0);
33 return comparing(memoize(DefaultNameFormatter.getInstance()::format),
34 digitsLast.thenComparing(AlphanumComparator.getInstance()));
35 }
36
37 /**
38 * Returns a comparator comparing primitives by their {@linkplain IPrimitive#getUniqueId unique id}.
39 *
40 * @return a comparator comparing primitives by their {@linkplain IPrimitive#getUniqueId unique id}.
41 */
42 public static Comparator<IPrimitive> comparingUniqueId() {
43 return doComparingUniqueId();
44 }
45
46 static <T extends IPrimitive> Comparator<T> doComparingUniqueId() {
47 return comparing(IPrimitive::getUniqueId);
48 }
49
50 /**
51 * Returns a comparator ordering the primitives by type in the order NODE, WAY, RELATION
52 *
53 * @return a comparator ordering the primitives by type in the order NODE, WAY, RELATION
54 */
55 public static Comparator<IPrimitive> orderingNodesWaysRelations() {
56 return doOrderingNodesWaysRelations();
57 }
58
59 static <T extends IPrimitive> Comparator<T> doOrderingNodesWaysRelations() {
60 return comparing(PrimitiveId::getType);
61 }
62
63 /**
64 * Returns a comparator ordering the primitives by type in the order WAY, RELATION, NODE
65 *
66 * @return a comparator ordering the primitives by type in the order WAY, RELATION, NODE
67 */
68 public static Comparator<IPrimitive> orderingWaysRelationsNodes() {
69 return doOrderingWaysRelationsNodes();
70 }
71
72 static <T extends IPrimitive> Comparator<T> doOrderingWaysRelationsNodes() {
73 return comparingInt(osm -> {
74 switch (osm.getType()) {
75 case WAY:
76 return 1;
77 case RELATION:
78 return 2;
79 case NODE:
80 return 3;
81 default:
82 throw new IllegalStateException();
83 }
84 });
85 }
86
87 /**
88 * Returns a comparator ordering the primitives by type in the order RELATION, WAY, NODE
89 *
90 * @return a comparator ordering the primitives by type in the order RELATION, WAY, NODE
91 */
92 public static Comparator<IPrimitive> orderingRelationsWaysNodes() {
93 return doOrderingRelationsWaysNodes();
94 }
95
96 static <T extends IPrimitive> Comparator<T> doOrderingRelationsWaysNodes() {
97 return comparingInt(osm -> {
98 switch (osm.getType()) {
99 case RELATION:
100 return 1;
101 case WAY:
102 return 2;
103 case NODE:
104 return 3;
105 default:
106 throw new IllegalStateException();
107 }
108 });
109 }
110
111 private static <T, R> Function<T, R> memoize(Function<T, R> base) {
112 final Map<T, R> cache = new HashMap<>();
113 return t -> cache.computeIfAbsent(t, base);
114 }
115
116 private PrimitiveComparator() {
117 }
118}
Note: See TracBrowser for help on using the repository browser.