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