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

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

sonar - Performance - Method passes constant String of length 1 to character overridden method + add unit tests/javadoc

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