source: josm/trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java@ 8496

Last change on this file since 8496 was 8496, checked in by simon04, 9 years ago

fix #11505 - Download objects: load a range of OSM objects (modified patch by windu.2b)

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