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.text.MessageFormat;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.Date;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.Locale;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Objects;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
19 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
22 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
23 | import org.openstreetmap.josm.data.osm.User;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Represents an immutable OSM primitive in the context of a historical view on OSM data.
|
---|
29 | * @since 1670
|
---|
30 | */
|
---|
31 | public abstract class HistoryOsmPrimitive implements Tagged, Comparable<HistoryOsmPrimitive> {
|
---|
32 |
|
---|
33 | private long id;
|
---|
34 | private boolean visible;
|
---|
35 | private User user;
|
---|
36 | private long changesetId;
|
---|
37 | private Changeset changeset;
|
---|
38 | private Date timestamp;
|
---|
39 | private long version;
|
---|
40 | private Map<String, String> tags;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new {@code HistoryOsmPrimitive}.
|
---|
44 | *
|
---|
45 | * @param id the id (> 0 required)
|
---|
46 | * @param version the version (> 0 required)
|
---|
47 | * @param visible whether the primitive is still visible
|
---|
48 | * @param user the user (!= null required)
|
---|
49 | * @param changesetId the changeset id (> 0 required)
|
---|
50 | * @param timestamp the timestamp (!= null required)
|
---|
51 | *
|
---|
52 | * @throws IllegalArgumentException if preconditions are violated
|
---|
53 | */
|
---|
54 | public HistoryOsmPrimitive(long id, long version, boolean visible, User user, long changesetId, Date timestamp) {
|
---|
55 | this(id, version, visible, user, changesetId, timestamp, true);
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Constructs a new {@code HistoryOsmPrimitive} with a configurable checking of historic parameters.
|
---|
60 | * This is needed to build virtual HistoryOsmPrimitives for modified primitives, which do not have a timestamp and a changeset id.
|
---|
61 | *
|
---|
62 | * @param id the id (> 0 required)
|
---|
63 | * @param version the version (> 0 required)
|
---|
64 | * @param visible whether the primitive is still visible
|
---|
65 | * @param user the user (!= null required)
|
---|
66 | * @param changesetId the changeset id (> 0 required if {@code checkHistoricParams} is true)
|
---|
67 | * @param timestamp the timestamp (!= null required if {@code checkHistoricParams} is true)
|
---|
68 | * @param checkHistoricParams if true, checks values of {@code changesetId} and {@code timestamp}
|
---|
69 | *
|
---|
70 | * @throws IllegalArgumentException if preconditions are violated
|
---|
71 | * @since 5440
|
---|
72 | */
|
---|
73 | public HistoryOsmPrimitive(long id, long version, boolean visible, User user, long changesetId, Date timestamp,
|
---|
74 | boolean checkHistoricParams) {
|
---|
75 | ensurePositiveLong(id, "id");
|
---|
76 | ensurePositiveLong(version, "version");
|
---|
77 | CheckParameterUtil.ensureParameterNotNull(user, "user");
|
---|
78 | if (checkHistoricParams) {
|
---|
79 | ensurePositiveLong(changesetId, "changesetId");
|
---|
80 | CheckParameterUtil.ensureParameterNotNull(timestamp, "timestamp");
|
---|
81 | }
|
---|
82 | this.id = id;
|
---|
83 | this.version = version;
|
---|
84 | this.visible = visible;
|
---|
85 | this.user = user;
|
---|
86 | this.changesetId = changesetId;
|
---|
87 | this.timestamp = timestamp;
|
---|
88 | tags = new HashMap<>();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Constructs a new {@code HistoryOsmPrimitive} from an existing {@link OsmPrimitive}.
|
---|
93 | * @param p the primitive
|
---|
94 | */
|
---|
95 | public HistoryOsmPrimitive(OsmPrimitive p) {
|
---|
96 | this(p.getId(), p.getVersion(), p.isVisible(), p.getUser(), p.getChangesetId(), p.getTimestamp());
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Replies a new {@link HistoryNode}, {@link HistoryWay} or {@link HistoryRelation} from an existing {@link OsmPrimitive}.
|
---|
101 | * @param p the primitive
|
---|
102 | * @return a new {@code HistoryNode}, {@code HistoryWay} or {@code HistoryRelation} from {@code p}.
|
---|
103 | */
|
---|
104 | public static HistoryOsmPrimitive forOsmPrimitive(OsmPrimitive p) {
|
---|
105 | if (p instanceof Node) {
|
---|
106 | return new HistoryNode((Node) p);
|
---|
107 | } else if (p instanceof Way) {
|
---|
108 | return new HistoryWay((Way) p);
|
---|
109 | } else if (p instanceof Relation) {
|
---|
110 | return new HistoryRelation((Relation) p);
|
---|
111 | } else {
|
---|
112 | return null;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Returns the id.
|
---|
118 | * @return the id
|
---|
119 | */
|
---|
120 | public long getId() {
|
---|
121 | return id;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Returns the primitive id.
|
---|
126 | * @return the primitive id
|
---|
127 | */
|
---|
128 | public PrimitiveId getPrimitiveId() {
|
---|
129 | return new SimplePrimitiveId(id, getType());
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Determines if the primitive is still visible.
|
---|
134 | * @return {@code true} if the primitive is still visible
|
---|
135 | */
|
---|
136 | public boolean isVisible() {
|
---|
137 | return visible;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns the user.
|
---|
142 | * @return the user
|
---|
143 | */
|
---|
144 | public User getUser() {
|
---|
145 | return user;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Returns the changeset id.
|
---|
150 | * @return the changeset id
|
---|
151 | */
|
---|
152 | public long getChangesetId() {
|
---|
153 | return changesetId;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Returns the timestamp.
|
---|
158 | * @return the timestamp
|
---|
159 | */
|
---|
160 | public Date getTimestamp() {
|
---|
161 | return timestamp;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Returns the version.
|
---|
166 | * @return the version
|
---|
167 | */
|
---|
168 | public long getVersion() {
|
---|
169 | return version;
|
---|
170 | }
|
---|
171 |
|
---|
172 | protected final void ensurePositiveLong(long value, String name) {
|
---|
173 | if (value <= 0) {
|
---|
174 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", name, value));
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | public boolean matches(long id, long version) {
|
---|
179 | return this.id == id && this.version == version;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public boolean matches(long id) {
|
---|
183 | return this.id == id;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Returns the primitive type.
|
---|
188 | * @return the primitive type
|
---|
189 | */
|
---|
190 | public abstract OsmPrimitiveType getType();
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public int compareTo(HistoryOsmPrimitive o) {
|
---|
194 | if (this.id != o.id)
|
---|
195 | throw new ClassCastException(tr("Cannot compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));
|
---|
196 | return Long.compare(this.version, o.version);
|
---|
197 | }
|
---|
198 |
|
---|
199 | @Override
|
---|
200 | public final void put(String key, String value) {
|
---|
201 | tags.put(key, value);
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public final String get(String key) {
|
---|
206 | return tags.get(key);
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public final boolean hasKey(String key) {
|
---|
211 | return tags.containsKey(key);
|
---|
212 | }
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public final Map<String, String> getKeys() {
|
---|
216 | return getTags();
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public final void setKeys(Map<String, String> keys) {
|
---|
221 | throw new UnsupportedOperationException();
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public final void remove(String key) {
|
---|
226 | throw new UnsupportedOperationException();
|
---|
227 | }
|
---|
228 |
|
---|
229 | @Override
|
---|
230 | public final void removeAll() {
|
---|
231 | throw new UnsupportedOperationException();
|
---|
232 | }
|
---|
233 |
|
---|
234 | @Override
|
---|
235 | public final boolean hasKeys() {
|
---|
236 | return !tags.isEmpty();
|
---|
237 | }
|
---|
238 |
|
---|
239 | @Override
|
---|
240 | public final Collection<String> keySet() {
|
---|
241 | return Collections.unmodifiableSet(tags.keySet());
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Replies the key/value map.
|
---|
246 | * @return the key/value map
|
---|
247 | */
|
---|
248 | public Map<String, String> getTags() {
|
---|
249 | return Collections.unmodifiableMap(tags);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Returns the changeset for this history primitive.
|
---|
254 | * @return the changeset for this history primitive
|
---|
255 | */
|
---|
256 | public Changeset getChangeset() {
|
---|
257 | return changeset;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Sets the changeset for this history primitive.
|
---|
262 | * @param changeset the changeset for this history primitive
|
---|
263 | */
|
---|
264 | public void setChangeset(Changeset changeset) {
|
---|
265 | this.changeset = changeset;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Sets the tags for this history primitive. Removes all tags if <code>tags</code> is null.
|
---|
270 | *
|
---|
271 | * @param tags the tags. May be null.
|
---|
272 | */
|
---|
273 | public void setTags(Map<String, String> tags) {
|
---|
274 | if (tags == null) {
|
---|
275 | this.tags = new HashMap<>();
|
---|
276 | } else {
|
---|
277 | this.tags = new HashMap<>(tags);
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Replies the name of this primitive. The default implementation replies the value
|
---|
283 | * of the tag <tt>name</tt> or null, if this tag is not present.
|
---|
284 | *
|
---|
285 | * @return the name of this primitive
|
---|
286 | */
|
---|
287 | public String getName() {
|
---|
288 | if (get("name") != null)
|
---|
289 | return get("name");
|
---|
290 | return null;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
295 | * @param formatter The formatter used to generate a display name
|
---|
296 | *
|
---|
297 | * @return the display name
|
---|
298 | */
|
---|
299 | public abstract String getDisplayName(HistoryNameFormatter formatter);
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Replies the a localized name for this primitive given by the value of the tags (in this order)
|
---|
303 | * <ul>
|
---|
304 | * <li>name:lang_COUNTRY_Variant of the current locale</li>
|
---|
305 | * <li>name:lang_COUNTRY of the current locale</li>
|
---|
306 | * <li>name:lang of the current locale</li>
|
---|
307 | * <li>name of the current locale</li>
|
---|
308 | * </ul>
|
---|
309 | *
|
---|
310 | * null, if no such tag exists
|
---|
311 | *
|
---|
312 | * @return the name of this primitive
|
---|
313 | */
|
---|
314 | public String getLocalName() {
|
---|
315 | String key = "name:" + Locale.getDefault();
|
---|
316 | if (get(key) != null)
|
---|
317 | return get(key);
|
---|
318 | key = "name:" + Locale.getDefault().getLanguage() + '_' + Locale.getDefault().getCountry();
|
---|
319 | if (get(key) != null)
|
---|
320 | return get(key);
|
---|
321 | key = "name:" + Locale.getDefault().getLanguage();
|
---|
322 | if (get(key) != null)
|
---|
323 | return get(key);
|
---|
324 | return getName();
|
---|
325 | }
|
---|
326 |
|
---|
327 | @Override
|
---|
328 | public int hashCode() {
|
---|
329 | return Objects.hash(id, version);
|
---|
330 | }
|
---|
331 |
|
---|
332 | @Override
|
---|
333 | public boolean equals(Object obj) {
|
---|
334 | if (this == obj) return true;
|
---|
335 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
336 | HistoryOsmPrimitive that = (HistoryOsmPrimitive) obj;
|
---|
337 | return id == that.id &&
|
---|
338 | version == that.version;
|
---|
339 | }
|
---|
340 |
|
---|
341 | @Override
|
---|
342 | public String toString() {
|
---|
343 | return getClass().getSimpleName() + " [version=" + version + ", id=" + id + ", visible=" + visible + ", "
|
---|
344 | + (timestamp != null ? ("timestamp=" + timestamp) : "") + ", "
|
---|
345 | + (user != null ? ("user=" + user + ", ") : "") + "changesetId="
|
---|
346 | + changesetId
|
---|
347 | + ']';
|
---|
348 | }
|
---|
349 | }
|
---|