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.LinkedHashSet;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Locale;
|
---|
17 | import java.util.Map;
|
---|
18 | import java.util.Set;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 | import java.util.concurrent.atomic.AtomicLong;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.ElemStyle;
|
---|
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, PrimitiveId {
|
---|
37 |
|
---|
38 | private static final AtomicLong idCounter = new AtomicLong(0);
|
---|
39 |
|
---|
40 | static long generateUniqueId() {
|
---|
41 | return idCounter.decrementAndGet();
|
---|
42 | }
|
---|
43 |
|
---|
44 | private static class KeysEntry implements Entry<String, String>{
|
---|
45 |
|
---|
46 | private final String key;
|
---|
47 | private final String value;
|
---|
48 |
|
---|
49 | private KeysEntry(String key, String value) {
|
---|
50 | this.key = key;
|
---|
51 | this.value = value;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public String getKey() {
|
---|
55 | return key;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public String getValue() {
|
---|
59 | return value;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public String setValue(String value) {
|
---|
63 | throw new UnsupportedOperationException();
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public int hashCode() {
|
---|
68 | final int prime = 31;
|
---|
69 | int result = 1;
|
---|
70 | result = prime * result + ((key == null) ? 0 : key.hashCode());
|
---|
71 | result = prime * result + ((value == null) ? 0 : value.hashCode());
|
---|
72 | return result;
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public boolean equals(Object obj) {
|
---|
77 | if (this == obj)
|
---|
78 | return true;
|
---|
79 | if (obj == null)
|
---|
80 | return false;
|
---|
81 | if (getClass() != obj.getClass())
|
---|
82 | return false;
|
---|
83 | KeysEntry other = (KeysEntry) obj;
|
---|
84 | if (key == null) {
|
---|
85 | if (other.key != null)
|
---|
86 | return false;
|
---|
87 | } else if (!key.equals(other.key))
|
---|
88 | return false;
|
---|
89 | if (value == null) {
|
---|
90 | if (other.value != null)
|
---|
91 | return false;
|
---|
92 | } else if (!value.equals(other.value))
|
---|
93 | return false;
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static final int FLAG_MODIFIED = 1 << 0;
|
---|
99 | private static final int FLAG_VISIBLE = 1 << 1;
|
---|
100 | private static final int FLAG_DISABLED = 1 << 2;
|
---|
101 | private static final int FLAG_DELETED = 1 << 3;
|
---|
102 | private static final int FLAG_FILTERED = 1 << 4;
|
---|
103 | private static final int FLAG_HAS_DIRECTIONS = 1 << 5;
|
---|
104 | private static final int FLAG_TAGGED = 1 << 6;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
108 | * another collection of {@see OsmPrimitive}s. The result collection is a list.
|
---|
109 | *
|
---|
110 | * If <code>list</code> is null, replies an empty list.
|
---|
111 | *
|
---|
112 | * @param <T>
|
---|
113 | * @param list the original list
|
---|
114 | * @param type the type to filter for
|
---|
115 | * @return the sub-list of OSM primitives of type <code>type</code>
|
---|
116 | */
|
---|
117 | static public <T extends OsmPrimitive> List<T> getFilteredList(Collection<OsmPrimitive> list, Class<T> type) {
|
---|
118 | if (list == null) return Collections.emptyList();
|
---|
119 | List<T> ret = new LinkedList<T>();
|
---|
120 | for(OsmPrimitive p: list) {
|
---|
121 | if (type.isInstance(p)) {
|
---|
122 | ret.add(type.cast(p));
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return ret;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
130 | * another collection of {@see OsmPrimitive}s. The result collection is a set.
|
---|
131 | *
|
---|
132 | * If <code>list</code> is null, replies an empty set.
|
---|
133 | *
|
---|
134 | * @param <T>
|
---|
135 | * @param list the original collection
|
---|
136 | * @param type the type to filter for
|
---|
137 | * @return the sub-set of OSM primitives of type <code>type</code>
|
---|
138 | */
|
---|
139 | static public <T extends OsmPrimitive> LinkedHashSet<T> getFilteredSet(Collection<OsmPrimitive> set, Class<T> type) {
|
---|
140 | LinkedHashSet<T> ret = new LinkedHashSet<T>();
|
---|
141 | if (set != null) {
|
---|
142 | for(OsmPrimitive p: set) {
|
---|
143 | if (type.isInstance(p)) {
|
---|
144 | ret.add(type.cast(p));
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | return ret;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Replies the collection of referring primitives for the primitives in <code>primitives</code>.
|
---|
153 | *
|
---|
154 | * @param primitives the collection of primitives.
|
---|
155 | * @return the collection of referring primitives for the primitives in <code>primitives</code>;
|
---|
156 | * empty set if primitives is null or if there are no referring primitives
|
---|
157 | */
|
---|
158 | static public Set<OsmPrimitive> getReferrer(Collection<? extends OsmPrimitive> primitives) {
|
---|
159 | HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
|
---|
160 | if (primitives == null || primitives.isEmpty()) return ret;
|
---|
161 | for (OsmPrimitive p: primitives) {
|
---|
162 | ret.addAll(p.getReferrers());
|
---|
163 | }
|
---|
164 | return ret;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /* mappaint data */
|
---|
169 | public ElemStyle mappaintStyle = null;
|
---|
170 | public Integer mappaintDrawnCode = 0;
|
---|
171 |
|
---|
172 | /* This should not be called from outside. Fixing the UI to add relevant
|
---|
173 | get/set functions calling this implicitely is preferred, so we can have
|
---|
174 | transparent cache handling in the future. */
|
---|
175 | protected void clearCached()
|
---|
176 | {
|
---|
177 | mappaintDrawnCode = 0;
|
---|
178 | mappaintStyle = null;
|
---|
179 | }
|
---|
180 | /* end of mappaint data */
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Unique identifier in OSM. This is used to identify objects on the server.
|
---|
184 | * An id of 0 means an unknown id. The object has not been uploaded yet to
|
---|
185 | * know what id it will get.
|
---|
186 | *
|
---|
187 | */
|
---|
188 | private long id = 0;
|
---|
189 |
|
---|
190 | /** the parent dataset */
|
---|
191 | private DataSet dataSet;
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * This method should never ever by called from somewhere else than Dataset.addPrimitive or removePrimitive methods
|
---|
195 | * @param dataSet
|
---|
196 | */
|
---|
197 | void setDataset(DataSet dataSet) {
|
---|
198 | if (this.dataSet != null && dataSet != null && this.dataSet != dataSet)
|
---|
199 | throw new DataIntegrityProblemException("Primitive cannot be included in more than one Dataset");
|
---|
200 | this.dataSet = dataSet;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | *
|
---|
205 | * @return DataSet this primitive is part of.
|
---|
206 | */
|
---|
207 | public DataSet getDataSet() {
|
---|
208 | return dataSet;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Throws exception if primitive is not part of the dataset
|
---|
213 | */
|
---|
214 | public void checkDataset() {
|
---|
215 | if (dataSet == null)
|
---|
216 | throw new DataIntegrityProblemException("Primitive must be part of the dataset: " + toString());
|
---|
217 | }
|
---|
218 |
|
---|
219 | private volatile byte flags = FLAG_VISIBLE; // visible per default
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * User that last modified this primitive, as specified by the server.
|
---|
223 | * Never changed by JOSM.
|
---|
224 | */
|
---|
225 | private User user = null;
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * If set to true, this object is incomplete, which means only the id
|
---|
229 | * and type is known (type is the objects instance class)
|
---|
230 | */
|
---|
231 | private boolean incomplete = false;
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Contains the version number as returned by the API. Needed to
|
---|
235 | * ensure update consistency
|
---|
236 | */
|
---|
237 | private int version = 0;
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * The id of the changeset this primitive was last uploaded to.
|
---|
241 | * 0 if it wasn't uploaded to a changeset yet of if the changeset
|
---|
242 | * id isn't known.
|
---|
243 | */
|
---|
244 | private int changesetId;
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Creates a new primitive for the given id. If the id > 0, the primitive is marked
|
---|
248 | * as incomplete.
|
---|
249 | *
|
---|
250 | * @param id the id. > 0 required
|
---|
251 | * @param allowNegativeId Allows to set negative id. For internal use
|
---|
252 | * @throws IllegalArgumentException thrown if id < 0 and allowNegativeId is false
|
---|
253 | */
|
---|
254 | protected OsmPrimitive(long id, boolean allowNegativeId) throws IllegalArgumentException {
|
---|
255 | if (allowNegativeId) {
|
---|
256 | this.id = id;
|
---|
257 | } else {
|
---|
258 | if (id < 0)
|
---|
259 | throw new IllegalArgumentException(tr("Expected ID >= 0. Got {0}.", id));
|
---|
260 | else if (id == 0) {
|
---|
261 | this.id = generateUniqueId();
|
---|
262 | } else {
|
---|
263 | this.id = id;
|
---|
264 | }
|
---|
265 |
|
---|
266 | }
|
---|
267 | this.version = 0;
|
---|
268 | this.setIncomplete(id > 0);
|
---|
269 | }
|
---|
270 |
|
---|
271 | protected OsmPrimitive(PrimitiveData data) {
|
---|
272 | version = data.getVersion();
|
---|
273 | id = data.getId();
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* ------------------------------------------------------------------------------------ */
|
---|
277 | /* accessors */
|
---|
278 | /* ------------------------------------------------------------------------------------ */
|
---|
279 | /**
|
---|
280 | * Sets whether this primitive is disabled or not.
|
---|
281 | *
|
---|
282 | * @param disabled true, if this primitive is disabled; false, otherwise
|
---|
283 | */
|
---|
284 | void setDisabled(boolean disabled) {
|
---|
285 | if (disabled) {
|
---|
286 | flags |= FLAG_DISABLED;
|
---|
287 | } else {
|
---|
288 | flags &= ~FLAG_DISABLED;
|
---|
289 | }
|
---|
290 |
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Replies true, if this primitive is disabled.
|
---|
295 | *
|
---|
296 | * @return true, if this primitive is disabled
|
---|
297 | */
|
---|
298 | public boolean isDisabled() {
|
---|
299 | return (flags & FLAG_DISABLED) != 0;
|
---|
300 | }
|
---|
301 | /**
|
---|
302 | * Sets whether this primitive is filtered out or not.
|
---|
303 | *
|
---|
304 | * @param filtered true, if this primitive is filtered out; false, otherwise
|
---|
305 | */
|
---|
306 | public void setFiltered(boolean filtered) {
|
---|
307 | if (filtered) {
|
---|
308 | flags |= FLAG_FILTERED;
|
---|
309 | } else {
|
---|
310 | flags &= ~FLAG_FILTERED;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | /**
|
---|
314 | * Replies true, if this primitive is filtered out.
|
---|
315 | *
|
---|
316 | * @return true, if this primitive is filtered out
|
---|
317 | */
|
---|
318 | public boolean isFiltered() {
|
---|
319 | return (flags & FLAG_FILTERED) != 0;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Marks this primitive as being modified.
|
---|
324 | *
|
---|
325 | * @param modified true, if this primitive is to be modified
|
---|
326 | */
|
---|
327 | public void setModified(boolean modified) {
|
---|
328 | if (modified) {
|
---|
329 | flags |= FLAG_MODIFIED;
|
---|
330 | } else {
|
---|
331 | flags &= ~FLAG_MODIFIED;
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Replies <code>true</code> if the object has been modified since it was loaded from
|
---|
337 | * the server. In this case, on next upload, this object will be updated.
|
---|
338 | *
|
---|
339 | * Deleted objects are deleted from the server. If the objects are added (id=0),
|
---|
340 | * the modified is ignored and the object is added to the server.
|
---|
341 | *
|
---|
342 | * @return <code>true</code> if the object has been modified since it was loaded from
|
---|
343 | * the server
|
---|
344 | */
|
---|
345 | public boolean isModified() {
|
---|
346 | return (flags & FLAG_MODIFIED) != 0;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Replies <code>true</code>, if the object has been deleted.
|
---|
351 | *
|
---|
352 | * @return <code>true</code>, if the object has been deleted.
|
---|
353 | * @see #setDeleted(boolean)
|
---|
354 | */
|
---|
355 | public boolean isDeleted() {
|
---|
356 | return (flags & FLAG_DELETED) != 0;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Replies <code>true</code>, if the object is usable.
|
---|
361 | *
|
---|
362 | * @return <code>true</code>, if the object is unusable.
|
---|
363 | * @see #delete(boolean)
|
---|
364 | */
|
---|
365 | public boolean isUsable() {
|
---|
366 | return !isDeleted() && !isIncomplete() && !isDisabled();
|
---|
367 | }
|
---|
368 |
|
---|
369 | public boolean isDrawable()
|
---|
370 | {
|
---|
371 | return !isDeleted() && !isIncomplete() && !isFiltered();
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Replies true if this primitive is either unknown to the server (i.e. its id
|
---|
376 | * is 0) or it is known to the server and it hasn't be deleted on the server.
|
---|
377 | * Replies false, if this primitive is known on the server and has been deleted
|
---|
378 | * on the server.
|
---|
379 | *
|
---|
380 | * @see #setVisible(boolean)
|
---|
381 | */
|
---|
382 | public boolean isVisible() {
|
---|
383 | return (flags & FLAG_VISIBLE) != 0;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Sets whether this primitive is visible, i.e. whether it is known on the server
|
---|
388 | * and not deleted on the server.
|
---|
389 | *
|
---|
390 | * @see #isVisible()
|
---|
391 | * @throws IllegalStateException thrown if visible is set to false on an primitive with
|
---|
392 | * id==0
|
---|
393 | */
|
---|
394 | public void setVisible(boolean visible) throws IllegalStateException{
|
---|
395 | if (isNew() && visible == false)
|
---|
396 | throw new IllegalStateException(tr("A primitive with ID = 0 can't be invisible."));
|
---|
397 | if (visible) {
|
---|
398 | flags |= FLAG_VISIBLE;
|
---|
399 | } else {
|
---|
400 | flags &= ~FLAG_VISIBLE;
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | /**
|
---|
405 | * Replies the version number as returned by the API. The version is 0 if the id is 0 or
|
---|
406 | * if this primitive is incomplete.
|
---|
407 | *
|
---|
408 | * @see #setVersion(int)
|
---|
409 | */
|
---|
410 | public long getVersion() {
|
---|
411 | return version;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Replies the id of this primitive.
|
---|
416 | *
|
---|
417 | * @return the id of this primitive.
|
---|
418 | */
|
---|
419 | public long getId() {
|
---|
420 | return id >= 0?id:0;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /**
|
---|
424 | *
|
---|
425 | * @return Osm id if primitive already exists on the server. Unique negative value if primitive is new
|
---|
426 | */
|
---|
427 | public long getUniqueId() {
|
---|
428 | return id;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | *
|
---|
433 | * @return True if primitive is new (not yet uploaded the server, id <= 0)
|
---|
434 | */
|
---|
435 | public boolean isNew() {
|
---|
436 | return id <= 0;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /**
|
---|
440 | * Sets the id and the version of this primitive if it is known to the OSM API.
|
---|
441 | *
|
---|
442 | * Since we know the id and its version it can't be incomplete anymore. incomplete
|
---|
443 | * is set to false.
|
---|
444 | *
|
---|
445 | * @param id the id. > 0 required
|
---|
446 | * @param version the version > 0 required
|
---|
447 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
448 | * @throws IllegalArgumentException thrown if version <= 0
|
---|
449 | * @throws DataIntegrityProblemException If id is changed and primitive was already added to the dataset
|
---|
450 | */
|
---|
451 | public void setOsmId(long id, int version) {
|
---|
452 | if (id <= 0)
|
---|
453 | throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
|
---|
454 | if (version <= 0)
|
---|
455 | throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
|
---|
456 | if (dataSet != null && id != this.id) {
|
---|
457 | DataSet datasetCopy = dataSet;
|
---|
458 | // Reindex primitive
|
---|
459 | datasetCopy.removePrimitive(this);
|
---|
460 | this.id = id;
|
---|
461 | datasetCopy.addPrimitive(this);
|
---|
462 | }
|
---|
463 | this.id = id;
|
---|
464 | this.version = version;
|
---|
465 | this.setIncomplete(false);
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Clears the id and version known to the OSM API. The id and the version is set to 0.
|
---|
470 | * incomplete is set to false. It's preferred to use copy constructor with clearId set to true instead
|
---|
471 | * of calling this method.
|
---|
472 | *
|
---|
473 | * <strong>Caution</strong>: Do not use this method on primitives which are already added to a {@see DataSet}.
|
---|
474 | *
|
---|
475 | * @throws DataIntegrityProblemException If primitive was already added to the dataset
|
---|
476 | */
|
---|
477 | public void clearOsmId() {
|
---|
478 | if (dataSet != null)
|
---|
479 | throw new DataIntegrityProblemException("Method cannot be called after primitive was added to the dataset");
|
---|
480 | this.id = generateUniqueId();
|
---|
481 | this.version = 0;
|
---|
482 | this.setIncomplete(false);
|
---|
483 | }
|
---|
484 |
|
---|
485 | public void setTimestamp(Date timestamp) {
|
---|
486 | this.timestamp = (int)(timestamp.getTime() / 1000);
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Time of last modification to this object. This is not set by JOSM but
|
---|
491 | * read from the server and delivered back to the server unmodified. It is
|
---|
492 | * used to check against edit conflicts.
|
---|
493 | *
|
---|
494 | */
|
---|
495 | public Date getTimestamp() {
|
---|
496 | return new Date(timestamp * 1000l);
|
---|
497 | }
|
---|
498 |
|
---|
499 | public boolean isTimestampEmpty() {
|
---|
500 | return timestamp == 0;
|
---|
501 | }
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * If set to true, this object is highlighted. Currently this is only used to
|
---|
505 | * show which ways/nodes will connect
|
---|
506 | */
|
---|
507 | public volatile boolean highlighted = false;
|
---|
508 |
|
---|
509 | private int timestamp;
|
---|
510 |
|
---|
511 | private static Collection<String> uninteresting = null;
|
---|
512 | /**
|
---|
513 | * Contains a list of "uninteresting" keys that do not make an object
|
---|
514 | * "tagged".
|
---|
515 | * Initialized by checkTagged()
|
---|
516 | */
|
---|
517 | public static Collection<String> getUninterestingKeys() {
|
---|
518 | if (uninteresting == null) {
|
---|
519 | uninteresting = Main.pref.getCollection("tags.uninteresting",
|
---|
520 | Arrays.asList(new String[]{"source","note","comment","converted_by","created_by"}));
|
---|
521 | }
|
---|
522 | return uninteresting;
|
---|
523 | }
|
---|
524 |
|
---|
525 | private static Collection<String> directionKeys = null;
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Contains a list of direction-dependent keys that make an object
|
---|
529 | * direction dependent.
|
---|
530 | * Initialized by checkDirectionTagged()
|
---|
531 | */
|
---|
532 | public static Collection<String> getDirectionKeys() {
|
---|
533 | if(directionKeys == null) {
|
---|
534 | directionKeys = Main.pref.getCollection("tags.direction",
|
---|
535 | Arrays.asList("oneway","incline","incline_steep","aerialway"));
|
---|
536 | }
|
---|
537 | return directionKeys;
|
---|
538 | }
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * Implementation of the visitor scheme. Subclasses have to call the correct
|
---|
542 | * visitor function.
|
---|
543 | * @param visitor The visitor from which the visit() function must be called.
|
---|
544 | */
|
---|
545 | abstract public void visit(Visitor visitor);
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Sets whether this primitive is deleted or not.
|
---|
549 | *
|
---|
550 | * Also marks this primitive as modified if deleted is true.
|
---|
551 | *
|
---|
552 | * @param deleted true, if this primitive is deleted; false, otherwise
|
---|
553 | */
|
---|
554 | public void setDeleted(boolean deleted) {
|
---|
555 | if (deleted) {
|
---|
556 | flags |= FLAG_DELETED;
|
---|
557 | } else {
|
---|
558 | flags &= ~FLAG_DELETED;
|
---|
559 | }
|
---|
560 | setModified(deleted);
|
---|
561 | if (dataSet != null) {
|
---|
562 | if (deleted) {
|
---|
563 | dataSet.firePrimitivesRemoved(Collections.singleton(this));
|
---|
564 | } else {
|
---|
565 | dataSet.firePrimitivesAdded(Collections.singleton(this));
|
---|
566 | }
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Replies the user who has last touched this object. May be null.
|
---|
572 | *
|
---|
573 | * @return the user who has last touched this object. May be null.
|
---|
574 | */
|
---|
575 | public User getUser() {
|
---|
576 | return user;
|
---|
577 | }
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Sets the user who has last touched this object.
|
---|
581 | *
|
---|
582 | * @param user the user
|
---|
583 | */
|
---|
584 | public void setUser(User user) {
|
---|
585 | this.user = user;
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Replies the id of the changeset this primitive was last uploaded to.
|
---|
591 | * 0 if this primitive wasn't uploaded to a changeset yet or if the
|
---|
592 | * changeset isn't known.
|
---|
593 | *
|
---|
594 | * @return the id of the changeset this primitive was last uploaded to.
|
---|
595 | */
|
---|
596 | public int getChangesetId() {
|
---|
597 | return changesetId;
|
---|
598 | }
|
---|
599 |
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * Sets the changeset id of this primitive. Can't be set on a new
|
---|
603 | * primitive.
|
---|
604 | *
|
---|
605 | * @param changesetId the id. >= 0 required.
|
---|
606 | * @throws IllegalStateException thrown if this primitive is new.
|
---|
607 | * @throws IllegalArgumentException thrown if id < 0
|
---|
608 | */
|
---|
609 | public void setChangesetId(int changesetId) throws IllegalStateException, IllegalArgumentException {
|
---|
610 | if (changesetId < 0)
|
---|
611 | throw new IllegalArgumentException(tr("Parameter ''{0}'' >= 0 expected, got {1}", "changesetId", changesetId));
|
---|
612 | if (isNew() && changesetId > 0)
|
---|
613 | throw new IllegalStateException(tr("Can''t assign a changesetId > 0 to a new primitive. Value of changesetId is {0}", changesetId));
|
---|
614 | this.changesetId = changesetId;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /**
|
---|
618 | * Equal, if the id (and class) is equal.
|
---|
619 | *
|
---|
620 | * An primitive is equal to its incomplete counter part.
|
---|
621 | */
|
---|
622 | @Override public boolean equals(Object obj) {
|
---|
623 | if (obj instanceof OsmPrimitive)
|
---|
624 | return ((OsmPrimitive)obj).id == id && obj.getClass() == getClass();
|
---|
625 | return false;
|
---|
626 | }
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.
|
---|
630 | *
|
---|
631 | * An primitive has the same hashcode as its incomplete counterpart.
|
---|
632 | */
|
---|
633 | @Override public final int hashCode() {
|
---|
634 | final int[] ret = new int[1];
|
---|
635 | Visitor v = new Visitor(){
|
---|
636 | public void visit(Node n) { ret[0] = 0; }
|
---|
637 | public void visit(Way w) { ret[0] = 1; }
|
---|
638 | public void visit(Relation e) { ret[0] = 2; }
|
---|
639 | public void visit(Changeset cs) { ret[0] = 3; }
|
---|
640 | };
|
---|
641 | visit(v);
|
---|
642 | return (int)(id<<2)+ret[0];
|
---|
643 | }
|
---|
644 |
|
---|
645 | /*------------
|
---|
646 | * Keys handling
|
---|
647 | ------------*/
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * The key/value list for this primitive.
|
---|
651 | *
|
---|
652 | */
|
---|
653 | private String[] keys;
|
---|
654 |
|
---|
655 | /**
|
---|
656 | * Replies the map of key/value pairs. Never replies null. The map can be empty, though.
|
---|
657 | *
|
---|
658 | * @return tags of this primitive. Changes made in returned map are not mapped
|
---|
659 | * back to the primitive, use setKeys() to modify the keys
|
---|
660 | * @since 1924
|
---|
661 | */
|
---|
662 | public Map<String, String> getKeys() {
|
---|
663 | Map<String, String> result = new HashMap<String, String>();
|
---|
664 | if (keys != null) {
|
---|
665 | for (int i=0; i<keys.length ; i+=2) {
|
---|
666 | result.put(keys[i], keys[i + 1]);
|
---|
667 | }
|
---|
668 | }
|
---|
669 | return result;
|
---|
670 | }
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Sets the keys of this primitives to the key/value pairs in <code>keys</code>.
|
---|
674 | * If <code>keys</code> is null removes all existing key/value pairs.
|
---|
675 | *
|
---|
676 | * @param keys the key/value pairs to set. If null, removes all existing key/value pairs.
|
---|
677 | * @since 1924
|
---|
678 | */
|
---|
679 | public void setKeys(Map<String, String> keys) {
|
---|
680 | if (keys == null) {
|
---|
681 | this.keys = null;
|
---|
682 | return;
|
---|
683 | }
|
---|
684 | String[] newKeys = new String[keys.size() * 2];
|
---|
685 | int index = 0;
|
---|
686 | for (Entry<String, String> entry:keys.entrySet()) {
|
---|
687 | newKeys[index++] = entry.getKey();
|
---|
688 | newKeys[index++] = entry.getValue();
|
---|
689 | }
|
---|
690 | this.keys = newKeys;
|
---|
691 | keysChangedImpl();
|
---|
692 | }
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * Set the given value to the given key. If key is null, does nothing. If value is null,
|
---|
696 | * removes the key and behaves like {@see #remove(String)}.
|
---|
697 | *
|
---|
698 | * @param key The key, for which the value is to be set. Can be null, does nothing in this case.
|
---|
699 | * @param value The value for the key. If null, removes the respective key/value pair.
|
---|
700 | *
|
---|
701 | * @see #remove(String)
|
---|
702 | */
|
---|
703 | public final void put(String key, String value) {
|
---|
704 | if (key == null)
|
---|
705 | return;
|
---|
706 | else if (value == null) {
|
---|
707 | remove(key);
|
---|
708 | } else if (keys == null || keys.length == 0){
|
---|
709 | keys = new String[] {key, value};
|
---|
710 | keysChangedImpl();
|
---|
711 | } else {
|
---|
712 | for (int i=0; i<keys.length;i+=2) {
|
---|
713 | if (keys[i].equals(key)) {
|
---|
714 | keys[i+1] = value;
|
---|
715 | keysChangedImpl();
|
---|
716 | return;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | String[] newKeys = new String[keys.length + 2];
|
---|
720 | for (int i=0; i< keys.length;i+=2) {
|
---|
721 | newKeys[i] = keys[i];
|
---|
722 | newKeys[i+1] = keys[i+1];
|
---|
723 | }
|
---|
724 | newKeys[keys.length] = key;
|
---|
725 | newKeys[keys.length + 1] = value;
|
---|
726 | keys = newKeys;
|
---|
727 | keysChangedImpl();
|
---|
728 | }
|
---|
729 | }
|
---|
730 | /**
|
---|
731 | * Remove the given key from the list
|
---|
732 | *
|
---|
733 | * @param key the key to be removed. Ignored, if key is null.
|
---|
734 | */
|
---|
735 | public final void remove(String key) {
|
---|
736 | if (key == null || keys == null || keys.length == 0 ) return;
|
---|
737 | if (!hasKey(key))
|
---|
738 | return;
|
---|
739 | if (keys.length == 2) {
|
---|
740 | keys = null;
|
---|
741 | keysChangedImpl();
|
---|
742 | return;
|
---|
743 | }
|
---|
744 | String[] newKeys = new String[keys.length - 2];
|
---|
745 | int j=0;
|
---|
746 | for (int i=0; i < keys.length; i+=2) {
|
---|
747 | if (!keys[i].equals(key)) {
|
---|
748 | newKeys[j++] = keys[i];
|
---|
749 | newKeys[j++] = keys[i+1];
|
---|
750 | }
|
---|
751 | }
|
---|
752 | keys = newKeys;
|
---|
753 | keysChangedImpl();
|
---|
754 | }
|
---|
755 |
|
---|
756 | /**
|
---|
757 | * Removes all keys from this primitive.
|
---|
758 | *
|
---|
759 | * @since 1843
|
---|
760 | */
|
---|
761 | public final void removeAll() {
|
---|
762 | keys = null;
|
---|
763 | keysChangedImpl();
|
---|
764 | }
|
---|
765 |
|
---|
766 | /**
|
---|
767 | * Replies the value for key <code>key</code>. Replies null, if <code>key</code> is null.
|
---|
768 | * Replies null, if there is no value for the given key.
|
---|
769 | *
|
---|
770 | * @param key the key. Can be null, replies null in this case.
|
---|
771 | * @return the value for key <code>key</code>.
|
---|
772 | */
|
---|
773 | public final String get(String key) {
|
---|
774 | if (key == null)
|
---|
775 | return null;
|
---|
776 | if (keys == null || keys.length == 0)
|
---|
777 | return null;
|
---|
778 | for (int i=0; i<keys.length;i+=2) {
|
---|
779 | if (keys[i].equals(key)) return keys[i+1];
|
---|
780 | }
|
---|
781 | return null;
|
---|
782 | }
|
---|
783 |
|
---|
784 | // FIXME: why replying a collection of entries? Should be replaced by
|
---|
785 | // a method Map<String,String> getTags()
|
---|
786 | //
|
---|
787 | public final Collection<Entry<String, String>> entrySet() {
|
---|
788 | if (keys == null || keys.length ==0)
|
---|
789 | return Collections.emptySet();
|
---|
790 | Set<Entry<String, String>> result = new HashSet<Entry<String,String>>();
|
---|
791 | for (int i=0; i<keys.length; i+=2) {
|
---|
792 | result.add(new KeysEntry(keys[i], keys[i+1]));
|
---|
793 | }
|
---|
794 | return result;
|
---|
795 | }
|
---|
796 |
|
---|
797 | public final Collection<String> keySet() {
|
---|
798 | if (keys == null || keys.length == 0)
|
---|
799 | return Collections.emptySet();
|
---|
800 | Set<String> result = new HashSet<String>(keys.length / 2);
|
---|
801 | for (int i=0; i<keys.length; i+=2) {
|
---|
802 | result.add(keys[i]);
|
---|
803 | }
|
---|
804 | return result;
|
---|
805 | }
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Replies true, if the map of key/value pairs of this primitive is not empty.
|
---|
809 | *
|
---|
810 | * @return true, if the map of key/value pairs of this primitive is not empty; false
|
---|
811 | * otherwise
|
---|
812 | */
|
---|
813 | public final boolean hasKeys() {
|
---|
814 | return keys != null && keys.length != 0;
|
---|
815 | }
|
---|
816 |
|
---|
817 | private void keysChangedImpl() {
|
---|
818 | clearCached();
|
---|
819 | updateHasDirectionKeys();
|
---|
820 | updateTagged();
|
---|
821 | if (dataSet != null) {
|
---|
822 | dataSet.fireTagsChanged(this);
|
---|
823 | }
|
---|
824 | }
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * Replies true if this primitive has a tag with key <code>key</code>
|
---|
828 | *
|
---|
829 | * @param key the key
|
---|
830 | * @return true, if his primitive has a tag with key <code>key</code>
|
---|
831 | */
|
---|
832 | public boolean hasKey(String key) {
|
---|
833 | if (key == null) return false;
|
---|
834 | if (keys == null) return false;
|
---|
835 | for (int i=0; i< keys.length;i+=2) {
|
---|
836 | if (keys[i].equals(key)) return true;
|
---|
837 | }
|
---|
838 | return false;
|
---|
839 | }
|
---|
840 |
|
---|
841 | /**
|
---|
842 | * Replies true if other isn't null and has the same tags (key/value-pairs) as this.
|
---|
843 | *
|
---|
844 | * @param other the other object primitive
|
---|
845 | * @return true if other isn't null and has the same tags (key/value-pairs) as this.
|
---|
846 | */
|
---|
847 | public boolean hasSameTags(OsmPrimitive other) {
|
---|
848 | return entrySet().equals(other.entrySet());
|
---|
849 | }
|
---|
850 |
|
---|
851 | /*------------
|
---|
852 | * Referrers
|
---|
853 | ------------*/
|
---|
854 |
|
---|
855 | private Object referrers;
|
---|
856 |
|
---|
857 | /**
|
---|
858 | * Add new referrer. If referrer is already included then no action is taken
|
---|
859 | * @param referrer
|
---|
860 | */
|
---|
861 | protected void addReferrer(OsmPrimitive referrer) {
|
---|
862 | // Based on methods from josm-ng
|
---|
863 | if (referrers == null) {
|
---|
864 | referrers = referrer;
|
---|
865 | } else if (referrers instanceof OsmPrimitive) {
|
---|
866 | if (referrers != referrer) {
|
---|
867 | referrers = new OsmPrimitive[] { (OsmPrimitive)referrers, referrer };
|
---|
868 | }
|
---|
869 | } else {
|
---|
870 | for (OsmPrimitive primitive:(OsmPrimitive[])referrers) {
|
---|
871 | if (primitive == referrer)
|
---|
872 | return;
|
---|
873 | }
|
---|
874 | OsmPrimitive[] orig = (OsmPrimitive[])referrers;
|
---|
875 | OsmPrimitive[] bigger = new OsmPrimitive[orig.length+1];
|
---|
876 | System.arraycopy(orig, 0, bigger, 0, orig.length);
|
---|
877 | bigger[orig.length] = referrer;
|
---|
878 | referrers = bigger;
|
---|
879 | }
|
---|
880 | }
|
---|
881 |
|
---|
882 | /**
|
---|
883 | * Remove referrer. No action is taken if referrer is not registered
|
---|
884 | * @param referrer
|
---|
885 | */
|
---|
886 | protected void removeReferrer(OsmPrimitive referrer) {
|
---|
887 | // Based on methods from josm-ng
|
---|
888 | if (referrers instanceof OsmPrimitive) {
|
---|
889 | if (referrers == referrer) {
|
---|
890 | referrers = null;
|
---|
891 | }
|
---|
892 | } else if (referrers instanceof OsmPrimitive[]) {
|
---|
893 | OsmPrimitive[] orig = (OsmPrimitive[])referrers;
|
---|
894 | int idx = -1;
|
---|
895 | for (int i=0; i<orig.length; i++) {
|
---|
896 | if (orig[i] == referrer) {
|
---|
897 | idx = i;
|
---|
898 | break;
|
---|
899 | }
|
---|
900 | }
|
---|
901 | if (idx == -1)
|
---|
902 | return;
|
---|
903 |
|
---|
904 | if (orig.length == 2) {
|
---|
905 | referrers = orig[1-idx]; // idx is either 0 or 1, take the other
|
---|
906 | } else { // downsize the array
|
---|
907 | OsmPrimitive[] smaller = new OsmPrimitive[orig.length-1];
|
---|
908 | System.arraycopy(orig, 0, smaller, 0, idx);
|
---|
909 | System.arraycopy(orig, idx+1, smaller, idx, smaller.length-idx);
|
---|
910 | referrers = smaller;
|
---|
911 | }
|
---|
912 | }
|
---|
913 | }
|
---|
914 | /**
|
---|
915 | * Find primitives that reference this primitive. Returns only primitives that are included in the same
|
---|
916 | * dataset as this primitive. <br>
|
---|
917 | *
|
---|
918 | * For example following code will add wnew as referer to all nodes of existingWay, but this method will
|
---|
919 | * not return wnew because it's not part of the dataset <br>
|
---|
920 | *
|
---|
921 | * <code>Way wnew = new Way(existingWay)</code>
|
---|
922 | *
|
---|
923 | * @return a collection of all primitives that reference this primitive.
|
---|
924 | */
|
---|
925 |
|
---|
926 | public final List<OsmPrimitive> getReferrers() {
|
---|
927 | checkDataset();
|
---|
928 | // Method copied from OsmPrimitive in josm-ng
|
---|
929 | // Returns only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
930 | // when way is cloned
|
---|
931 | List<OsmPrimitive> result = new ArrayList<OsmPrimitive>();
|
---|
932 | if (referrers != null) {
|
---|
933 | if (referrers instanceof OsmPrimitive) {
|
---|
934 | OsmPrimitive ref = (OsmPrimitive)referrers;
|
---|
935 | if (ref.dataSet == dataSet) {
|
---|
936 | result.add(ref);
|
---|
937 | }
|
---|
938 | } else {
|
---|
939 | for (OsmPrimitive o:(OsmPrimitive[])referrers) {
|
---|
940 | if (dataSet == o.dataSet) {
|
---|
941 | result.add(o);
|
---|
942 | }
|
---|
943 | }
|
---|
944 | }
|
---|
945 | }
|
---|
946 |
|
---|
947 | return result;
|
---|
948 | }
|
---|
949 |
|
---|
950 | /**
|
---|
951 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
952 | * use this only in the data initializing phase
|
---|
953 | */
|
---|
954 | public void cloneFrom(OsmPrimitive other) {
|
---|
955 | setKeys(other.getKeys());
|
---|
956 | id = other.id;
|
---|
957 | timestamp = other.timestamp;
|
---|
958 | version = other.version;
|
---|
959 | setIncomplete(other.isIncomplete());
|
---|
960 | flags = other.flags;
|
---|
961 | user= other.user;
|
---|
962 | changesetId = other.changesetId;
|
---|
963 | clearCached();
|
---|
964 | }
|
---|
965 |
|
---|
966 | /**
|
---|
967 | * Merges the technical and semantical attributes from <code>other</code> onto this.
|
---|
968 | *
|
---|
969 | * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
|
---|
970 | * have an assigend OSM id, the IDs have to be the same.
|
---|
971 | *
|
---|
972 | * @param other the other primitive. Must not be null.
|
---|
973 | * @throws IllegalArgumentException thrown if other is null.
|
---|
974 | * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not
|
---|
975 | * @throws DataIntegrityProblemException thrown if other isn't new and other.getId() != this.getId()
|
---|
976 | */
|
---|
977 | public void mergeFrom(OsmPrimitive other) {
|
---|
978 | if (other == null)
|
---|
979 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "other"));
|
---|
980 | if (other.isNew() ^ isNew())
|
---|
981 | throw new DataIntegrityProblemException(tr("Can''t merge because either of the participating primitives is new and the other is not"));
|
---|
982 | if (! other.isNew() && other.getId() != id)
|
---|
983 | throw new DataIntegrityProblemException(tr("Can''t merge primitives with different ids. This id is {0}, the other is {1}", id, other.getId()));
|
---|
984 | setKeys(other.getKeys());
|
---|
985 | timestamp = other.timestamp;
|
---|
986 | version = other.version;
|
---|
987 | setIncomplete(other.isIncomplete());
|
---|
988 | flags = other.flags;
|
---|
989 | user= other.user;
|
---|
990 | changesetId = other.changesetId;
|
---|
991 | }
|
---|
992 |
|
---|
993 | /**
|
---|
994 | * Replies true if this primitive and other are equal with respect to their
|
---|
995 | * semantic attributes.
|
---|
996 | * <ol>
|
---|
997 | * <li>equal id</ol>
|
---|
998 | * <li>both are complete or both are incomplete</li>
|
---|
999 | * <li>both have the same tags</li>
|
---|
1000 | * </ol>
|
---|
1001 | * @param other
|
---|
1002 | * @return true if this primitive and other are equal with respect to their
|
---|
1003 | * semantic attributes.
|
---|
1004 | */
|
---|
1005 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
1006 | if (!isNew() && id != other.id)
|
---|
1007 | return false;
|
---|
1008 | if (isIncomplete() && ! other.isIncomplete() || !isIncomplete() && other.isIncomplete())
|
---|
1009 | return false;
|
---|
1010 | // can't do an equals check on the internal keys array because it is not ordered
|
---|
1011 | //
|
---|
1012 | return hasSameTags(other);
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | /**
|
---|
1016 | * Replies true if this primitive and other are equal with respect to their
|
---|
1017 | * technical attributes. The attributes:
|
---|
1018 | * <ol>
|
---|
1019 | * <li>deleted</ol>
|
---|
1020 | * <li>modified</ol>
|
---|
1021 | * <li>timestamp</ol>
|
---|
1022 | * <li>version</ol>
|
---|
1023 | * <li>visible</ol>
|
---|
1024 | * <li>user</ol>
|
---|
1025 | * </ol>
|
---|
1026 | * have to be equal
|
---|
1027 | * @param other the other primitive
|
---|
1028 | * @return true if this primitive and other are equal with respect to their
|
---|
1029 | * technical attributes
|
---|
1030 | */
|
---|
1031 | public boolean hasEqualTechnicalAttributes(OsmPrimitive other) {
|
---|
1032 | if (other == null) return false;
|
---|
1033 |
|
---|
1034 | return
|
---|
1035 | isDeleted() == other.isDeleted()
|
---|
1036 | && isModified() == other.isModified()
|
---|
1037 | && timestamp == other.timestamp
|
---|
1038 | && version == other.version
|
---|
1039 | && isVisible() == other.isVisible()
|
---|
1040 | && (user == null ? other.user==null : user==other.user)
|
---|
1041 | && changesetId == other.changesetId;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | private void updateTagged() {
|
---|
1045 | getUninterestingKeys();
|
---|
1046 | if (keys != null) {
|
---|
1047 | for (Entry<String,String> e : getKeys().entrySet()) {
|
---|
1048 | if (!uninteresting.contains(e.getKey())) {
|
---|
1049 | flags |= FLAG_TAGGED;
|
---|
1050 | return;
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 | flags &= ~FLAG_TAGGED;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | /**
|
---|
1058 | * true if this object is considered "tagged". To be "tagged", an object
|
---|
1059 | * must have one or more "interesting" tags. "created_by" and "source"
|
---|
1060 | * are typically considered "uninteresting" and do not make an object
|
---|
1061 | * "tagged".
|
---|
1062 | */
|
---|
1063 | public boolean isTagged() {
|
---|
1064 | return (flags & FLAG_TAGGED) != 0;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | private void updateHasDirectionKeys() {
|
---|
1068 | getDirectionKeys();
|
---|
1069 | if (keys != null) {
|
---|
1070 | for (Entry<String,String> e : getKeys().entrySet()) {
|
---|
1071 | if (directionKeys.contains(e.getKey())) {
|
---|
1072 | flags |= FLAG_HAS_DIRECTIONS;
|
---|
1073 | return;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 | flags &= ~FLAG_HAS_DIRECTIONS;
|
---|
1078 | }
|
---|
1079 | /**
|
---|
1080 | * true if this object has direction dependent tags (e.g. oneway)
|
---|
1081 | */
|
---|
1082 | public boolean hasDirectionKeys() {
|
---|
1083 | return (flags & FLAG_HAS_DIRECTIONS) != 0;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | /**
|
---|
1087 | * Replies the name of this primitive. The default implementation replies the value
|
---|
1088 | * of the tag <tt>name</tt> or null, if this tag is not present.
|
---|
1089 | *
|
---|
1090 | * @return the name of this primitive
|
---|
1091 | */
|
---|
1092 | public String getName() {
|
---|
1093 | if (get("name") != null)
|
---|
1094 | return get("name");
|
---|
1095 | return null;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * Replies the a localized name for this primitive given by the value of the tags (in this order)
|
---|
1100 | * <ul>
|
---|
1101 | * <li>name:lang_COUNTRY_Variant of the current locale</li>
|
---|
1102 | * <li>name:lang_COUNTRY of the current locale</li>
|
---|
1103 | * <li>name:lang of the current locale</li>
|
---|
1104 | * <li>name of the current locale</li>
|
---|
1105 | * </ul>
|
---|
1106 | *
|
---|
1107 | * null, if no such tag exists
|
---|
1108 | *
|
---|
1109 | * @return the name of this primitive
|
---|
1110 | */
|
---|
1111 | public String getLocalName() {
|
---|
1112 | String key = "name:" + Locale.getDefault().toString();
|
---|
1113 | if (get(key) != null)
|
---|
1114 | return get(key);
|
---|
1115 | key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
|
---|
1116 | if (get(key) != null)
|
---|
1117 | return get(key);
|
---|
1118 | key = "name:" + Locale.getDefault().getLanguage();
|
---|
1119 | if (get(key) != null)
|
---|
1120 | return get(key);
|
---|
1121 | return getName();
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
1126 | *
|
---|
1127 | * @return the display name
|
---|
1128 | */
|
---|
1129 | public abstract String getDisplayName(NameFormatter formatter);
|
---|
1130 |
|
---|
1131 | /**
|
---|
1132 | * Loads (clone) this primitive from provided PrimitiveData
|
---|
1133 | * @param data
|
---|
1134 | */
|
---|
1135 | public void load(PrimitiveData data) {
|
---|
1136 | setKeys(data.getKeys());
|
---|
1137 | timestamp = data.getTimestamp();
|
---|
1138 | user = data.getUser();
|
---|
1139 | changesetId = data.getChangesetId();
|
---|
1140 | setDeleted(data.isDeleted());
|
---|
1141 | setModified(data.isModified());
|
---|
1142 | setVisible(data.isVisible());
|
---|
1143 | setIncomplete(data.isIncomplete());
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | /**
|
---|
1147 | * Save parameters of this primitive to the transport object
|
---|
1148 | * @return
|
---|
1149 | */
|
---|
1150 | public abstract PrimitiveData save();
|
---|
1151 |
|
---|
1152 | protected void saveCommonAttributes(PrimitiveData data) {
|
---|
1153 | data.setId(id);
|
---|
1154 | data.getKeys().clear();
|
---|
1155 | data.getKeys().putAll(getKeys());
|
---|
1156 | data.setTimestamp(timestamp);
|
---|
1157 | data.setUser(user);
|
---|
1158 | data.setDeleted(isDeleted());
|
---|
1159 | data.setModified(isModified());
|
---|
1160 | data.setVisible(isVisible());
|
---|
1161 | data.setIncomplete(isIncomplete());
|
---|
1162 | data.setChangesetId(changesetId);
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | protected String getFlagsAsString() {
|
---|
1166 |
|
---|
1167 | StringBuilder builder = new StringBuilder();
|
---|
1168 |
|
---|
1169 | if (isIncomplete()) {
|
---|
1170 | builder.append("I");
|
---|
1171 | }
|
---|
1172 | if (isModified()) {
|
---|
1173 | builder.append("M");
|
---|
1174 | }
|
---|
1175 | if (isVisible()) {
|
---|
1176 | builder.append("V");
|
---|
1177 | }
|
---|
1178 | if (isDeleted()) {
|
---|
1179 | builder.append("D");
|
---|
1180 | }
|
---|
1181 | if (isFiltered()) {
|
---|
1182 | builder.append("f");
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | if (isDeleted()) {
|
---|
1186 | builder.append("d");
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | return builder.toString();
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | public abstract BBox getBBox();
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Called by Dataset to update cached position information of primitive (bbox, cached EarthNorth, ...)
|
---|
1196 | */
|
---|
1197 | public abstract void updatePosition();
|
---|
1198 |
|
---|
1199 | /**
|
---|
1200 | * Replies the unique primitive id for this primitive
|
---|
1201 | *
|
---|
1202 | * @return the unique primitive id for this primitive
|
---|
1203 | */
|
---|
1204 | public PrimitiveId getPrimitiveId() {
|
---|
1205 | return new SimplePrimitiveId(getUniqueId(), getType());
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | //TODO This method should not be necessary, incomplete state should be handled internally by OsmPrimitive
|
---|
1209 | public void setIncomplete(boolean incomplete) {
|
---|
1210 | this.incomplete = incomplete;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | public boolean isIncomplete() {
|
---|
1214 | return incomplete;
|
---|
1215 | }
|
---|
1216 | }
|
---|