1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Objects;
|
---|
8 | import java.util.regex.MatchResult;
|
---|
9 | import java.util.regex.Matcher;
|
---|
10 | import java.util.regex.Pattern;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * A primitive id and a primitive type
|
---|
14 | */
|
---|
15 | public class SimplePrimitiveId implements PrimitiveId, Serializable {
|
---|
16 |
|
---|
17 | private static final long serialVersionUID = 1L;
|
---|
18 |
|
---|
19 | private final long id;
|
---|
20 | private final OsmPrimitiveType type;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A pattern that is used to parse a textual primitive id
|
---|
24 | */
|
---|
25 | public static final Pattern ID_PATTERN = Pattern.compile("(n|node|w|way|r|rel|relation)[ /]?(\\d+)");
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A pattern that is used to parse an id range
|
---|
29 | */
|
---|
30 | public static final Pattern MULTIPLE_IDS_PATTERN = Pattern.compile(ID_PATTERN.pattern() + "(-(\\d+))?");
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Create a new primtive id
|
---|
34 | * @param id The id
|
---|
35 | * @param type The type of the primitive
|
---|
36 | */
|
---|
37 | public SimplePrimitiveId(long id, OsmPrimitiveType type) {
|
---|
38 | this.id = id;
|
---|
39 | this.type = type;
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public OsmPrimitiveType getType() {
|
---|
44 | return type;
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public long getUniqueId() {
|
---|
49 | return id;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public boolean isNew() {
|
---|
54 | return id <= 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public int hashCode() {
|
---|
59 | return Objects.hash(id, type);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public boolean equals(Object obj) {
|
---|
64 | if (this == obj) return true;
|
---|
65 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
66 | SimplePrimitiveId that = (SimplePrimitiveId) obj;
|
---|
67 | return id == that.id &&
|
---|
68 | type == that.type;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public String toString() {
|
---|
73 | return type.toString() + ' ' + id;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Parses a {@code SimplePrimitiveId} from the string {@code s}.
|
---|
78 | * @param s the string to be parsed, e.g., {@code n1}, {@code node1},
|
---|
79 | * {@code w1}, {@code way1}, {@code r1}, {@code rel1}, {@code relation1}.
|
---|
80 | * @return the parsed {@code SimplePrimitiveId}
|
---|
81 | * @throws IllegalArgumentException if the string does not match the pattern
|
---|
82 | */
|
---|
83 | public static SimplePrimitiveId fromString(String s) {
|
---|
84 | final Matcher m = ID_PATTERN.matcher(s);
|
---|
85 | if (m.matches()) {
|
---|
86 | return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())), getOsmPrimitiveType(s.charAt(0)));
|
---|
87 | } else {
|
---|
88 | throw new IllegalArgumentException("The string " + s + " does not match the pattern " + ID_PATTERN);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Parses a range {@code SimplePrimitiveId} from the string {@code s}.
|
---|
94 | * @param s the string to be parsed, e.g., {@code node1}, {@code node1-7}, {@code node70-7}.
|
---|
95 | * @return the parsed {@code SimplePrimitiveId}s
|
---|
96 | * @throws IllegalArgumentException if the string does not match the pattern
|
---|
97 | */
|
---|
98 | public static List<SimplePrimitiveId> multipleFromString(String s) {
|
---|
99 | final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s);
|
---|
100 | if (m.matches()) {
|
---|
101 | return extractIdsInto(m, new ArrayList<SimplePrimitiveId>());
|
---|
102 | } else {
|
---|
103 | throw new IllegalArgumentException("The string " + s + " does not match the pattern " + MULTIPLE_IDS_PATTERN);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Attempts to parse extract any primitive id from the string {@code s}.
|
---|
109 | * @param s the string to be parsed, e.g., {@code "n1, w1"}, {@code "node1 and rel2"}, {@code "node 123-29"}.
|
---|
110 | * @return the parsed list of {@code OsmPrimitiveType}s.
|
---|
111 | */
|
---|
112 | public static List<SimplePrimitiveId> fuzzyParse(String s) {
|
---|
113 | final List<SimplePrimitiveId> ids = new ArrayList<>();
|
---|
114 | final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s);
|
---|
115 | while (m.find()) {
|
---|
116 | extractIdsInto(m, ids);
|
---|
117 | }
|
---|
118 | return ids;
|
---|
119 | }
|
---|
120 |
|
---|
121 | private static List<SimplePrimitiveId> extractIdsInto(MatchResult m, List<SimplePrimitiveId> ids) {
|
---|
122 | final OsmPrimitiveType type = getOsmPrimitiveType(m.group(1).charAt(0));
|
---|
123 | final String firstId = m.group(2);
|
---|
124 | final String lastId = m.group(4);
|
---|
125 | if (lastId != null) {
|
---|
126 | final long lastIdParsed;
|
---|
127 | if (lastId.length() < firstId.length()) {
|
---|
128 | // parse ranges such as 123-25 or 123-5
|
---|
129 | lastIdParsed = Long.parseLong(firstId.substring(0, firstId.length() - lastId.length()) + lastId);
|
---|
130 | } else {
|
---|
131 | // parse ranges such as 123-125 or 998-1001
|
---|
132 | lastIdParsed = Long.parseLong(lastId);
|
---|
133 | }
|
---|
134 | for (long i = Long.parseLong(firstId); i <= lastIdParsed; i++) {
|
---|
135 | ids.add(new SimplePrimitiveId(i, type));
|
---|
136 | }
|
---|
137 | } else {
|
---|
138 | ids.add(new SimplePrimitiveId(Long.parseLong(firstId), type));
|
---|
139 | }
|
---|
140 | return ids;
|
---|
141 | }
|
---|
142 |
|
---|
143 | private static OsmPrimitiveType getOsmPrimitiveType(char firstChar) {
|
---|
144 | return firstChar == 'n' ? OsmPrimitiveType.NODE : firstChar == 'w' ? OsmPrimitiveType.WAY : OsmPrimitiveType.RELATION;
|
---|
145 | }
|
---|
146 | }
|
---|