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

Last change on this file since 8308 was 8308, checked in by Don-vip, 9 years ago

fix potential NPEs and Sonar issues related to serialization

  • Property svn:eol-style set to native
File size: 3.5 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.Matcher;
8import java.util.regex.Pattern;
9
10public class SimplePrimitiveId implements PrimitiveId, Serializable {
11
12 private static final long serialVersionUID = 1L;
13
14 private final long id;
15 private final OsmPrimitiveType type;
16
17 public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)[ /]?)(\\d+)");
18
19 public SimplePrimitiveId(long id, OsmPrimitiveType type) {
20 this.id = id;
21 this.type = type;
22 }
23
24 @Override
25 public OsmPrimitiveType getType() {
26 return type;
27 }
28
29 @Override
30 public long getUniqueId() {
31 return id;
32 }
33
34 @Override
35 public boolean isNew() {
36 return id <= 0;
37 }
38
39 @Override
40 public int hashCode() {
41 final int prime = 31;
42 int result = 1;
43 result = prime * result + (int) (id ^ (id >>> 32));
44 result = prime * result + ((type == null) ? 0 : type.hashCode());
45 return result;
46 }
47
48 @Override
49 public boolean equals(Object obj) {
50 if (this == obj)
51 return true;
52 if (obj == null)
53 return false;
54 if (getClass() != obj.getClass())
55 return false;
56 SimplePrimitiveId other = (SimplePrimitiveId) obj;
57 if (id != other.id)
58 return false;
59 if (type == null) {
60 if (other.type != null)
61 return false;
62 } else if (!type.equals(other.type))
63 return false;
64 return true;
65 }
66
67 @Override
68 public String toString() {
69 return type + " " + id;
70 }
71
72 /**
73 * Parses a {@code SimplePrimitiveId} from the string {@code s}.
74 * @param s the string to be parsed, e.g., {@code n1}, {@code node1},
75 * {@code w1}, {@code way1}, {@code r1}, {@code rel1}, {@code relation1}.
76 * @return the parsed {@code SimplePrimitiveId}
77 * @throws IllegalArgumentException if the string does not match the pattern
78 */
79 public static SimplePrimitiveId fromString(String s) {
80 final Matcher m = ID_PATTERN.matcher(s);
81 if (m.matches()) {
82 return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
83 s.charAt(0) == 'n'
84 ? OsmPrimitiveType.NODE
85 : s.charAt(0) == 'w'
86 ? OsmPrimitiveType.WAY
87 : OsmPrimitiveType.RELATION);
88 } else {
89 throw new IllegalArgumentException("The string " + s + " does not match the pattern " + ID_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}.
96 * @return the parsed list of {@code OsmPrimitiveType}s.
97 */
98 public static List<SimplePrimitiveId> fuzzyParse(String s) {
99 final ArrayList<SimplePrimitiveId> ids = new ArrayList<>();
100 final Matcher m = ID_PATTERN.matcher(s);
101 while (m.find()) {
102 final char firstChar = s.charAt(m.start());
103 ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
104 firstChar == 'n'
105 ? OsmPrimitiveType.NODE
106 : firstChar == 'w'
107 ? OsmPrimitiveType.WAY
108 : OsmPrimitiveType.RELATION));
109 }
110 return ids;
111 }
112}
Note: See TracBrowser for help on using the repository browser.