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

Last change on this file since 7715 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

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