1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Date;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.HashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Locale;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.Set;
|
---|
18 | import java.util.Map.Entry;
|
---|
19 | import java.util.concurrent.atomic.AtomicLong;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.ElemStyle;
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * An OSM primitive can be associated with a key/value pair. It can be created, deleted
|
---|
28 | * and updated within the OSM-Server.
|
---|
29 | *
|
---|
30 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
31 | * it by any other than from the package {@link org.openstreetmap.josm.data.osm}. The available primitives are a fixed set that are given
|
---|
32 | * by the server environment and not an extendible data stuff.
|
---|
33 | *
|
---|
34 | * @author imi
|
---|
35 | */
|
---|
36 | abstract public class OsmPrimitive implements Comparable<OsmPrimitive>, Tagged {
|
---|
37 |
|
---|
38 | private static final AtomicLong idCounter = new AtomicLong(0);
|
---|
39 |
|
---|
40 | static long generateUniqueId() {
|
---|
41 | return idCounter.decrementAndGet();
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static final int FLAG_MODIFIED = 1 << 0;
|
---|
45 | private static final int FLAG_VISIBLE = 1 << 1;
|
---|
46 | private static final int FLAG_DISABLED = 1 << 2;
|
---|
47 | private static final int FLAG_DELETED = 1 << 3;
|
---|
48 | private static final int FLAG_FILTERED = 1 << 4;
|
---|
49 | private static final int FLAG_SELECTED = 1 << 5;
|
---|
50 | private static final int FLAG_HAS_DIRECTIONS = 1 << 6;
|
---|
51 | private static final int FLAG_TAGGED = 1 << 7;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
55 | * another collection of {@see OsmPrimitive}s. The result collection is a list.
|
---|
56 | *
|
---|
57 | * If <code>list</code> is null, replies an empty list.
|
---|
58 | *
|
---|
59 | * @param <T>
|
---|
60 | * @param list the original list
|
---|
61 | * @param type the type to filter for
|
---|
62 | * @return the sub-list of OSM primitives of type <code>type</code>
|
---|
63 | */
|
---|
64 | static public <T extends OsmPrimitive> List<T> getFilteredList(Collection<OsmPrimitive> list, Class<T> type) {
|
---|
65 | if (list == null) return Collections.emptyList();
|
---|
66 | List<T> ret = new LinkedList<T>();
|
---|
67 | for(OsmPrimitive p: list) {
|
---|
68 | if (type.isInstance(p)) {
|
---|
69 | ret.add(type.cast(p));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return ret;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
77 | * another collection of {@see OsmPrimitive}s. The result collection is a set.
|
---|
78 | *
|
---|
79 | * If <code>list</code> is null, replies an empty set.
|
---|
80 | *
|
---|
81 | * @param <T>
|
---|
82 | * @param list the original collection
|
---|
83 | * @param type the type to filter for
|
---|
84 | * @return the sub-set of OSM primitives of type <code>type</code>
|
---|
85 | */
|
---|
86 | static public <T extends OsmPrimitive> Set<T> getFilteredSet(Collection<OsmPrimitive> set, Class<T> type) {
|
---|
87 | if (set == null) return Collections.emptySet();
|
---|
88 | HashSet<T> ret = new HashSet<T>();
|
---|
89 | for(OsmPrimitive p: set) {
|
---|
90 | if (type.isInstance(p)) {
|
---|
91 | ret.add(type.cast(p));
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return ret;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /* mappaint data */
|
---|
99 | public ElemStyle mappaintStyle = null;
|
---|
100 | public Integer mappaintVisibleCode = 0;
|
---|
101 | public Integer mappaintDrawnCode = 0;
|
---|
102 | public Collection<String> errors;
|
---|
103 |
|
---|
104 | public void putError(String text, Boolean isError)
|
---|
105 | {
|
---|
106 | if(errors == null) {
|
---|
107 | errors = new ArrayList<String>();
|
---|
108 | }
|
---|
109 | String s = isError ? tr("Error: {0}", text) : tr("Warning: {0}", text);
|
---|
110 | errors.add(s);
|
---|
111 | }
|
---|
112 | public void clearErrors()
|
---|
113 | {
|
---|
114 | errors = null;
|
---|
115 | }
|
---|
116 | /* This should not be called from outside. Fixing the UI to add relevant
|
---|
117 | get/set functions calling this implicitely is preferred, so we can have
|
---|
118 | transparent cache handling in the future. */
|
---|
119 | protected void clearCached()
|
---|
120 | {
|
---|
121 | mappaintVisibleCode = 0;
|
---|
122 | mappaintDrawnCode = 0;
|
---|
123 | mappaintStyle = null;
|
---|
124 | }
|
---|
125 | /* end of mappaint data */
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Unique identifier in OSM. This is used to identify objects on the server.
|
---|
129 | * An id of 0 means an unknown id. The object has not been uploaded yet to
|
---|
130 | * know what id it will get.
|
---|
131 | *
|
---|
132 | */
|
---|
133 | private long id = 0;
|
---|
134 |
|
---|
135 | private volatile byte flags = FLAG_VISIBLE; // visible per default
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * User that last modified this primitive, as specified by the server.
|
---|
139 | * Never changed by JOSM.
|
---|
140 | */
|
---|
141 | private User user = null;
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * If set to true, this object is incomplete, which means only the id
|
---|
145 | * and type is known (type is the objects instance class)
|
---|
146 | */
|
---|
147 | public boolean incomplete = false;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Contains the version number as returned by the API. Needed to
|
---|
151 | * ensure update consistency
|
---|
152 | */
|
---|
153 | private int version = 0;
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Creates a new primitive for the given id. If the id > 0, the primitive is marked
|
---|
157 | * as incomplete.
|
---|
158 | *
|
---|
159 | * @param id the id. > 0 required
|
---|
160 | * @param allowNegativeId Allows to set negative id. For internal use
|
---|
161 | * @throws IllegalArgumentException thrown if id < 0 and allowNegativeId is false
|
---|
162 | */
|
---|
163 | protected OsmPrimitive(long id, boolean allowNegativeId) throws IllegalArgumentException {
|
---|
164 | if (allowNegativeId) {
|
---|
165 | this.id = id;
|
---|
166 | } else {
|
---|
167 | if (id < 0)
|
---|
168 | throw new IllegalArgumentException(tr("Expected ID >= 0. Got {0}.", id));
|
---|
169 | else if (id == 0) {
|
---|
170 | this.id = generateUniqueId();
|
---|
171 | } else {
|
---|
172 | this.id = id;
|
---|
173 | }
|
---|
174 |
|
---|
175 | }
|
---|
176 | this.version = 0;
|
---|
177 | this.incomplete = id > 0;
|
---|
178 | }
|
---|
179 |
|
---|
180 | protected OsmPrimitive(PrimitiveData data) {
|
---|
181 | version = data.getVersion();
|
---|
182 | id = data.getId();
|
---|
183 | }
|
---|
184 |
|
---|
185 | /* ------------------------------------------------------------------------------------ */
|
---|
186 | /* accessors */
|
---|
187 | /* ------------------------------------------------------------------------------------ */
|
---|
188 | /**
|
---|
189 | * Sets whether this primitive is disabled or not.
|
---|
190 | *
|
---|
191 | * @param selected true, if this primitive is disabled; false, otherwise
|
---|
192 | */
|
---|
193 | public void setDisabled(boolean disabled) {
|
---|
194 | if (disabled) {
|
---|
195 | flags |= FLAG_DISABLED;
|
---|
196 | } else {
|
---|
197 | flags &= ~FLAG_DISABLED;
|
---|
198 | }
|
---|
199 |
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Replies true, if this primitive is disabled.
|
---|
204 | *
|
---|
205 | * @return true, if this primitive is disabled
|
---|
206 | */
|
---|
207 | public boolean isDisabled() {
|
---|
208 | return (flags & FLAG_DISABLED) != 0;
|
---|
209 | }
|
---|
210 | /**
|
---|
211 | * Sets whether this primitive is filtered out or not.
|
---|
212 | *
|
---|
213 | * @param selected true, if this primitive is filtered out; false, otherwise
|
---|
214 | */
|
---|
215 | public void setFiltered(boolean filtered) {
|
---|
216 | if (filtered) {
|
---|
217 | flags |= FLAG_FILTERED;
|
---|
218 | } else {
|
---|
219 | flags &= ~FLAG_FILTERED;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | /**
|
---|
223 | * Replies true, if this primitive is filtered out.
|
---|
224 | *
|
---|
225 | * @return true, if this primitive is filtered out
|
---|
226 | */
|
---|
227 | public boolean isFiltered() {
|
---|
228 | return (flags & FLAG_FILTERED) != 0;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Sets whether this primitive is selected or not.
|
---|
233 | *
|
---|
234 | * @param selected true, if this primitive is selected; false, otherwise
|
---|
235 | * @since 1899
|
---|
236 | */
|
---|
237 | @Deprecated public void setSelected(boolean selected) {
|
---|
238 | if (selected) {
|
---|
239 | flags |= FLAG_SELECTED;
|
---|
240 | } else {
|
---|
241 | flags &= ~FLAG_SELECTED;
|
---|
242 | }
|
---|
243 | }
|
---|
244 | /**
|
---|
245 | * Replies true, if this primitive is selected.
|
---|
246 | *
|
---|
247 | * @return true, if this primitive is selected
|
---|
248 | * @since 1899
|
---|
249 | */
|
---|
250 | @Deprecated public boolean isSelected() {
|
---|
251 | return (flags & FLAG_SELECTED) != 0;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Marks this primitive as being modified.
|
---|
256 | *
|
---|
257 | * @param modified true, if this primitive is to be modified
|
---|
258 | */
|
---|
259 | public void setModified(boolean modified) {
|
---|
260 | if (modified) {
|
---|
261 | flags |= FLAG_MODIFIED;
|
---|
262 | } else {
|
---|
263 | flags &= ~FLAG_MODIFIED;
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Replies <code>true</code> if the object has been modified since it was loaded from
|
---|
269 | * the server. In this case, on next upload, this object will be updated.
|
---|
270 | *
|
---|
271 | * Deleted objects are deleted from the server. If the objects are added (id=0),
|
---|
272 | * the modified is ignored and the object is added to the server.
|
---|
273 | *
|
---|
274 | * @return <code>true</code> if the object has been modified since it was loaded from
|
---|
275 | * the server
|
---|
276 | */
|
---|
277 | public boolean isModified() {
|
---|
278 | return (flags & FLAG_MODIFIED) != 0;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * Replies <code>true</code>, if the object has been deleted.
|
---|
283 | *
|
---|
284 | * @return <code>true</code>, if the object has been deleted.
|
---|
285 | * @see #setDeleted(boolean)
|
---|
286 | */
|
---|
287 | public boolean isDeleted() {
|
---|
288 | return (flags & FLAG_DELETED) != 0;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Replies <code>true</code>, if the object is usable.
|
---|
293 | *
|
---|
294 | * @return <code>true</code>, if the object is unusable.
|
---|
295 | * @see #delete(boolean)
|
---|
296 | */
|
---|
297 | public boolean isUsable() {
|
---|
298 | return !isDeleted() && !incomplete && !isDisabled();
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Replies true if this primitive is either unknown to the server (i.e. its id
|
---|
303 | * is 0) or it is known to the server and it hasn't be deleted on the server.
|
---|
304 | * Replies false, if this primitive is known on the server and has been deleted
|
---|
305 | * on the server.
|
---|
306 | *
|
---|
307 | * @see #setVisible(boolean)
|
---|
308 | */
|
---|
309 | public boolean isVisible() {
|
---|
310 | return (flags & FLAG_VISIBLE) != 0;
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Sets whether this primitive is visible, i.e. whether it is known on the server
|
---|
315 | * and not deleted on the server.
|
---|
316 | *
|
---|
317 | * @see #isVisible()
|
---|
318 | * @throws IllegalStateException thrown if visible is set to false on an primitive with
|
---|
319 | * id==0
|
---|
320 | */
|
---|
321 | public void setVisible(boolean visible) throws IllegalStateException{
|
---|
322 | if (isNew() && visible == false)
|
---|
323 | throw new IllegalStateException(tr("A primitive with ID = 0 can't be invisible."));
|
---|
324 | if (visible) {
|
---|
325 | flags |= FLAG_VISIBLE;
|
---|
326 | } else {
|
---|
327 | flags &= ~FLAG_VISIBLE;
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Replies the version number as returned by the API. The version is 0 if the id is 0 or
|
---|
333 | * if this primitive is incomplete.
|
---|
334 | *
|
---|
335 | * @see #setVersion(int)
|
---|
336 | */
|
---|
337 | public long getVersion() {
|
---|
338 | return version;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Replies the id of this primitive.
|
---|
343 | *
|
---|
344 | * @return the id of this primitive.
|
---|
345 | */
|
---|
346 | public long getId() {
|
---|
347 | return id >= 0?id:0;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | *
|
---|
352 | * @return Osm id if primitive already exists on the server. Unique negative value if primitive is new
|
---|
353 | */
|
---|
354 | public long getUniqueId() {
|
---|
355 | return id;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | *
|
---|
360 | * @return True if primitive is new (not yet uploaded the server, id <= 0)
|
---|
361 | */
|
---|
362 | public boolean isNew() {
|
---|
363 | return id <= 0;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Sets the id and the version of this primitive if it is known to the OSM API.
|
---|
368 | *
|
---|
369 | * Since we know the id and its version it can't be incomplete anymore. incomplete
|
---|
370 | * is set to false.
|
---|
371 | *
|
---|
372 | * @param id the id. > 0 required
|
---|
373 | * @param version the version > 0 required
|
---|
374 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
375 | * @throws IllegalArgumentException thrown if version <= 0
|
---|
376 | */
|
---|
377 | public void setOsmId(long id, int version) {
|
---|
378 | if (id <= 0)
|
---|
379 | throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
|
---|
380 | if (version <= 0)
|
---|
381 | throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
|
---|
382 | this.id = id;
|
---|
383 | this.version = version;
|
---|
384 | this.incomplete = false;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Clears the id and version known to the OSM API. The id and the version is set to 0.
|
---|
389 | * incomplete is set to false.
|
---|
390 | *
|
---|
391 | * <strong>Caution</strong>: Do not use this method on primitives which are already added to a {@see DataSet}.
|
---|
392 | * Ways and relations might already refer to the primitive and clearing the OSM ID
|
---|
393 | * result in corrupt data.
|
---|
394 | *
|
---|
395 | * Here's an example use case:
|
---|
396 | * <pre>
|
---|
397 | * // create a clone of an already existing node
|
---|
398 | * Node copy = new Node(otherExistingNode);
|
---|
399 | *
|
---|
400 | * // reset the clones OSM id
|
---|
401 | * copy.clearOsmId();
|
---|
402 | * </pre>
|
---|
403 | *
|
---|
404 | */
|
---|
405 | public void clearOsmId() {
|
---|
406 | this.id = generateUniqueId();
|
---|
407 | this.version = 0;
|
---|
408 | this.incomplete = false;
|
---|
409 | }
|
---|
410 |
|
---|
411 | public void setTimestamp(Date timestamp) {
|
---|
412 | this.timestamp = (int)(timestamp.getTime() / 1000);
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Time of last modification to this object. This is not set by JOSM but
|
---|
417 | * read from the server and delivered back to the server unmodified. It is
|
---|
418 | * used to check against edit conflicts.
|
---|
419 | *
|
---|
420 | */
|
---|
421 | public Date getTimestamp() {
|
---|
422 | return new Date(timestamp * 1000l);
|
---|
423 | }
|
---|
424 |
|
---|
425 | public boolean isTimestampEmpty() {
|
---|
426 | return timestamp == 0;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * If set to true, this object is highlighted. Currently this is only used to
|
---|
431 | * show which ways/nodes will connect
|
---|
432 | */
|
---|
433 | public volatile boolean highlighted = false;
|
---|
434 |
|
---|
435 | private int timestamp;
|
---|
436 |
|
---|
437 | private static Collection<String> uninteresting = null;
|
---|
438 | /**
|
---|
439 | * Contains a list of "uninteresting" keys that do not make an object
|
---|
440 | * "tagged".
|
---|
441 | * Initialized by checkTagged()
|
---|
442 | */
|
---|
443 | public static Collection<String> getUninterestingKeys() {
|
---|
444 | if (uninteresting == null) {
|
---|
445 | uninteresting = Main.pref.getCollection("tags.uninteresting",
|
---|
446 | Arrays.asList(new String[]{"source","note","comment","converted_by","created_by"}));
|
---|
447 | }
|
---|
448 | return uninteresting;
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | private static Collection<String> directionKeys = null;
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Contains a list of direction-dependent keys that make an object
|
---|
456 | * direction dependent.
|
---|
457 | * Initialized by checkDirectionTagged()
|
---|
458 | */
|
---|
459 | public static Collection<String> getDirectionKeys() {
|
---|
460 | if(directionKeys == null) {
|
---|
461 | directionKeys = Main.pref.getCollection("tags.direction",
|
---|
462 | Arrays.asList("oneway","incline","incline_steep","aerialway"));
|
---|
463 | }
|
---|
464 | return directionKeys;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Implementation of the visitor scheme. Subclasses have to call the correct
|
---|
469 | * visitor function.
|
---|
470 | * @param visitor The visitor from which the visit() function must be called.
|
---|
471 | */
|
---|
472 | abstract public void visit(Visitor visitor);
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Sets whether this primitive is deleted or not.
|
---|
476 | *
|
---|
477 | * Also marks this primitive as modified if deleted is true and sets selected to false.
|
---|
478 | *
|
---|
479 | * @param deleted true, if this primitive is deleted; false, otherwise
|
---|
480 | */
|
---|
481 | public void setDeleted(boolean deleted) {
|
---|
482 | if (deleted) {
|
---|
483 | flags |= FLAG_DELETED;
|
---|
484 | } else {
|
---|
485 | flags &= ~FLAG_DELETED;
|
---|
486 | }
|
---|
487 | setModified(deleted);
|
---|
488 | setSelected(false);
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Replies the user who has last touched this object. May be null.
|
---|
493 | *
|
---|
494 | * @return the user who has last touched this object. May be null.
|
---|
495 | */
|
---|
496 | public User getUser() {
|
---|
497 | return user;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /**
|
---|
501 | * Sets the user who has last touched this object.
|
---|
502 | *
|
---|
503 | * @param user the user
|
---|
504 | */
|
---|
505 | public void setUser(User user) {
|
---|
506 | this.user = user;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Equal, if the id (and class) is equal.
|
---|
511 | *
|
---|
512 | * An primitive is equal to its incomplete counter part.
|
---|
513 | */
|
---|
514 | @Override public boolean equals(Object obj) {
|
---|
515 | if (isNew()) return obj == this;
|
---|
516 | if (obj instanceof OsmPrimitive)
|
---|
517 | return ((OsmPrimitive)obj).id == id && obj.getClass() == getClass();
|
---|
518 | return false;
|
---|
519 | }
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.
|
---|
523 | *
|
---|
524 | * An primitive has the same hashcode as its incomplete counterpart.
|
---|
525 | */
|
---|
526 | @Override public final int hashCode() {
|
---|
527 | if (id == 0)
|
---|
528 | return super.hashCode();
|
---|
529 | final int[] ret = new int[1];
|
---|
530 | Visitor v = new Visitor(){
|
---|
531 | public void visit(Node n) { ret[0] = 0; }
|
---|
532 | public void visit(Way w) { ret[0] = 1; }
|
---|
533 | public void visit(Relation e) { ret[0] = 2; }
|
---|
534 | public void visit(Changeset cs) { ret[0] = 3; }
|
---|
535 | };
|
---|
536 | visit(v);
|
---|
537 | return id == 0 ? super.hashCode() : (int)(id<<2)+ret[0];
|
---|
538 | }
|
---|
539 |
|
---|
540 | /*------------
|
---|
541 | * Keys handling
|
---|
542 | ------------*/
|
---|
543 |
|
---|
544 | /**
|
---|
545 | * The key/value list for this primitive.
|
---|
546 | *
|
---|
547 | */
|
---|
548 | private Map<String, String> keys;
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Replies the map of key/value pairs. Never replies null. The map can be empty, though.
|
---|
552 | *
|
---|
553 | * @return Keys of this primitive. Changes made in returned map are not mapped
|
---|
554 | * back to the primitive, use setKeys() to modify the keys
|
---|
555 | * @since 1924
|
---|
556 | */
|
---|
557 | public Map<String, String> getKeys() {
|
---|
558 | // TODO More effective map
|
---|
559 | // fix for #3218
|
---|
560 | if (keys == null)
|
---|
561 | return new HashMap<String, String>();
|
---|
562 | else
|
---|
563 | return new HashMap<String, String>(keys);
|
---|
564 | }
|
---|
565 |
|
---|
566 | /**
|
---|
567 | * Sets the keys of this primitives to the key/value pairs in <code>keys</code>.
|
---|
568 | * If <code>keys</code> is null removes all existing key/value pairs.
|
---|
569 | *
|
---|
570 | * @param keys the key/value pairs to set. If null, removes all existing key/value pairs.
|
---|
571 | * @since 1924
|
---|
572 | */
|
---|
573 | public void setKeys(Map<String, String> keys) {
|
---|
574 | if (keys == null) {
|
---|
575 | this.keys = null;
|
---|
576 | } else {
|
---|
577 | this.keys = new HashMap<String, String>(keys);
|
---|
578 | }
|
---|
579 | keysChangedImpl();
|
---|
580 | }
|
---|
581 |
|
---|
582 | /**
|
---|
583 | * Set the given value to the given key. If key is null, does nothing. If value is null,
|
---|
584 | * removes the key and behaves like {@see #remove(String)}.
|
---|
585 | *
|
---|
586 | * @param key The key, for which the value is to be set. Can be null, does nothing in this case.
|
---|
587 | * @param value The value for the key. If null, removes the respective key/value pair.
|
---|
588 | *
|
---|
589 | * @see #remove(String)
|
---|
590 | */
|
---|
591 | public final void put(String key, String value) {
|
---|
592 | if (key == null)
|
---|
593 | return;
|
---|
594 | else if (value == null) {
|
---|
595 | remove(key);
|
---|
596 | } else {
|
---|
597 | if (keys == null) {
|
---|
598 | keys = new HashMap<String, String>();
|
---|
599 | }
|
---|
600 | keys.put(key, value);
|
---|
601 | }
|
---|
602 | mappaintStyle = null;
|
---|
603 | keysChangedImpl();
|
---|
604 | }
|
---|
605 | /**
|
---|
606 | * Remove the given key from the list
|
---|
607 | *
|
---|
608 | * @param key the key to be removed. Ignored, if key is null.
|
---|
609 | */
|
---|
610 | public final void remove(String key) {
|
---|
611 | if (keys != null) {
|
---|
612 | keys.remove(key);
|
---|
613 | if (keys.isEmpty()) {
|
---|
614 | keys = null;
|
---|
615 | }
|
---|
616 | }
|
---|
617 | mappaintStyle = null;
|
---|
618 | keysChangedImpl();
|
---|
619 | }
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Removes all keys from this primitive.
|
---|
623 | *
|
---|
624 | * @since 1843
|
---|
625 | */
|
---|
626 | public final void removeAll() {
|
---|
627 | keys = null;
|
---|
628 | mappaintStyle = null;
|
---|
629 | keysChangedImpl();
|
---|
630 | }
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Replies the value for key <code>key</code>. Replies null, if <code>key</code> is null.
|
---|
634 | * Replies null, if there is no value for the given key.
|
---|
635 | *
|
---|
636 | * @param key the key. Can be null, replies null in this case.
|
---|
637 | * @return the value for key <code>key</code>.
|
---|
638 | */
|
---|
639 | public final String get(String key) {
|
---|
640 | if (key == null) return null;
|
---|
641 | return keys == null ? null : keys.get(key);
|
---|
642 | }
|
---|
643 |
|
---|
644 | public final Collection<Entry<String, String>> entrySet() {
|
---|
645 | if (keys == null)
|
---|
646 | return Collections.emptyList();
|
---|
647 | return keys.entrySet();
|
---|
648 | }
|
---|
649 |
|
---|
650 | public final Collection<String> keySet() {
|
---|
651 | if (keys == null)
|
---|
652 | return Collections.emptyList();
|
---|
653 | return keys.keySet();
|
---|
654 | }
|
---|
655 |
|
---|
656 | /**
|
---|
657 | * Replies true, if the map of key/value pairs of this primitive is not empty.
|
---|
658 | *
|
---|
659 | * @return true, if the map of key/value pairs of this primitive is not empty; false
|
---|
660 | * otherwise
|
---|
661 | *
|
---|
662 | * @since 1843
|
---|
663 | */
|
---|
664 | public final boolean hasKeys() {
|
---|
665 | return keys != null && !keys.isEmpty();
|
---|
666 | }
|
---|
667 |
|
---|
668 | private void keysChangedImpl() {
|
---|
669 | updateHasDirectionKeys();
|
---|
670 | updateTagged();
|
---|
671 | }
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
675 | * use this only in the data initializing phase
|
---|
676 | */
|
---|
677 | public void cloneFrom(OsmPrimitive osm) {
|
---|
678 | keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);
|
---|
679 | id = osm.id;
|
---|
680 | timestamp = osm.timestamp;
|
---|
681 | version = osm.version;
|
---|
682 | incomplete = osm.incomplete;
|
---|
683 | flags = osm.flags;
|
---|
684 | user= osm.user;
|
---|
685 | clearCached();
|
---|
686 | clearErrors();
|
---|
687 | }
|
---|
688 |
|
---|
689 | /**
|
---|
690 | * Replies true if this primitive and other are equal with respect to their
|
---|
691 | * semantic attributes.
|
---|
692 | * <ol>
|
---|
693 | * <li>equal id</ol>
|
---|
694 | * <li>both are complete or both are incomplete</li>
|
---|
695 | * <li>both have the same tags</li>
|
---|
696 | * </ol>
|
---|
697 | * @param other
|
---|
698 | * @return true if this primitive and other are equal with respect to their
|
---|
699 | * semantic attributes.
|
---|
700 | */
|
---|
701 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
702 | if (!isNew() && id != other.id)
|
---|
703 | return false;
|
---|
704 | if (incomplete && ! other.incomplete || !incomplete && other.incomplete)
|
---|
705 | return false;
|
---|
706 | return (keys == null ? other.keys==null : keys.equals(other.keys));
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * Replies true if this primitive and other are equal with respect to their
|
---|
711 | * technical attributes. The attributes:
|
---|
712 | * <ol>
|
---|
713 | * <li>deleted</ol>
|
---|
714 | * <li>modified</ol>
|
---|
715 | * <li>timestamp</ol>
|
---|
716 | * <li>version</ol>
|
---|
717 | * <li>visible</ol>
|
---|
718 | * <li>user</ol>
|
---|
719 | * </ol>
|
---|
720 | * have to be equal
|
---|
721 | * @param other the other primitive
|
---|
722 | * @return true if this primitive and other are equal with respect to their
|
---|
723 | * technical attributes
|
---|
724 | */
|
---|
725 | public boolean hasEqualTechnicalAttributes(OsmPrimitive other) {
|
---|
726 | if (other == null) return false;
|
---|
727 |
|
---|
728 | return
|
---|
729 | isDeleted() == other.isDeleted()
|
---|
730 | && isModified() == other.isModified()
|
---|
731 | && timestamp == other.timestamp
|
---|
732 | && version == other.version
|
---|
733 | && isVisible() == other.isVisible()
|
---|
734 | && (user == null ? other.user==null : user==other.user);
|
---|
735 | }
|
---|
736 |
|
---|
737 | private void updateTagged() {
|
---|
738 | getUninterestingKeys();
|
---|
739 | if (keys != null) {
|
---|
740 | for (Entry<String,String> e : keys.entrySet()) {
|
---|
741 | if (!uninteresting.contains(e.getKey())) {
|
---|
742 | flags |= FLAG_TAGGED;
|
---|
743 | return;
|
---|
744 | }
|
---|
745 | }
|
---|
746 | }
|
---|
747 | flags &= ~FLAG_TAGGED;
|
---|
748 | }
|
---|
749 |
|
---|
750 | /**
|
---|
751 | * true if this object is considered "tagged". To be "tagged", an object
|
---|
752 | * must have one or more "interesting" tags. "created_by" and "source"
|
---|
753 | * are typically considered "uninteresting" and do not make an object
|
---|
754 | * "tagged".
|
---|
755 | */
|
---|
756 | public boolean isTagged() {
|
---|
757 | return (flags & FLAG_TAGGED) != 0;
|
---|
758 | }
|
---|
759 |
|
---|
760 | private void updateHasDirectionKeys() {
|
---|
761 | getDirectionKeys();
|
---|
762 | if (keys != null) {
|
---|
763 | for (Entry<String,String> e : keys.entrySet()) {
|
---|
764 | if (directionKeys.contains(e.getKey())) {
|
---|
765 | flags |= FLAG_HAS_DIRECTIONS;
|
---|
766 | return;
|
---|
767 | }
|
---|
768 | }
|
---|
769 | }
|
---|
770 | flags &= ~FLAG_HAS_DIRECTIONS;
|
---|
771 | }
|
---|
772 | /**
|
---|
773 | * true if this object has direction dependent tags (e.g. oneway)
|
---|
774 | */
|
---|
775 | public boolean hasDirectionKeys() {
|
---|
776 | return (flags & FLAG_HAS_DIRECTIONS) != 0;
|
---|
777 | }
|
---|
778 |
|
---|
779 |
|
---|
780 | /**
|
---|
781 | * Replies the name of this primitive. The default implementation replies the value
|
---|
782 | * of the tag <tt>name</tt> or null, if this tag is not present.
|
---|
783 | *
|
---|
784 | * @return the name of this primitive
|
---|
785 | */
|
---|
786 | public String getName() {
|
---|
787 | if (get("name") != null)
|
---|
788 | return get("name");
|
---|
789 | return null;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /**
|
---|
793 | * Replies the a localized name for this primitive given by the value of the tags (in this order)
|
---|
794 | * <ul>
|
---|
795 | * <li>name:lang_COUNTRY_Variant of the current locale</li>
|
---|
796 | * <li>name:lang_COUNTRY of the current locale</li>
|
---|
797 | * <li>name:lang of the current locale</li>
|
---|
798 | * <li>name of the current locale</li>
|
---|
799 | * </ul>
|
---|
800 | *
|
---|
801 | * null, if no such tag exists
|
---|
802 | *
|
---|
803 | * @return the name of this primitive
|
---|
804 | */
|
---|
805 | public String getLocalName() {
|
---|
806 | String key = "name:" + Locale.getDefault().toString();
|
---|
807 | if (get(key) != null)
|
---|
808 | return get(key);
|
---|
809 | key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
|
---|
810 | if (get(key) != null)
|
---|
811 | return get(key);
|
---|
812 | key = "name:" + Locale.getDefault().getLanguage();
|
---|
813 | if (get(key) != null)
|
---|
814 | return get(key);
|
---|
815 | return getName();
|
---|
816 | }
|
---|
817 |
|
---|
818 | /**
|
---|
819 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
820 | *
|
---|
821 | * @return the display name
|
---|
822 | */
|
---|
823 | public abstract String getDisplayName(NameFormatter formatter);
|
---|
824 |
|
---|
825 | /**
|
---|
826 | * Loads (clone) this primitive from provided PrimitiveData
|
---|
827 | * @param data
|
---|
828 | * @param dataSet Dataset this primitive is part of. This parameter is used only
|
---|
829 | * temporarily. OsmPrimitive will have final field dataset in future
|
---|
830 | */
|
---|
831 | public void load(PrimitiveData data, DataSet dataSet) {
|
---|
832 | setKeys(data.getKeys());
|
---|
833 | timestamp = data.getTimestamp();
|
---|
834 | user = data.getUser();
|
---|
835 | setDeleted(data.isDeleted());
|
---|
836 | setModified(data.isModified());
|
---|
837 | setVisible(data.isVisible());
|
---|
838 | }
|
---|
839 |
|
---|
840 | /**
|
---|
841 | * Save parameters of this primitive to the transport object
|
---|
842 | * @return
|
---|
843 | */
|
---|
844 | public abstract PrimitiveData save();
|
---|
845 |
|
---|
846 | protected void saveCommonAttributes(PrimitiveData data) {
|
---|
847 | data.setId(id);
|
---|
848 | data.getKeys().clear();
|
---|
849 | data.getKeys().putAll(getKeys());
|
---|
850 | data.setTimestamp(timestamp);
|
---|
851 | data.setUser(user);
|
---|
852 | data.setDeleted(isDeleted());
|
---|
853 | data.setModified(isModified());
|
---|
854 | data.setVisible(isVisible());
|
---|
855 | }
|
---|
856 |
|
---|
857 | }
|
---|
858 |
|
---|