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.text.MessageFormat;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.Date;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.LinkedHashSet;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Locale;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Set;
|
---|
20 | import java.util.Map.Entry;
|
---|
21 | import java.util.concurrent.atomic.AtomicLong;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.Main;
|
---|
24 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
25 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
26 | import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
|
---|
27 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
28 | import org.openstreetmap.josm.gui.mappaint.StyleCache;
|
---|
29 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
30 | import org.openstreetmap.josm.tools.Predicate;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * An OSM primitive can be associated with a key/value pair. It can be created, deleted
|
---|
34 | * and updated within the OSM-Server.
|
---|
35 | *
|
---|
36 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
37 | * it by any other than from the package {@link org.openstreetmap.josm.data.osm}. The available primitives are a fixed set that are given
|
---|
38 | * by the server environment and not an extendible data stuff.
|
---|
39 | *
|
---|
40 | * @author imi
|
---|
41 | */
|
---|
42 | abstract public class OsmPrimitive implements Comparable<OsmPrimitive>, Tagged, PrimitiveId {
|
---|
43 |
|
---|
44 | private static final AtomicLong idCounter = new AtomicLong(0);
|
---|
45 |
|
---|
46 | static long generateUniqueId() {
|
---|
47 | return idCounter.decrementAndGet();
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * This flag shows, that the properties have been changed by the user
|
---|
52 | * and on upload the object will be send to the server
|
---|
53 | */
|
---|
54 | private static final int FLAG_MODIFIED = 1 << 0;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * The visible flag indicates, that an object is marked
|
---|
58 | * as deleted on the server.
|
---|
59 | */
|
---|
60 | private static final int FLAG_VISIBLE = 1 << 1;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * An object can be disabled by the filter mechanism.
|
---|
64 | * Then it will show in a shade of gray on the map or it is completely
|
---|
65 | * hidden from the view.
|
---|
66 | * Disabled objects usually cannot be selected or modified
|
---|
67 | * while the filter is active.
|
---|
68 | */
|
---|
69 | private static final int FLAG_DISABLED = 1 << 2;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * An object that was deleted by the user.
|
---|
73 | * Deleted objects are usually hidden on the map and a request
|
---|
74 | * for deletion will be send to the server on upload.
|
---|
75 | * An object usually cannot be deleted if it has non-deleted
|
---|
76 | * objects still referring to it.
|
---|
77 | */
|
---|
78 | private static final int FLAG_DELETED = 1 << 3;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * This flag is only relevant if an object is disabled by the
|
---|
82 | * filter mechanism (i.e. FLAG_DISABLED is set).
|
---|
83 | * Then it indicates, whether it is completely hidden or
|
---|
84 | * just shown in gray color.
|
---|
85 | *
|
---|
86 | * When the primitive is not disabled, this flag should be
|
---|
87 | * unset as well (for efficient access).
|
---|
88 | */
|
---|
89 | private static final int FLAG_HIDE_IF_DISABLED = 1 << 4;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * This flag is set if the primitive is a way and
|
---|
93 | * according to the tags, the direction of the way is important.
|
---|
94 | * (e.g. one way street.)
|
---|
95 | */
|
---|
96 | private static final int FLAG_HAS_DIRECTIONS = 1 << 5;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * If the primitive is tagged.
|
---|
100 | * Some trivial tags like source=* are ignored here.
|
---|
101 | */
|
---|
102 | private static final int FLAG_TAGGED = 1 << 6;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * This flag is only relevant if FLAG_HAS_DIRECTIONS is set.
|
---|
106 | * It shows, that direction of the arrows should be reversed.
|
---|
107 | * (E.g. oneway=-1.)
|
---|
108 | */
|
---|
109 | private static final int FLAG_DIRECTION_REVERSED = 1 << 7;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * When hovering over ways and nodes in add mode, the
|
---|
113 | * "target" objects are visually highlighted. This flag indicates
|
---|
114 | * that the primitive is currently highlighted.
|
---|
115 | */
|
---|
116 | private static final int FLAG_HIGHLIGHTED = 1 << 8;
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * A primitive is incomplete if we know its id and type, but nothing more.
|
---|
120 | * Typically some members of a relation are incomplete until they are
|
---|
121 | * fetched from the server.
|
---|
122 | */
|
---|
123 | private static final int FLAG_INCOMPLETE = 1 << 9;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
127 | * another collection of {@see OsmPrimitive}s. The result collection is a list.
|
---|
128 | *
|
---|
129 | * If <code>list</code> is null, replies an empty list.
|
---|
130 | *
|
---|
131 | * @param <T>
|
---|
132 | * @param list the original list
|
---|
133 | * @param type the type to filter for
|
---|
134 | * @return the sub-list of OSM primitives of type <code>type</code>
|
---|
135 | */
|
---|
136 | static public <T extends OsmPrimitive> List<T> getFilteredList(Collection<OsmPrimitive> list, Class<T> type) {
|
---|
137 | if (list == null) return Collections.emptyList();
|
---|
138 | List<T> ret = new LinkedList<T>();
|
---|
139 | for(OsmPrimitive p: list) {
|
---|
140 | if (type.isInstance(p)) {
|
---|
141 | ret.add(type.cast(p));
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return ret;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
149 | * another collection of {@see OsmPrimitive}s. The result collection is a set.
|
---|
150 | *
|
---|
151 | * If <code>list</code> is null, replies an empty set.
|
---|
152 | *
|
---|
153 | * @param <T>
|
---|
154 | * @param list the original collection
|
---|
155 | * @param type the type to filter for
|
---|
156 | * @return the sub-set of OSM primitives of type <code>type</code>
|
---|
157 | */
|
---|
158 | static public <T extends OsmPrimitive> LinkedHashSet<T> getFilteredSet(Collection<OsmPrimitive> set, Class<T> type) {
|
---|
159 | LinkedHashSet<T> ret = new LinkedHashSet<T>();
|
---|
160 | if (set != null) {
|
---|
161 | for(OsmPrimitive p: set) {
|
---|
162 | if (type.isInstance(p)) {
|
---|
163 | ret.add(type.cast(p));
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | return ret;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Replies the collection of referring primitives for the primitives in <code>primitives</code>.
|
---|
172 | *
|
---|
173 | * @param primitives the collection of primitives.
|
---|
174 | * @return the collection of referring primitives for the primitives in <code>primitives</code>;
|
---|
175 | * empty set if primitives is null or if there are no referring primitives
|
---|
176 | */
|
---|
177 | static public Set<OsmPrimitive> getReferrer(Collection<? extends OsmPrimitive> primitives) {
|
---|
178 | HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
|
---|
179 | if (primitives == null || primitives.isEmpty()) return ret;
|
---|
180 | for (OsmPrimitive p: primitives) {
|
---|
181 | ret.addAll(p.getReferrers());
|
---|
182 | }
|
---|
183 | return ret;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Some predicates, that describe conditions on primitives.
|
---|
188 | */
|
---|
189 | public static final Predicate<OsmPrimitive> isUsablePredicate = new Predicate<OsmPrimitive>() {
|
---|
190 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
191 | return primitive.isUsable();
|
---|
192 | }
|
---|
193 | };
|
---|
194 |
|
---|
195 | public static final Predicate<OsmPrimitive> isSelectablePredicate = new Predicate<OsmPrimitive>() {
|
---|
196 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
197 | return primitive.isSelectable();
|
---|
198 | }
|
---|
199 | };
|
---|
200 |
|
---|
201 | public static final Predicate<OsmPrimitive> nonDeletedPredicate = new Predicate<OsmPrimitive>() {
|
---|
202 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
203 | return !primitive.isDeleted();
|
---|
204 | }
|
---|
205 | };
|
---|
206 |
|
---|
207 | public static final Predicate<OsmPrimitive> nonDeletedCompletePredicate = new Predicate<OsmPrimitive>() {
|
---|
208 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
209 | return !primitive.isDeleted() && !primitive.isIncomplete();
|
---|
210 | }
|
---|
211 | };
|
---|
212 |
|
---|
213 | public static final Predicate<OsmPrimitive> nonDeletedPhysicalPredicate = new Predicate<OsmPrimitive>() {
|
---|
214 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
215 | return !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation);
|
---|
216 | }
|
---|
217 | };
|
---|
218 |
|
---|
219 | public static final Predicate<OsmPrimitive> modifiedPredicate = new Predicate<OsmPrimitive>() {
|
---|
220 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
221 | return primitive.isModified();
|
---|
222 | }
|
---|
223 | };
|
---|
224 |
|
---|
225 | public static final Predicate<OsmPrimitive> nodePredicate = new Predicate<OsmPrimitive>() {
|
---|
226 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
227 | return primitive.getClass() == Node.class;
|
---|
228 | }
|
---|
229 | };
|
---|
230 |
|
---|
231 | public static final Predicate<OsmPrimitive> wayPredicate = new Predicate<OsmPrimitive>() {
|
---|
232 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
233 | return primitive.getClass() == Way.class;
|
---|
234 | }
|
---|
235 | };
|
---|
236 |
|
---|
237 | public static final Predicate<OsmPrimitive> relationPredicate = new Predicate<OsmPrimitive>() {
|
---|
238 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
239 | return primitive.getClass() == Relation.class;
|
---|
240 | }
|
---|
241 | };
|
---|
242 |
|
---|
243 | public static final Predicate<OsmPrimitive> allPredicate = new Predicate<OsmPrimitive>() {
|
---|
244 | public boolean evaluate(OsmPrimitive primitive) {
|
---|
245 | return true;
|
---|
246 | }
|
---|
247 | };
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Creates a new primitive for the given id.
|
---|
251 | *
|
---|
252 | * If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing.
|
---|
253 | * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
|
---|
254 | * positive number.
|
---|
255 | *
|
---|
256 | * @param id the id
|
---|
257 | * @param allowNegativeId
|
---|
258 | * @throws IllegalArgumentException thrown if id < 0 and allowNegativeId is false
|
---|
259 | */
|
---|
260 | protected OsmPrimitive(long id, boolean allowNegativeId) throws IllegalArgumentException {
|
---|
261 | if (allowNegativeId) {
|
---|
262 | this.id = id;
|
---|
263 | } else {
|
---|
264 | if (id < 0)
|
---|
265 | throw new IllegalArgumentException(MessageFormat.format("Expected ID >= 0. Got {0}.", id));
|
---|
266 | else if (id == 0) {
|
---|
267 | this.id = generateUniqueId();
|
---|
268 | } else {
|
---|
269 | this.id = id;
|
---|
270 | }
|
---|
271 |
|
---|
272 | }
|
---|
273 | this.version = 0;
|
---|
274 | this.setIncomplete(id > 0);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Creates a new primitive for the given id and version.
|
---|
279 | *
|
---|
280 | * If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing.
|
---|
281 | * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
|
---|
282 | * positive number.
|
---|
283 | *
|
---|
284 | * If id is not > 0 version is ignored and set to 0.
|
---|
285 | *
|
---|
286 | * @param id
|
---|
287 | * @param version
|
---|
288 | * @param allowNegativeId
|
---|
289 | * @throws IllegalArgumentException thrown if id < 0 and allowNegativeId is false
|
---|
290 | */
|
---|
291 | protected OsmPrimitive(long id, int version, boolean allowNegativeId) throws IllegalArgumentException {
|
---|
292 | this(id, allowNegativeId);
|
---|
293 | this.version = (id > 0 ? version : 0);
|
---|
294 | setIncomplete(id > 0 && version == 0);
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | /*----------
|
---|
299 | * MAPPAINT
|
---|
300 | *--------*/
|
---|
301 | public StyleCache mappaintStyle = null;
|
---|
302 | public int mappaintCacheIdx;
|
---|
303 |
|
---|
304 | /* This should not be called from outside. Fixing the UI to add relevant
|
---|
305 | get/set functions calling this implicitely is preferred, so we can have
|
---|
306 | transparent cache handling in the future. */
|
---|
307 | public void clearCached()
|
---|
308 | {
|
---|
309 | mappaintStyle = null;
|
---|
310 | }
|
---|
311 | /* end of mappaint data */
|
---|
312 |
|
---|
313 | /*---------
|
---|
314 | * DATASET
|
---|
315 | *---------*/
|
---|
316 |
|
---|
317 | /** the parent dataset */
|
---|
318 | private DataSet dataSet;
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * This method should never ever by called from somewhere else than Dataset.addPrimitive or removePrimitive methods
|
---|
322 | * @param dataSet
|
---|
323 | */
|
---|
324 | void setDataset(DataSet dataSet) {
|
---|
325 | if (this.dataSet != null && dataSet != null && this.dataSet != dataSet)
|
---|
326 | throw new DataIntegrityProblemException("Primitive cannot be included in more than one Dataset");
|
---|
327 | this.dataSet = dataSet;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | *
|
---|
332 | * @return DataSet this primitive is part of.
|
---|
333 | */
|
---|
334 | public DataSet getDataSet() {
|
---|
335 | return dataSet;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Throws exception if primitive is not part of the dataset
|
---|
340 | */
|
---|
341 | public void checkDataset() {
|
---|
342 | if (dataSet == null)
|
---|
343 | throw new DataIntegrityProblemException("Primitive must be part of the dataset: " + toString());
|
---|
344 | }
|
---|
345 |
|
---|
346 | protected boolean writeLock() {
|
---|
347 | if (dataSet != null) {
|
---|
348 | dataSet.beginUpdate();
|
---|
349 | return true;
|
---|
350 | } else
|
---|
351 | return false;
|
---|
352 | }
|
---|
353 |
|
---|
354 | protected void writeUnlock(boolean locked) {
|
---|
355 | if (locked) {
|
---|
356 | // It shouldn't be possible for dataset to become null because method calling setDataset would need write lock which is owned by this thread
|
---|
357 | dataSet.endUpdate();
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | /*-------------------
|
---|
363 | * OTHER PROPERTIES
|
---|
364 | *-------------------*/
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Unique identifier in OSM. This is used to identify objects on the server.
|
---|
368 | * An id of 0 means an unknown id. The object has not been uploaded yet to
|
---|
369 | * know what id it will get.
|
---|
370 | *
|
---|
371 | */
|
---|
372 | private long id = 0;
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * User that last modified this primitive, as specified by the server.
|
---|
376 | * Never changed by JOSM.
|
---|
377 | */
|
---|
378 | private User user = null;
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Contains the version number as returned by the API. Needed to
|
---|
382 | * ensure update consistency
|
---|
383 | */
|
---|
384 | private int version = 0;
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * The id of the changeset this primitive was last uploaded to.
|
---|
388 | * 0 if it wasn't uploaded to a changeset yet of if the changeset
|
---|
389 | * id isn't known.
|
---|
390 | */
|
---|
391 | private int changesetId;
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Replies the version number as returned by the API. The version is 0 if the id is 0 or
|
---|
395 | * if this primitive is incomplete.
|
---|
396 | *
|
---|
397 | * @see #setVersion(int)
|
---|
398 | */
|
---|
399 | public long getVersion() {
|
---|
400 | return version;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Replies the id of this primitive.
|
---|
405 | *
|
---|
406 | * @return the id of this primitive.
|
---|
407 | */
|
---|
408 | public long getId() {
|
---|
409 | long id = this.id;
|
---|
410 | return id >= 0?id:0;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | *
|
---|
415 | * @return Osm id if primitive already exists on the server. Unique negative value if primitive is new
|
---|
416 | */
|
---|
417 | public long getUniqueId() {
|
---|
418 | return id;
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | *
|
---|
423 | * @return True if primitive is new (not yet uploaded the server, id <= 0)
|
---|
424 | */
|
---|
425 | public boolean isNew() {
|
---|
426 | return id <= 0;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | *
|
---|
431 | * @return True if primitive is new or undeleted
|
---|
432 | * @see #isNew()
|
---|
433 | * @see #isUndeleted()
|
---|
434 | */
|
---|
435 | public boolean isNewOrUndeleted() {
|
---|
436 | return (id <= 0) || ((flags & (FLAG_VISIBLE + FLAG_DELETED)) == 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 | boolean locked = writeLock();
|
---|
453 | try {
|
---|
454 | if (id <= 0)
|
---|
455 | throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
|
---|
456 | if (version <= 0)
|
---|
457 | throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
|
---|
458 | if (dataSet != null && id != this.id) {
|
---|
459 | DataSet datasetCopy = dataSet;
|
---|
460 | // Reindex primitive
|
---|
461 | datasetCopy.removePrimitive(this);
|
---|
462 | this.id = id;
|
---|
463 | datasetCopy.addPrimitive(this);
|
---|
464 | }
|
---|
465 | this.id = id;
|
---|
466 | this.version = version;
|
---|
467 | this.setIncomplete(false);
|
---|
468 | } finally {
|
---|
469 | writeUnlock(locked);
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Clears the id and version known to the OSM API. The id and the version is set to 0.
|
---|
475 | * incomplete is set to false. It's preferred to use copy constructor with clearId set to true instead
|
---|
476 | * of calling this method.
|
---|
477 | *
|
---|
478 | * <strong>Caution</strong>: Do not use this method on primitives which are already added to a {@see DataSet}.
|
---|
479 | *
|
---|
480 | * @throws DataIntegrityProblemException If primitive was already added to the dataset
|
---|
481 | */
|
---|
482 | public void clearOsmId() {
|
---|
483 | if (dataSet != null)
|
---|
484 | throw new DataIntegrityProblemException("Method cannot be called after primitive was added to the dataset");
|
---|
485 |
|
---|
486 | // Not part of dataset - no lock necessary
|
---|
487 | this.id = generateUniqueId();
|
---|
488 | this.version = 0;
|
---|
489 | this.changesetId = 0; // reset changeset id on a new object
|
---|
490 | this.setIncomplete(false);
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Replies the user who has last touched this object. May be null.
|
---|
495 | *
|
---|
496 | * @return the user who has last touched this object. May be null.
|
---|
497 | */
|
---|
498 | public User getUser() {
|
---|
499 | return user;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Sets the user who has last touched this object.
|
---|
504 | *
|
---|
505 | * @param user the user
|
---|
506 | */
|
---|
507 | public void setUser(User user) {
|
---|
508 | boolean locked = writeLock();
|
---|
509 | try {
|
---|
510 | this.user = user;
|
---|
511 | } finally {
|
---|
512 | writeUnlock(locked);
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * Replies the id of the changeset this primitive was last uploaded to.
|
---|
518 | * 0 if this primitive wasn't uploaded to a changeset yet or if the
|
---|
519 | * changeset isn't known.
|
---|
520 | *
|
---|
521 | * @return the id of the changeset this primitive was last uploaded to.
|
---|
522 | */
|
---|
523 | public int getChangesetId() {
|
---|
524 | return changesetId;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Sets the changeset id of this primitive. Can't be set on a new
|
---|
529 | * primitive.
|
---|
530 | *
|
---|
531 | * @param changesetId the id. >= 0 required.
|
---|
532 | * @throws IllegalStateException thrown if this primitive is new.
|
---|
533 | * @throws IllegalArgumentException thrown if id < 0
|
---|
534 | */
|
---|
535 | public void setChangesetId(int changesetId) throws IllegalStateException, IllegalArgumentException {
|
---|
536 | boolean locked = writeLock();
|
---|
537 | try {
|
---|
538 | if (this.changesetId == changesetId)
|
---|
539 | return;
|
---|
540 | if (changesetId < 0)
|
---|
541 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' >= 0 expected, got {1}", "changesetId", changesetId));
|
---|
542 | if (isNew() && changesetId > 0)
|
---|
543 | throw new IllegalStateException(tr("Cannot assign a changesetId > 0 to a new primitive. Value of changesetId is {0}", changesetId));
|
---|
544 |
|
---|
545 | int old = this.changesetId;
|
---|
546 | this.changesetId = changesetId;
|
---|
547 | if (dataSet != null) {
|
---|
548 | dataSet.fireChangesetIdChanged(this, old, changesetId);
|
---|
549 | }
|
---|
550 | } finally {
|
---|
551 | writeUnlock(locked);
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | /**
|
---|
556 | * Replies the unique primitive id for this primitive
|
---|
557 | *
|
---|
558 | * @return the unique primitive id for this primitive
|
---|
559 | */
|
---|
560 | public PrimitiveId getPrimitiveId() {
|
---|
561 | return new SimplePrimitiveId(getUniqueId(), getType());
|
---|
562 | }
|
---|
563 |
|
---|
564 | public void setTimestamp(Date timestamp) {
|
---|
565 | boolean locked = writeLock();
|
---|
566 | try {
|
---|
567 | this.timestamp = (int)(timestamp.getTime() / 1000);
|
---|
568 | } finally {
|
---|
569 | writeUnlock(locked);
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Time of last modification to this object. This is not set by JOSM but
|
---|
575 | * read from the server and delivered back to the server unmodified. It is
|
---|
576 | * used to check against edit conflicts.
|
---|
577 | *
|
---|
578 | */
|
---|
579 | public Date getTimestamp() {
|
---|
580 | return new Date(timestamp * 1000l);
|
---|
581 | }
|
---|
582 |
|
---|
583 | public boolean isTimestampEmpty() {
|
---|
584 | return timestamp == 0;
|
---|
585 | }
|
---|
586 |
|
---|
587 | private int timestamp;
|
---|
588 |
|
---|
589 | /* -------
|
---|
590 | /* FLAGS
|
---|
591 | /* ------*/
|
---|
592 |
|
---|
593 | private volatile short flags = FLAG_VISIBLE; // visible per default
|
---|
594 |
|
---|
595 | private void updateFlagsNoLock(int flag, boolean value) {
|
---|
596 | if (value) {
|
---|
597 | flags |= flag;
|
---|
598 | } else {
|
---|
599 | flags &= ~flag;
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | private void updateFlags(int flag, boolean value) {
|
---|
604 | boolean locked = writeLock();
|
---|
605 | try {
|
---|
606 | updateFlagsNoLock(flag, value);
|
---|
607 | } finally {
|
---|
608 | writeUnlock(locked);
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Make the primitive disabled (e.g. if a filter applies).
|
---|
614 | * To enable the primitive again, use unsetDisabledState.
|
---|
615 | * @param hide if the primitive should be completely hidden from view or
|
---|
616 | * just shown in gray color.
|
---|
617 | */
|
---|
618 | public boolean setDisabledState(boolean hide) {
|
---|
619 | boolean locked = writeLock();
|
---|
620 | try {
|
---|
621 | int oldFlags = flags;
|
---|
622 | updateFlagsNoLock(FLAG_DISABLED, true);
|
---|
623 | updateFlagsNoLock(FLAG_HIDE_IF_DISABLED, hide);
|
---|
624 | return oldFlags != flags;
|
---|
625 | } finally {
|
---|
626 | writeUnlock(locked);
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * Remove the disabled flag from the primitive.
|
---|
632 | * Afterwards, the primitive is displayed normally and can be selected
|
---|
633 | * again.
|
---|
634 | */
|
---|
635 | public boolean unsetDisabledState() {
|
---|
636 | boolean locked = writeLock();
|
---|
637 | try {
|
---|
638 | int oldFlags = flags;
|
---|
639 | updateFlagsNoLock(FLAG_DISABLED + FLAG_HIDE_IF_DISABLED, false);
|
---|
640 | return oldFlags != flags;
|
---|
641 | } finally {
|
---|
642 | writeUnlock(locked);
|
---|
643 | }
|
---|
644 | }
|
---|
645 |
|
---|
646 | /**
|
---|
647 | * Replies true, if this primitive is disabled. (E.g. a filter
|
---|
648 | * applies)
|
---|
649 | */
|
---|
650 | public boolean isDisabled() {
|
---|
651 | return (flags & FLAG_DISABLED) != 0;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Replies true, if this primitive is disabled and marked as
|
---|
656 | * completely hidden on the map.
|
---|
657 | */
|
---|
658 | public boolean isDisabledAndHidden() {
|
---|
659 | return (((flags & FLAG_DISABLED) != 0) && ((flags & FLAG_HIDE_IF_DISABLED) != 0));
|
---|
660 | }
|
---|
661 |
|
---|
662 | @Deprecated
|
---|
663 | public boolean isFiltered() {
|
---|
664 | return isDisabledAndHidden();
|
---|
665 | }
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * Marks this primitive as being modified.
|
---|
669 | *
|
---|
670 | * @param modified true, if this primitive is to be modified
|
---|
671 | */
|
---|
672 | public void setModified(boolean modified) {
|
---|
673 | updateFlags(FLAG_MODIFIED, modified);
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Replies <code>true</code> if the object has been modified since it was loaded from
|
---|
678 | * the server. In this case, on next upload, this object will be updated.
|
---|
679 | *
|
---|
680 | * Deleted objects are deleted from the server. If the objects are added (id=0),
|
---|
681 | * the modified is ignored and the object is added to the server.
|
---|
682 | *
|
---|
683 | * @return <code>true</code> if the object has been modified since it was loaded from
|
---|
684 | * the server
|
---|
685 | */
|
---|
686 | public boolean isModified() {
|
---|
687 | return (flags & FLAG_MODIFIED) != 0;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * Replies <code>true</code>, if the object has been deleted.
|
---|
692 | *
|
---|
693 | * @return <code>true</code>, if the object has been deleted.
|
---|
694 | * @see #setDeleted(boolean)
|
---|
695 | */
|
---|
696 | public boolean isDeleted() {
|
---|
697 | return (flags & FLAG_DELETED) != 0;
|
---|
698 | }
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * Replies <code>true</code> if the object has been deleted on the server and was undeleted by the user.
|
---|
702 | * @return <code>true</code> if the object has been undeleted
|
---|
703 | */
|
---|
704 | public boolean isUndeleted() {
|
---|
705 | return (flags & (FLAG_VISIBLE + FLAG_DELETED)) == 0;
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Replies <code>true</code>, if the object is usable (i.e. complete
|
---|
710 | * and not deleted).
|
---|
711 | *
|
---|
712 | * @return <code>true</code>, if the object is usable.
|
---|
713 | * @see #delete(boolean)
|
---|
714 | */
|
---|
715 | public boolean isUsable() {
|
---|
716 | return (flags & (FLAG_DELETED + FLAG_INCOMPLETE)) == 0;
|
---|
717 | }
|
---|
718 |
|
---|
719 | public boolean isSelectable() {
|
---|
720 | return (flags & (FLAG_DELETED + FLAG_INCOMPLETE + FLAG_DISABLED + FLAG_HIDE_IF_DISABLED)) == 0;
|
---|
721 | }
|
---|
722 |
|
---|
723 | public boolean isDrawable() {
|
---|
724 | return (flags & (FLAG_DELETED + FLAG_INCOMPLETE + FLAG_HIDE_IF_DISABLED)) == 0;
|
---|
725 | }
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Replies true if this primitive is either unknown to the server (i.e. its id
|
---|
729 | * is 0) or it is known to the server and it hasn't be deleted on the server.
|
---|
730 | * Replies false, if this primitive is known on the server and has been deleted
|
---|
731 | * on the server.
|
---|
732 | *
|
---|
733 | * @see #setVisible(boolean)
|
---|
734 | */
|
---|
735 | public boolean isVisible() {
|
---|
736 | return (flags & FLAG_VISIBLE) != 0;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * Sets whether this primitive is visible, i.e. whether it is known on the server
|
---|
741 | * and not deleted on the server.
|
---|
742 | *
|
---|
743 | * @see #isVisible()
|
---|
744 | * @throws IllegalStateException thrown if visible is set to false on an primitive with
|
---|
745 | * id==0
|
---|
746 | */
|
---|
747 | public void setVisible(boolean visible) throws IllegalStateException{
|
---|
748 | boolean locked = writeLock();
|
---|
749 | try {
|
---|
750 | if (isNew() && visible == false)
|
---|
751 | throw new IllegalStateException(tr("A primitive with ID = 0 cannot be invisible."));
|
---|
752 | updateFlagsNoLock(FLAG_VISIBLE, visible);
|
---|
753 | } finally {
|
---|
754 | writeUnlock(locked);
|
---|
755 | }
|
---|
756 | }
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * Sets whether this primitive is deleted or not.
|
---|
760 | *
|
---|
761 | * Also marks this primitive as modified if deleted is true.
|
---|
762 | *
|
---|
763 | * @param deleted true, if this primitive is deleted; false, otherwise
|
---|
764 | */
|
---|
765 | public void setDeleted(boolean deleted) {
|
---|
766 | boolean locked = writeLock();
|
---|
767 | try {
|
---|
768 | updateFlagsNoLock(FLAG_DELETED, deleted);
|
---|
769 | setModified(deleted ^ !isVisible());
|
---|
770 | if (dataSet != null) {
|
---|
771 | if (deleted) {
|
---|
772 | dataSet.firePrimitivesRemoved(Collections.singleton(this), false);
|
---|
773 | } else {
|
---|
774 | dataSet.firePrimitivesAdded(Collections.singleton(this), false);
|
---|
775 | }
|
---|
776 | }
|
---|
777 | } finally {
|
---|
778 | writeUnlock(locked);
|
---|
779 | }
|
---|
780 | }
|
---|
781 |
|
---|
782 |
|
---|
783 | /**
|
---|
784 | * If set to true, this object is incomplete, which means only the id
|
---|
785 | * and type is known (type is the objects instance class)
|
---|
786 | */
|
---|
787 | private void setIncomplete(boolean incomplete) {
|
---|
788 | boolean locked = writeLock();
|
---|
789 | try {
|
---|
790 | if (dataSet != null && incomplete != this.isIncomplete()) {
|
---|
791 | if (incomplete) {
|
---|
792 | dataSet.firePrimitivesRemoved(Collections.singletonList(this), true);
|
---|
793 | } else {
|
---|
794 | dataSet.firePrimitivesAdded(Collections.singletonList(this), true);
|
---|
795 | }
|
---|
796 | }
|
---|
797 | updateFlagsNoLock(FLAG_INCOMPLETE, incomplete);
|
---|
798 | } finally {
|
---|
799 | writeUnlock(locked);
|
---|
800 | }
|
---|
801 | }
|
---|
802 |
|
---|
803 | public boolean isIncomplete() {
|
---|
804 | return (flags & FLAG_INCOMPLETE) != 0;
|
---|
805 | }
|
---|
806 |
|
---|
807 | public boolean isSelected() {
|
---|
808 | return dataSet != null && dataSet.isSelected(this);
|
---|
809 | }
|
---|
810 |
|
---|
811 | public boolean isMemberOfSelected() {
|
---|
812 | if (referrers == null)
|
---|
813 | return false;
|
---|
814 | if (referrers instanceof OsmPrimitive)
|
---|
815 | return referrers instanceof Relation && ((OsmPrimitive) referrers).isSelected();
|
---|
816 | for (OsmPrimitive ref : (OsmPrimitive[]) referrers) {
|
---|
817 | if (ref instanceof Relation && ref.isSelected())
|
---|
818 | return true;
|
---|
819 | }
|
---|
820 | return false;
|
---|
821 | }
|
---|
822 |
|
---|
823 | public void setHighlighted(boolean highlighted) {
|
---|
824 | if (isHighlighted() != highlighted) {
|
---|
825 | updateFlags(FLAG_HIGHLIGHTED, highlighted);
|
---|
826 | if (dataSet != null) {
|
---|
827 | dataSet.fireHighlightingChanged(this);
|
---|
828 | }
|
---|
829 | }
|
---|
830 | }
|
---|
831 |
|
---|
832 | public boolean isHighlighted() {
|
---|
833 | return (flags & FLAG_HIGHLIGHTED) != 0;
|
---|
834 | }
|
---|
835 |
|
---|
836 | /*----------------------------------
|
---|
837 | * UNINTERESTING AND DIRECTION KEYS
|
---|
838 | *----------------------------------*/
|
---|
839 |
|
---|
840 |
|
---|
841 | private static volatile Collection<String> uninteresting = null;
|
---|
842 | /**
|
---|
843 | * Contains a list of "uninteresting" keys that do not make an object
|
---|
844 | * "tagged". Entries that end with ':' are causing a whole namespace to be considered
|
---|
845 | * "uninteresting". Only the first level namespace is considered.
|
---|
846 | * Initialized by isUninterestingKey()
|
---|
847 | */
|
---|
848 | public static Collection<String> getUninterestingKeys() {
|
---|
849 | if (uninteresting == null) {
|
---|
850 | uninteresting = Main.pref.getCollection("tags.uninteresting",
|
---|
851 | Arrays.asList(new String[]{"source", "source_ref", "source:", "note", "comment",
|
---|
852 | "converted_by", "created_by", "watch", "watch:", "fixme", "FIXME",
|
---|
853 | "description", "attribution"}));
|
---|
854 | }
|
---|
855 | return uninteresting;
|
---|
856 | }
|
---|
857 |
|
---|
858 | /**
|
---|
859 | * Returns true if key is considered "uninteresting".
|
---|
860 | */
|
---|
861 | public static boolean isUninterestingKey(String key) {
|
---|
862 | getUninterestingKeys();
|
---|
863 | if (uninteresting.contains(key))
|
---|
864 | return true;
|
---|
865 | int pos = key.indexOf(':');
|
---|
866 | if (pos > 0)
|
---|
867 | return uninteresting.contains(key.substring(0, pos + 1));
|
---|
868 | return false;
|
---|
869 | }
|
---|
870 |
|
---|
871 | private static volatile Match directionKeys = null;
|
---|
872 | private static volatile Match reversedDirectionKeys = null;
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Contains a list of direction-dependent keys that make an object
|
---|
876 | * direction dependent.
|
---|
877 | * Initialized by checkDirectionTagged()
|
---|
878 | */
|
---|
879 | static {
|
---|
880 | // Legacy support - convert list of keys to search pattern
|
---|
881 | if (Main.pref.isCollection("tags.direction", false)) {
|
---|
882 | System.out.println("Collection of keys in tags.direction is no longer supported, value will converted to search pattern");
|
---|
883 | Collection<String> keys = Main.pref.getCollection("tags.direction", null);
|
---|
884 | StringBuilder builder = new StringBuilder();
|
---|
885 | for (String key:keys) {
|
---|
886 | builder.append(key);
|
---|
887 | builder.append("=* | ");
|
---|
888 | }
|
---|
889 | builder.delete(builder.length() - 3, builder.length());
|
---|
890 | Main.pref.put("tags.direction", builder.toString());
|
---|
891 | }
|
---|
892 |
|
---|
893 | // FIXME: incline=\"-*\" search pattern does not work.
|
---|
894 | String reversedDirectionDefault = "oneway=\"-1\" | incline=down | incline=\"-*\"";
|
---|
895 |
|
---|
896 | String directionDefault = "oneway? | incline=* | aerialway=* | "+
|
---|
897 | "waterway=stream | waterway=river | waterway=canal | waterway=drain | waterway=rapids | "+
|
---|
898 | "\"piste:type\"=downhill | \"piste:type\"=sled | man_made=\"piste:halfpipe\" | "+
|
---|
899 | "junction=roundabout";
|
---|
900 |
|
---|
901 | try {
|
---|
902 | reversedDirectionKeys = SearchCompiler.compile(Main.pref.get("tags.reversed_direction", reversedDirectionDefault), false, false);
|
---|
903 | } catch (ParseError e) {
|
---|
904 | System.err.println("Unable to compile pattern for tags.reversed_direction, trying default pattern: " + e.getMessage());
|
---|
905 |
|
---|
906 | try {
|
---|
907 | reversedDirectionKeys = SearchCompiler.compile(reversedDirectionDefault, false, false);
|
---|
908 | } catch (ParseError e2) {
|
---|
909 | throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage());
|
---|
910 | }
|
---|
911 | }
|
---|
912 | try {
|
---|
913 | directionKeys = SearchCompiler.compile(Main.pref.get("tags.direction", directionDefault), false, false);
|
---|
914 | } catch (ParseError e) {
|
---|
915 | System.err.println("Unable to compile pattern for tags.direction, trying default pattern: " + e.getMessage());
|
---|
916 |
|
---|
917 | try {
|
---|
918 | directionKeys = SearchCompiler.compile(directionDefault, false, false);
|
---|
919 | } catch (ParseError e2) {
|
---|
920 | throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage());
|
---|
921 | }
|
---|
922 | }
|
---|
923 | }
|
---|
924 |
|
---|
925 | private void updateTagged() {
|
---|
926 | if (keys != null) {
|
---|
927 | for (String key: keySet()) {
|
---|
928 | if (!isUninterestingKey(key)) {
|
---|
929 | updateFlagsNoLock(FLAG_TAGGED, true);
|
---|
930 | return;
|
---|
931 | }
|
---|
932 | }
|
---|
933 | }
|
---|
934 | updateFlagsNoLock(FLAG_TAGGED, false);
|
---|
935 | }
|
---|
936 |
|
---|
937 | /**
|
---|
938 | * true if this object is considered "tagged". To be "tagged", an object
|
---|
939 | * must have one or more "interesting" tags. "created_by" and "source"
|
---|
940 | * are typically considered "uninteresting" and do not make an object
|
---|
941 | * "tagged".
|
---|
942 | */
|
---|
943 | public boolean isTagged() {
|
---|
944 | return (flags & FLAG_TAGGED) != 0;
|
---|
945 | }
|
---|
946 |
|
---|
947 | private void updateDirectionFlags() {
|
---|
948 | boolean hasDirections = false;
|
---|
949 | boolean directionReversed = false;
|
---|
950 | if (reversedDirectionKeys.match(this)) {
|
---|
951 | hasDirections = true;
|
---|
952 | directionReversed = true;
|
---|
953 | }
|
---|
954 | if (directionKeys.match(this)) {
|
---|
955 | hasDirections = true;
|
---|
956 | }
|
---|
957 |
|
---|
958 | updateFlagsNoLock(FLAG_DIRECTION_REVERSED, directionReversed);
|
---|
959 | updateFlagsNoLock(FLAG_HAS_DIRECTIONS, hasDirections);
|
---|
960 | }
|
---|
961 |
|
---|
962 | /**
|
---|
963 | * true if this object has direction dependent tags (e.g. oneway)
|
---|
964 | */
|
---|
965 | public boolean hasDirectionKeys() {
|
---|
966 | return (flags & FLAG_HAS_DIRECTIONS) != 0;
|
---|
967 | }
|
---|
968 |
|
---|
969 | public boolean reversedDirection() {
|
---|
970 | return (flags & FLAG_DIRECTION_REVERSED) != 0;
|
---|
971 | }
|
---|
972 |
|
---|
973 | /*------------
|
---|
974 | * Keys handling
|
---|
975 | ------------*/
|
---|
976 |
|
---|
977 | // Note that all methods that read keys first make local copy of keys array reference. This is to ensure thread safety - reading
|
---|
978 | // doesn't have to be locked so it's possible that keys array will be modified. But all write methods make copy of keys array so
|
---|
979 | // the array itself will be never modified - only reference will be changed
|
---|
980 |
|
---|
981 | /**
|
---|
982 | * The key/value list for this primitive.
|
---|
983 | *
|
---|
984 | */
|
---|
985 | private String[] keys;
|
---|
986 |
|
---|
987 | /**
|
---|
988 | * Replies the map of key/value pairs. Never replies null. The map can be empty, though.
|
---|
989 | *
|
---|
990 | * @return tags of this primitive. Changes made in returned map are not mapped
|
---|
991 | * back to the primitive, use setKeys() to modify the keys
|
---|
992 | */
|
---|
993 | public Map<String, String> getKeys() {
|
---|
994 | Map<String, String> result = new HashMap<String, String>();
|
---|
995 | String[] keys = this.keys;
|
---|
996 | if (keys != null) {
|
---|
997 | for (int i=0; i<keys.length ; i+=2) {
|
---|
998 | result.put(keys[i], keys[i + 1]);
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 | return result;
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /**
|
---|
1005 | * Sets the keys of this primitives to the key/value pairs in <code>keys</code>.
|
---|
1006 | * If <code>keys</code> is null removes all existing key/value pairs.
|
---|
1007 | *
|
---|
1008 | * @param keys the key/value pairs to set. If null, removes all existing key/value pairs.
|
---|
1009 | */
|
---|
1010 | public void setKeys(Map<String, String> keys) {
|
---|
1011 | boolean locked = writeLock();
|
---|
1012 | try {
|
---|
1013 | Map<String, String> originalKeys = getKeys();
|
---|
1014 | if (keys == null || keys.isEmpty()) {
|
---|
1015 | this.keys = null;
|
---|
1016 | keysChangedImpl(originalKeys);
|
---|
1017 | return;
|
---|
1018 | }
|
---|
1019 | String[] newKeys = new String[keys.size() * 2];
|
---|
1020 | int index = 0;
|
---|
1021 | for (Entry<String, String> entry:keys.entrySet()) {
|
---|
1022 | newKeys[index++] = entry.getKey();
|
---|
1023 | newKeys[index++] = entry.getValue();
|
---|
1024 | }
|
---|
1025 | this.keys = newKeys;
|
---|
1026 | keysChangedImpl(originalKeys);
|
---|
1027 | } finally {
|
---|
1028 | writeUnlock(locked);
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Set the given value to the given key. If key is null, does nothing. If value is null,
|
---|
1034 | * removes the key and behaves like {@see #remove(String)}.
|
---|
1035 | *
|
---|
1036 | * @param key The key, for which the value is to be set. Can be null, does nothing in this case.
|
---|
1037 | * @param value The value for the key. If null, removes the respective key/value pair.
|
---|
1038 | *
|
---|
1039 | * @see #remove(String)
|
---|
1040 | */
|
---|
1041 | public final void put(String key, String value) {
|
---|
1042 | boolean locked = writeLock();
|
---|
1043 | try {
|
---|
1044 | Map<String, String> originalKeys = getKeys();
|
---|
1045 | if (key == null)
|
---|
1046 | return;
|
---|
1047 | else if (value == null) {
|
---|
1048 | remove(key);
|
---|
1049 | } else if (keys == null){
|
---|
1050 | keys = new String[] {key, value};
|
---|
1051 | keysChangedImpl(originalKeys);
|
---|
1052 | } else {
|
---|
1053 | for (int i=0; i<keys.length;i+=2) {
|
---|
1054 | if (keys[i].equals(key)) {
|
---|
1055 | keys[i+1] = value; // This modifies the keys array but it doesn't make it invalidate for any time so its ok (see note no top)
|
---|
1056 | keysChangedImpl(originalKeys);
|
---|
1057 | return;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | String[] newKeys = new String[keys.length + 2];
|
---|
1061 | for (int i=0; i< keys.length;i+=2) {
|
---|
1062 | newKeys[i] = keys[i];
|
---|
1063 | newKeys[i+1] = keys[i+1];
|
---|
1064 | }
|
---|
1065 | newKeys[keys.length] = key;
|
---|
1066 | newKeys[keys.length + 1] = value;
|
---|
1067 | keys = newKeys;
|
---|
1068 | keysChangedImpl(originalKeys);
|
---|
1069 | }
|
---|
1070 | } finally {
|
---|
1071 | writeUnlock(locked);
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 | /**
|
---|
1075 | * Remove the given key from the list
|
---|
1076 | *
|
---|
1077 | * @param key the key to be removed. Ignored, if key is null.
|
---|
1078 | */
|
---|
1079 | public final void remove(String key) {
|
---|
1080 | boolean locked = writeLock();
|
---|
1081 | try {
|
---|
1082 | if (key == null || keys == null) return;
|
---|
1083 | if (!hasKey(key))
|
---|
1084 | return;
|
---|
1085 | Map<String, String> originalKeys = getKeys();
|
---|
1086 | if (keys.length == 2) {
|
---|
1087 | keys = null;
|
---|
1088 | keysChangedImpl(originalKeys);
|
---|
1089 | return;
|
---|
1090 | }
|
---|
1091 | String[] newKeys = new String[keys.length - 2];
|
---|
1092 | int j=0;
|
---|
1093 | for (int i=0; i < keys.length; i+=2) {
|
---|
1094 | if (!keys[i].equals(key)) {
|
---|
1095 | newKeys[j++] = keys[i];
|
---|
1096 | newKeys[j++] = keys[i+1];
|
---|
1097 | }
|
---|
1098 | }
|
---|
1099 | keys = newKeys;
|
---|
1100 | keysChangedImpl(originalKeys);
|
---|
1101 | } finally {
|
---|
1102 | writeUnlock(locked);
|
---|
1103 | }
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | /**
|
---|
1107 | * Removes all keys from this primitive.
|
---|
1108 | *
|
---|
1109 | * @since 1843
|
---|
1110 | */
|
---|
1111 | public final void removeAll() {
|
---|
1112 | boolean locked = writeLock();
|
---|
1113 | try {
|
---|
1114 | if (keys != null) {
|
---|
1115 | Map<String, String> originalKeys = getKeys();
|
---|
1116 | keys = null;
|
---|
1117 | keysChangedImpl(originalKeys);
|
---|
1118 | }
|
---|
1119 | } finally {
|
---|
1120 | writeUnlock(locked);
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /**
|
---|
1125 | * Replies the value for key <code>key</code>. Replies null, if <code>key</code> is null.
|
---|
1126 | * Replies null, if there is no value for the given key.
|
---|
1127 | *
|
---|
1128 | * @param key the key. Can be null, replies null in this case.
|
---|
1129 | * @return the value for key <code>key</code>.
|
---|
1130 | */
|
---|
1131 | public final String get(String key) {
|
---|
1132 | String[] keys = this.keys;
|
---|
1133 | if (key == null)
|
---|
1134 | return null;
|
---|
1135 | if (keys == null)
|
---|
1136 | return null;
|
---|
1137 | for (int i=0; i<keys.length;i+=2) {
|
---|
1138 | if (keys[i].equals(key)) return keys[i+1];
|
---|
1139 | }
|
---|
1140 | return null;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 | public final Collection<String> keySet() {
|
---|
1144 | String[] keys = this.keys;
|
---|
1145 | if (keys == null)
|
---|
1146 | return Collections.emptySet();
|
---|
1147 | Set<String> result = new HashSet<String>(keys.length / 2);
|
---|
1148 | for (int i=0; i<keys.length; i+=2) {
|
---|
1149 | result.add(keys[i]);
|
---|
1150 | }
|
---|
1151 | return result;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | /**
|
---|
1155 | * Replies true, if the map of key/value pairs of this primitive is not empty.
|
---|
1156 | *
|
---|
1157 | * @return true, if the map of key/value pairs of this primitive is not empty; false
|
---|
1158 | * otherwise
|
---|
1159 | */
|
---|
1160 | public final boolean hasKeys() {
|
---|
1161 | return keys != null;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | private void keysChangedImpl(Map<String, String> originalKeys) {
|
---|
1165 | clearCached();
|
---|
1166 | updateDirectionFlags();
|
---|
1167 | updateTagged();
|
---|
1168 | if (dataSet != null) {
|
---|
1169 | dataSet.fireTagsChanged(this, originalKeys);
|
---|
1170 | }
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /**
|
---|
1174 | * Replies true if this primitive has a tag with key <code>key</code>
|
---|
1175 | *
|
---|
1176 | * @param key the key
|
---|
1177 | * @return true, if his primitive has a tag with key <code>key</code>
|
---|
1178 | */
|
---|
1179 | public boolean hasKey(String key) {
|
---|
1180 | String[] keys = this.keys;
|
---|
1181 | if (key == null) return false;
|
---|
1182 | if (keys == null) return false;
|
---|
1183 | for (int i=0; i< keys.length;i+=2) {
|
---|
1184 | if (keys[i].equals(key)) return true;
|
---|
1185 | }
|
---|
1186 | return false;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | /**
|
---|
1190 | * Replies true if other isn't null and has the same tags (key/value-pairs) as this.
|
---|
1191 | *
|
---|
1192 | * @param other the other object primitive
|
---|
1193 | * @return true if other isn't null and has the same tags (key/value-pairs) as this.
|
---|
1194 | */
|
---|
1195 | public boolean hasSameTags(OsmPrimitive other) {
|
---|
1196 | return getKeys().equals(other.getKeys());
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | /*------------
|
---|
1200 | * Referrers
|
---|
1201 | ------------*/
|
---|
1202 |
|
---|
1203 | private Object referrers;
|
---|
1204 |
|
---|
1205 | /**
|
---|
1206 | * Add new referrer. If referrer is already included then no action is taken
|
---|
1207 | * @param referrer
|
---|
1208 | */
|
---|
1209 | protected void addReferrer(OsmPrimitive referrer) {
|
---|
1210 | // Based on methods from josm-ng
|
---|
1211 | if (referrers == null) {
|
---|
1212 | referrers = referrer;
|
---|
1213 | } else if (referrers instanceof OsmPrimitive) {
|
---|
1214 | if (referrers != referrer) {
|
---|
1215 | referrers = new OsmPrimitive[] { (OsmPrimitive)referrers, referrer };
|
---|
1216 | }
|
---|
1217 | } else {
|
---|
1218 | for (OsmPrimitive primitive:(OsmPrimitive[])referrers) {
|
---|
1219 | if (primitive == referrer)
|
---|
1220 | return;
|
---|
1221 | }
|
---|
1222 | OsmPrimitive[] orig = (OsmPrimitive[])referrers;
|
---|
1223 | OsmPrimitive[] bigger = new OsmPrimitive[orig.length+1];
|
---|
1224 | System.arraycopy(orig, 0, bigger, 0, orig.length);
|
---|
1225 | bigger[orig.length] = referrer;
|
---|
1226 | referrers = bigger;
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | /**
|
---|
1231 | * Remove referrer. No action is taken if referrer is not registered
|
---|
1232 | * @param referrer
|
---|
1233 | */
|
---|
1234 | protected void removeReferrer(OsmPrimitive referrer) {
|
---|
1235 | // Based on methods from josm-ng
|
---|
1236 | if (referrers instanceof OsmPrimitive) {
|
---|
1237 | if (referrers == referrer) {
|
---|
1238 | referrers = null;
|
---|
1239 | }
|
---|
1240 | } else if (referrers instanceof OsmPrimitive[]) {
|
---|
1241 | OsmPrimitive[] orig = (OsmPrimitive[])referrers;
|
---|
1242 | int idx = -1;
|
---|
1243 | for (int i=0; i<orig.length; i++) {
|
---|
1244 | if (orig[i] == referrer) {
|
---|
1245 | idx = i;
|
---|
1246 | break;
|
---|
1247 | }
|
---|
1248 | }
|
---|
1249 | if (idx == -1)
|
---|
1250 | return;
|
---|
1251 |
|
---|
1252 | if (orig.length == 2) {
|
---|
1253 | referrers = orig[1-idx]; // idx is either 0 or 1, take the other
|
---|
1254 | } else { // downsize the array
|
---|
1255 | OsmPrimitive[] smaller = new OsmPrimitive[orig.length-1];
|
---|
1256 | System.arraycopy(orig, 0, smaller, 0, idx);
|
---|
1257 | System.arraycopy(orig, idx+1, smaller, idx, smaller.length-idx);
|
---|
1258 | referrers = smaller;
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 | }
|
---|
1262 | /**
|
---|
1263 | * Find primitives that reference this primitive. Returns only primitives that are included in the same
|
---|
1264 | * dataset as this primitive. <br>
|
---|
1265 | *
|
---|
1266 | * For example following code will add wnew as referer to all nodes of existingWay, but this method will
|
---|
1267 | * not return wnew because it's not part of the dataset <br>
|
---|
1268 | *
|
---|
1269 | * <code>Way wnew = new Way(existingWay)</code>
|
---|
1270 | *
|
---|
1271 | * @return a collection of all primitives that reference this primitive.
|
---|
1272 | */
|
---|
1273 |
|
---|
1274 | public final List<OsmPrimitive> getReferrers() {
|
---|
1275 | // Method copied from OsmPrimitive in josm-ng
|
---|
1276 | // Returns only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
1277 | // when way is cloned
|
---|
1278 | checkDataset();
|
---|
1279 | Object referrers = this.referrers;
|
---|
1280 | List<OsmPrimitive> result = new ArrayList<OsmPrimitive>();
|
---|
1281 | if (referrers != null) {
|
---|
1282 | if (referrers instanceof OsmPrimitive) {
|
---|
1283 | OsmPrimitive ref = (OsmPrimitive)referrers;
|
---|
1284 | if (ref.dataSet == dataSet) {
|
---|
1285 | result.add(ref);
|
---|
1286 | }
|
---|
1287 | } else {
|
---|
1288 | for (OsmPrimitive o:(OsmPrimitive[])referrers) {
|
---|
1289 | if (dataSet == o.dataSet) {
|
---|
1290 | result.add(o);
|
---|
1291 | }
|
---|
1292 | }
|
---|
1293 | }
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 | return result;
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | /**
|
---|
1300 | Return true, if this primitive is referred by at least n ways
|
---|
1301 | @param n Minimal number of ways to return true. Must be positive
|
---|
1302 | */
|
---|
1303 | public final boolean isReferredByWays(int n) {
|
---|
1304 | // Count only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
1305 | // when way is cloned
|
---|
1306 | Object referrers = this.referrers;
|
---|
1307 | if (referrers == null) return false;
|
---|
1308 | checkDataset();
|
---|
1309 | if (referrers instanceof OsmPrimitive) {
|
---|
1310 | return n<=1 && referrers instanceof Way && ((OsmPrimitive)referrers).dataSet == dataSet;
|
---|
1311 | } else {
|
---|
1312 | int counter=0;
|
---|
1313 | for (OsmPrimitive o : (OsmPrimitive[])referrers) {
|
---|
1314 | if (dataSet == o.dataSet && o instanceof Way) {
|
---|
1315 | if (++counter >= n)
|
---|
1316 | return true;
|
---|
1317 | }
|
---|
1318 | }
|
---|
1319 | return false;
|
---|
1320 | }
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 |
|
---|
1324 | /*-----------------
|
---|
1325 | * OTHER METHODS
|
---|
1326 | *----------------/
|
---|
1327 |
|
---|
1328 | /**
|
---|
1329 | * Implementation of the visitor scheme. Subclasses have to call the correct
|
---|
1330 | * visitor function.
|
---|
1331 | * @param visitor The visitor from which the visit() function must be called.
|
---|
1332 | */
|
---|
1333 | abstract public void visit(Visitor visitor);
|
---|
1334 |
|
---|
1335 |
|
---|
1336 | /**
|
---|
1337 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
1338 | * use this only in the data initializing phase
|
---|
1339 | */
|
---|
1340 | public void cloneFrom(OsmPrimitive other) {
|
---|
1341 | // write lock is provided by subclasses
|
---|
1342 | if (id != other.id && dataSet != null)
|
---|
1343 | throw new DataIntegrityProblemException("Osm id cannot be changed after primitive was added to the dataset");
|
---|
1344 | setKeys(other.getKeys());
|
---|
1345 | id = other.id;
|
---|
1346 | if (id <=0) {
|
---|
1347 | // reset version and changeset id
|
---|
1348 | version = 0;
|
---|
1349 | changesetId = 0;
|
---|
1350 | }
|
---|
1351 | timestamp = other.timestamp;
|
---|
1352 | if (id > 0) {
|
---|
1353 | version = other.version;
|
---|
1354 | }
|
---|
1355 | setIncomplete(other.isIncomplete());
|
---|
1356 | flags = other.flags;
|
---|
1357 | user= other.user;
|
---|
1358 | if (id > 0 && other.changesetId > 0) {
|
---|
1359 | // #4208: sometimes we cloned from other with id < 0 *and*
|
---|
1360 | // an assigned changeset id. Don't know why yet. For primitives
|
---|
1361 | // with id < 0 we don't propagate the changeset id any more.
|
---|
1362 | //
|
---|
1363 | setChangesetId(other.changesetId);
|
---|
1364 | }
|
---|
1365 | clearCached();
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | /**
|
---|
1369 | * Merges the technical and semantical attributes from <code>other</code> onto this.
|
---|
1370 | *
|
---|
1371 | * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
|
---|
1372 | * have an assigend OSM id, the IDs have to be the same.
|
---|
1373 | *
|
---|
1374 | * @param other the other primitive. Must not be null.
|
---|
1375 | * @throws IllegalArgumentException thrown if other is null.
|
---|
1376 | * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not
|
---|
1377 | * @throws DataIntegrityProblemException thrown if other isn't new and other.getId() != this.getId()
|
---|
1378 | */
|
---|
1379 | public void mergeFrom(OsmPrimitive other) {
|
---|
1380 | boolean locked = writeLock();
|
---|
1381 | try {
|
---|
1382 | CheckParameterUtil.ensureParameterNotNull(other, "other");
|
---|
1383 | if (other.isNew() ^ isNew())
|
---|
1384 | throw new DataIntegrityProblemException(tr("Cannot merge because either of the participating primitives is new and the other is not"));
|
---|
1385 | if (! other.isNew() && other.getId() != id)
|
---|
1386 | throw new DataIntegrityProblemException(tr("Cannot merge primitives with different ids. This id is {0}, the other is {1}", id, other.getId()));
|
---|
1387 |
|
---|
1388 | setKeys(other.getKeys());
|
---|
1389 | timestamp = other.timestamp;
|
---|
1390 | version = other.version;
|
---|
1391 | setIncomplete(other.isIncomplete());
|
---|
1392 | flags = other.flags;
|
---|
1393 | user= other.user;
|
---|
1394 | changesetId = other.changesetId;
|
---|
1395 | } finally {
|
---|
1396 | writeUnlock(locked);
|
---|
1397 | }
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /**
|
---|
1401 | * Replies true if this primitive and other are equal with respect to their
|
---|
1402 | * semantic attributes.
|
---|
1403 | * <ol>
|
---|
1404 | * <li>equal id</ol>
|
---|
1405 | * <li>both are complete or both are incomplete</li>
|
---|
1406 | * <li>both have the same tags</li>
|
---|
1407 | * </ol>
|
---|
1408 | * @param other
|
---|
1409 | * @return true if this primitive and other are equal with respect to their
|
---|
1410 | * semantic attributes.
|
---|
1411 | */
|
---|
1412 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
1413 | if (!isNew() && id != other.id)
|
---|
1414 | return false;
|
---|
1415 | if (isIncomplete() && ! other.isIncomplete() || !isIncomplete() && other.isIncomplete())
|
---|
1416 | return false;
|
---|
1417 | // can't do an equals check on the internal keys array because it is not ordered
|
---|
1418 | //
|
---|
1419 | return hasSameTags(other);
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | /**
|
---|
1423 | * Replies true if this primitive and other are equal with respect to their
|
---|
1424 | * technical attributes. The attributes:
|
---|
1425 | * <ol>
|
---|
1426 | * <li>deleted</ol>
|
---|
1427 | * <li>modified</ol>
|
---|
1428 | * <li>timestamp</ol>
|
---|
1429 | * <li>version</ol>
|
---|
1430 | * <li>visible</ol>
|
---|
1431 | * <li>user</ol>
|
---|
1432 | * </ol>
|
---|
1433 | * have to be equal
|
---|
1434 | * @param other the other primitive
|
---|
1435 | * @return true if this primitive and other are equal with respect to their
|
---|
1436 | * technical attributes
|
---|
1437 | */
|
---|
1438 | public boolean hasEqualTechnicalAttributes(OsmPrimitive other) {
|
---|
1439 | if (other == null) return false;
|
---|
1440 |
|
---|
1441 | return
|
---|
1442 | isDeleted() == other.isDeleted()
|
---|
1443 | && isModified() == other.isModified()
|
---|
1444 | && timestamp == other.timestamp
|
---|
1445 | && version == other.version
|
---|
1446 | && isVisible() == other.isVisible()
|
---|
1447 | && (user == null ? other.user==null : user==other.user)
|
---|
1448 | && changesetId == other.changesetId;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | /**
|
---|
1452 | * Replies the name of this primitive. The default implementation replies the value
|
---|
1453 | * of the tag <tt>name</tt> or null, if this tag is not present.
|
---|
1454 | *
|
---|
1455 | * @return the name of this primitive
|
---|
1456 | */
|
---|
1457 | public String getName() {
|
---|
1458 | return get("name");
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | /**
|
---|
1462 | * Replies the a localized name for this primitive given by the value of the tags (in this order)
|
---|
1463 | * <ul>
|
---|
1464 | * <li>name:lang_COUNTRY_Variant of the current locale</li>
|
---|
1465 | * <li>name:lang_COUNTRY of the current locale</li>
|
---|
1466 | * <li>name:lang of the current locale</li>
|
---|
1467 | * <li>name of the current locale</li>
|
---|
1468 | * </ul>
|
---|
1469 | *
|
---|
1470 | * null, if no such tag exists
|
---|
1471 | *
|
---|
1472 | * @return the name of this primitive
|
---|
1473 | */
|
---|
1474 | public String getLocalName() {
|
---|
1475 | String key = "name:" + Locale.getDefault().toString();
|
---|
1476 | if (get(key) != null)
|
---|
1477 | return get(key);
|
---|
1478 | key = "name:" + Locale.getDefault().getLanguage() + "_" + Locale.getDefault().getCountry();
|
---|
1479 | if (get(key) != null)
|
---|
1480 | return get(key);
|
---|
1481 | key = "name:" + Locale.getDefault().getLanguage();
|
---|
1482 | if (get(key) != null)
|
---|
1483 | return get(key);
|
---|
1484 | return getName();
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /**
|
---|
1488 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
1489 | *
|
---|
1490 | * @return the display name
|
---|
1491 | */
|
---|
1492 | public abstract String getDisplayName(NameFormatter formatter);
|
---|
1493 |
|
---|
1494 | /**
|
---|
1495 | * Loads (clone) this primitive from provided PrimitiveData
|
---|
1496 | * @param data
|
---|
1497 | */
|
---|
1498 | public void load(PrimitiveData data) {
|
---|
1499 | // Write lock is provided by subclasses
|
---|
1500 | setKeys(data.getKeys());
|
---|
1501 | setTimestamp(data.getTimestamp());
|
---|
1502 | user = data.getUser();
|
---|
1503 | setChangesetId(data.getChangesetId());
|
---|
1504 | setDeleted(data.isDeleted());
|
---|
1505 | setModified(data.isModified());
|
---|
1506 | setIncomplete(data.isIncomplete());
|
---|
1507 | version = data.getVersion();
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | /**
|
---|
1511 | * Save parameters of this primitive to the transport object
|
---|
1512 | * @return
|
---|
1513 | */
|
---|
1514 | public abstract PrimitiveData save();
|
---|
1515 |
|
---|
1516 | protected void saveCommonAttributes(PrimitiveData data) {
|
---|
1517 | data.setId(id);
|
---|
1518 | data.getKeys().clear();
|
---|
1519 | data.getKeys().putAll(getKeys());
|
---|
1520 | data.setTimestamp(getTimestamp());
|
---|
1521 | data.setUser(user);
|
---|
1522 | data.setDeleted(isDeleted());
|
---|
1523 | data.setModified(isModified());
|
---|
1524 | data.setVisible(isVisible());
|
---|
1525 | data.setIncomplete(isIncomplete());
|
---|
1526 | data.setChangesetId(changesetId);
|
---|
1527 | data.setVersion(version);
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 |
|
---|
1531 | public abstract BBox getBBox();
|
---|
1532 |
|
---|
1533 | /**
|
---|
1534 | * Called by Dataset to update cached position information of primitive (bbox, cached EarthNorth, ...)
|
---|
1535 | */
|
---|
1536 | public abstract void updatePosition();
|
---|
1537 |
|
---|
1538 | /*----------------
|
---|
1539 | * OBJECT METHODS
|
---|
1540 | *---------------*/
|
---|
1541 |
|
---|
1542 | protected String getFlagsAsString() {
|
---|
1543 | StringBuilder builder = new StringBuilder();
|
---|
1544 |
|
---|
1545 | if (isIncomplete()) {
|
---|
1546 | builder.append("I");
|
---|
1547 | }
|
---|
1548 | if (isModified()) {
|
---|
1549 | builder.append("M");
|
---|
1550 | }
|
---|
1551 | if (isVisible()) {
|
---|
1552 | builder.append("V");
|
---|
1553 | }
|
---|
1554 | if (isDeleted()) {
|
---|
1555 | builder.append("D");
|
---|
1556 | }
|
---|
1557 | if (isDisabled()) {
|
---|
1558 | if (isDisabledAndHidden()) {
|
---|
1559 | builder.append("h");
|
---|
1560 | } else {
|
---|
1561 | builder.append("d");
|
---|
1562 | }
|
---|
1563 | }
|
---|
1564 | if (isTagged()) {
|
---|
1565 | builder.append("T");
|
---|
1566 | }
|
---|
1567 | if (hasDirectionKeys()) {
|
---|
1568 | if (reversedDirection()) {
|
---|
1569 | builder.append("<");
|
---|
1570 | } else {
|
---|
1571 | builder.append(">");
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 | return builder.toString();
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 | /**
|
---|
1578 | * Equal, if the id (and class) is equal.
|
---|
1579 | *
|
---|
1580 | * An primitive is equal to its incomplete counter part.
|
---|
1581 | */
|
---|
1582 | @Override public boolean equals(Object obj) {
|
---|
1583 | if (obj instanceof OsmPrimitive)
|
---|
1584 | return ((OsmPrimitive)obj).id == id && obj.getClass() == getClass();
|
---|
1585 | return false;
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | /**
|
---|
1589 | * Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.
|
---|
1590 | *
|
---|
1591 | * An primitive has the same hashcode as its incomplete counterpart.
|
---|
1592 | */
|
---|
1593 | @Override public final int hashCode() {
|
---|
1594 | return (int)id;
|
---|
1595 | }
|
---|
1596 |
|
---|
1597 | }
|
---|