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

Revision 5170, 2.4 KB checked in by Don-vip, 6 weeks ago (diff)

cleanup svn:mime-type properties preventing Java sources from being viewed as such on Trac

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.Serializable;
5import java.util.regex.Matcher;
6import java.util.regex.Pattern;
7
8public class SimplePrimitiveId implements PrimitiveId, Serializable {
9    private final long id;
10    private final OsmPrimitiveType type;
11
12    public SimplePrimitiveId(long id, OsmPrimitiveType type) {
13        this.id = id;
14        this.type = type;
15    }
16
17    public OsmPrimitiveType getType() {
18        return type;
19    }
20
21    public long getUniqueId() {
22        return id;
23    }
24
25    public boolean isNew() {
26        return id <= 0;
27    }
28
29    @Override
30    public int hashCode() {
31        final int prime = 31;
32        int result = 1;
33        result = prime * result + (int) (id ^ (id >>> 32));
34        result = prime * result + ((type == null) ? 0 : type.hashCode());
35        return result;
36    }
37
38    @Override
39    public boolean equals(Object obj) {
40        if (this == obj)
41            return true;
42        if (obj == null)
43            return false;
44        if (getClass() != obj.getClass())
45            return false;
46        SimplePrimitiveId other = (SimplePrimitiveId) obj;
47        if (id != other.id)
48            return false;
49        if (type == null) {
50            if (other.type != null)
51                return false;
52        } else if (!type.equals(other.type))
53            return false;
54        return true;
55    }
56
57    @Override
58    public String toString() {
59        return type + " " + id;
60    }
61
62    /**
63     * Parses a {@code OsmPrimitiveType} 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 OsmPrimitiveType}
67     * @throws IllegalArgumentException if the string does not match the pattern
68     */
69    public static SimplePrimitiveId fromString(String s) {
70        final Pattern p = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)");
71        final Matcher m = p.matcher(s);
72        if (m.matches()) {
73            return new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())),
74                    s.charAt(0) == 'n' ? OsmPrimitiveType.NODE
75                    : s.charAt(0) == 'w' ? OsmPrimitiveType.WAY
76                    : OsmPrimitiveType.RELATION);
77        } else {
78            throw new IllegalArgumentException("The string " + s + " does not match the pattern " + p);
79        }
80    }
81}
Note: See TracBrowser for help on using the repository browser.