1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.history;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Date;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
12 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
13 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Represents an immutable OSM primitive in the context of a historical view on
|
---|
17 | * OSM data.
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public abstract class HistoryOsmPrimitive implements Comparable<HistoryOsmPrimitive> {
|
---|
21 |
|
---|
22 | private long id;
|
---|
23 | private boolean visible;
|
---|
24 | private String user;
|
---|
25 | private long uid;
|
---|
26 | private long changesetId;
|
---|
27 | private Date timestamp;
|
---|
28 | private long version;
|
---|
29 | private HashMap<String, String> tags;
|
---|
30 |
|
---|
31 | protected void ensurePositiveLong(long value, String name) {
|
---|
32 | if (value <= 0)
|
---|
33 | throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected. Got ''{1}''.", name, value));
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected void ensureNotNull(Object obj, String name) {
|
---|
37 | if (obj == null)
|
---|
38 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", name));
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * constructor
|
---|
43 | *
|
---|
44 | * @param id the id (>0 required)
|
---|
45 | * @param version the version (> 0 required)
|
---|
46 | * @param visible whether the primitive is still visible
|
---|
47 | * @param user the user (! null required)
|
---|
48 | * @param uid the user id (> 0 required)
|
---|
49 | * @param changesetId the changeset id (may be null if the changeset isn't known)
|
---|
50 | * @param timestamp the timestamp (! null required)
|
---|
51 | *
|
---|
52 | * @throws IllegalArgumentException thrown if preconditions are violated
|
---|
53 | */
|
---|
54 | public HistoryOsmPrimitive(long id, long version, boolean visible, String user, long uid, long changesetId, Date timestamp) throws IllegalArgumentException {
|
---|
55 | ensurePositiveLong(id, "id");
|
---|
56 | ensurePositiveLong(version, "version");
|
---|
57 | if(uid != -1) {
|
---|
58 | ensurePositiveLong(uid, "uid");
|
---|
59 | }
|
---|
60 | ensureNotNull(user, "user");
|
---|
61 | ensureNotNull(timestamp, "timestamp");
|
---|
62 | this.id = id;
|
---|
63 | this.version = version;
|
---|
64 | this.visible = visible;
|
---|
65 | this.user = user;
|
---|
66 | this.uid = uid;
|
---|
67 | // FIXME: restrict to IDs > 0 as soon as OsmPrimitive holds the
|
---|
68 | // changeset id too
|
---|
69 | this.changesetId = changesetId;
|
---|
70 | this.timestamp = timestamp;
|
---|
71 | tags = new HashMap<String, String>();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public long getId() {
|
---|
75 | return id;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public PrimitiveId getPrimitiveId() {
|
---|
79 | return new SimplePrimitiveId(id, getType());
|
---|
80 | }
|
---|
81 |
|
---|
82 | public boolean isVisible() {
|
---|
83 | return visible;
|
---|
84 | }
|
---|
85 | public String getUser() {
|
---|
86 | return user;
|
---|
87 | }
|
---|
88 | public long getUid() {
|
---|
89 | return uid;
|
---|
90 | }
|
---|
91 | public long getChangesetId() {
|
---|
92 | return changesetId;
|
---|
93 | }
|
---|
94 | public Date getTimestamp() {
|
---|
95 | return timestamp;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public long getVersion() {
|
---|
99 | return version;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public boolean matches(long id, long version) {
|
---|
103 | return this.id == id && this.version == version;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public boolean matches(long id) {
|
---|
107 | return this.id == id;
|
---|
108 | }
|
---|
109 |
|
---|
110 | public abstract OsmPrimitiveType getType();
|
---|
111 |
|
---|
112 | public int compareTo(HistoryOsmPrimitive o) {
|
---|
113 | if (this.id != o.id)
|
---|
114 | throw new ClassCastException(tr("Can''t compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
|
---|
115 | return Long.valueOf(this.version).compareTo(o.version);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void put(String key, String value) {
|
---|
119 | tags.put(key, value);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public String get(String key) {
|
---|
123 | return tags.get(key);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public boolean hasTag(String key) {
|
---|
127 | return tags.get(key) != null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public Map<String,String> getTags() {
|
---|
131 | return Collections.unmodifiableMap(tags);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Sets the tags for this history primitive. Removes all
|
---|
136 | * tags if <code>tags</code> is null.
|
---|
137 | *
|
---|
138 | * @param tags the tags. May be null.
|
---|
139 | */
|
---|
140 | public void setTags(Map<String,String> tags) {
|
---|
141 | if (tags == null) {
|
---|
142 | this.tags = new HashMap<String, String>();
|
---|
143 | } else {
|
---|
144 | this.tags = new HashMap<String, String>(tags);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public int hashCode() {
|
---|
150 | final int prime = 31;
|
---|
151 | int result = 1;
|
---|
152 | result = prime * result + (int) (id ^ (id >>> 32));
|
---|
153 | result = prime * result + (int) (version ^ (version >>> 32));
|
---|
154 | return result;
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override
|
---|
158 | public boolean equals(Object obj) {
|
---|
159 | if (this == obj)
|
---|
160 | return true;
|
---|
161 | if (!(obj instanceof HistoryOsmPrimitive))
|
---|
162 | return false;
|
---|
163 | // equal semantics is valid for subclasses like {@see HistoryOsmNode} etc. too.
|
---|
164 | // So, don't enforce equality of class.
|
---|
165 | //
|
---|
166 | // if (getClass() != obj.getClass())
|
---|
167 | // return false;
|
---|
168 | HistoryOsmPrimitive other = (HistoryOsmPrimitive) obj;
|
---|
169 | if (id != other.id)
|
---|
170 | return false;
|
---|
171 | if (version != other.version)
|
---|
172 | return false;
|
---|
173 | return true;
|
---|
174 | }
|
---|
175 | }
|
---|