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.HashSet;
|
---|
13 | import java.util.LinkedHashSet;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
21 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
22 | import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
|
---|
23 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
24 | import org.openstreetmap.josm.gui.mappaint.StyleCache;
|
---|
25 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
26 | import org.openstreetmap.josm.tools.Predicate;
|
---|
27 | import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * An OSM primitive can be associated with a key/value pair. It can be created, deleted
|
---|
31 | * and updated within the OSM-Server.
|
---|
32 | *
|
---|
33 | * Although OsmPrimitive is designed as a base class, it is not to be meant to subclass
|
---|
34 | * it by any other than from the package {@link org.openstreetmap.josm.data.osm}. The available primitives are a fixed set that are given
|
---|
35 | * by the server environment and not an extendible data stuff.
|
---|
36 | *
|
---|
37 | * @author imi
|
---|
38 | */
|
---|
39 | abstract public class OsmPrimitive extends AbstractPrimitive implements Comparable<OsmPrimitive>, TemplateEngineDataProvider {
|
---|
40 | private static final String SPECIAL_VALUE_ID = "id";
|
---|
41 | private static final String SPECIAL_VALUE_LOCAL_NAME = "localname";
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * An object can be disabled by the filter mechanism.
|
---|
46 | * Then it will show in a shade of gray on the map or it is completely
|
---|
47 | * hidden from the view.
|
---|
48 | * Disabled objects usually cannot be selected or modified
|
---|
49 | * while the filter is active.
|
---|
50 | */
|
---|
51 | protected static final int FLAG_DISABLED = 1 << 4;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * This flag is only relevant if an object is disabled by the
|
---|
55 | * filter mechanism (i.e. FLAG_DISABLED is set).
|
---|
56 | * Then it indicates, whether it is completely hidden or
|
---|
57 | * just shown in gray color.
|
---|
58 | *
|
---|
59 | * When the primitive is not disabled, this flag should be
|
---|
60 | * unset as well (for efficient access).
|
---|
61 | */
|
---|
62 | protected static final int FLAG_HIDE_IF_DISABLED = 1 << 5;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * This flag is set if the primitive is a way and
|
---|
66 | * according to the tags, the direction of the way is important.
|
---|
67 | * (e.g. one way street.)
|
---|
68 | */
|
---|
69 | protected static final int FLAG_HAS_DIRECTIONS = 1 << 6;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * If the primitive is tagged.
|
---|
73 | * Some trivial tags like source=* are ignored here.
|
---|
74 | */
|
---|
75 | protected static final int FLAG_TAGGED = 1 << 7;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * This flag is only relevant if FLAG_HAS_DIRECTIONS is set.
|
---|
79 | * It shows, that direction of the arrows should be reversed.
|
---|
80 | * (E.g. oneway=-1.)
|
---|
81 | */
|
---|
82 | protected static final int FLAG_DIRECTION_REVERSED = 1 << 8;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * When hovering over ways and nodes in add mode, the
|
---|
86 | * "target" objects are visually highlighted. This flag indicates
|
---|
87 | * that the primitive is currently highlighted.
|
---|
88 | */
|
---|
89 | protected static final int FLAG_HIGHLIGHTED = 1 << 9;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
93 | * another collection of {@see OsmPrimitive}s. The result collection is a list.
|
---|
94 | *
|
---|
95 | * If <code>list</code> is null, replies an empty list.
|
---|
96 | *
|
---|
97 | * @param <T>
|
---|
98 | * @param list the original list
|
---|
99 | * @param type the type to filter for
|
---|
100 | * @return the sub-list of OSM primitives of type <code>type</code>
|
---|
101 | */
|
---|
102 | static public <T extends OsmPrimitive> List<T> getFilteredList(Collection<OsmPrimitive> list, Class<T> type) {
|
---|
103 | if (list == null) return Collections.emptyList();
|
---|
104 | List<T> ret = new LinkedList<T>();
|
---|
105 | for(OsmPrimitive p: list) {
|
---|
106 | if (type.isInstance(p)) {
|
---|
107 | ret.add(type.cast(p));
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Replies the sub-collection of {@see OsmPrimitive}s of type <code>type</code> present in
|
---|
115 | * another collection of {@see OsmPrimitive}s. The result collection is a set.
|
---|
116 | *
|
---|
117 | * If <code>list</code> is null, replies an empty set.
|
---|
118 | *
|
---|
119 | * @param <T>
|
---|
120 | * @param list the original collection
|
---|
121 | * @param type the type to filter for
|
---|
122 | * @return the sub-set of OSM primitives of type <code>type</code>
|
---|
123 | */
|
---|
124 | static public <T extends OsmPrimitive> LinkedHashSet<T> getFilteredSet(Collection<OsmPrimitive> set, Class<T> type) {
|
---|
125 | LinkedHashSet<T> ret = new LinkedHashSet<T>();
|
---|
126 | if (set != null) {
|
---|
127 | for(OsmPrimitive p: set) {
|
---|
128 | if (type.isInstance(p)) {
|
---|
129 | ret.add(type.cast(p));
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return ret;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Replies the collection of referring primitives for the primitives in <code>primitives</code>.
|
---|
138 | *
|
---|
139 | * @param primitives the collection of primitives.
|
---|
140 | * @return the collection of referring primitives for the primitives in <code>primitives</code>;
|
---|
141 | * empty set if primitives is null or if there are no referring primitives
|
---|
142 | */
|
---|
143 | static public Set<OsmPrimitive> getReferrer(Collection<? extends OsmPrimitive> primitives) {
|
---|
144 | HashSet<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
|
---|
145 | if (primitives == null || primitives.isEmpty()) return ret;
|
---|
146 | for (OsmPrimitive p: primitives) {
|
---|
147 | ret.addAll(p.getReferrers());
|
---|
148 | }
|
---|
149 | return ret;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Some predicates, that describe conditions on primitives.
|
---|
154 | */
|
---|
155 | public static final Predicate<OsmPrimitive> isUsablePredicate = new Predicate<OsmPrimitive>() {
|
---|
156 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
157 | return primitive.isUsable();
|
---|
158 | }
|
---|
159 | };
|
---|
160 |
|
---|
161 | public static final Predicate<OsmPrimitive> isSelectablePredicate = new Predicate<OsmPrimitive>() {
|
---|
162 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
163 | return primitive.isSelectable();
|
---|
164 | }
|
---|
165 | };
|
---|
166 |
|
---|
167 | public static final Predicate<OsmPrimitive> nonDeletedPredicate = new Predicate<OsmPrimitive>() {
|
---|
168 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
169 | return !primitive.isDeleted();
|
---|
170 | }
|
---|
171 | };
|
---|
172 |
|
---|
173 | public static final Predicate<OsmPrimitive> nonDeletedCompletePredicate = new Predicate<OsmPrimitive>() {
|
---|
174 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
175 | return !primitive.isDeleted() && !primitive.isIncomplete();
|
---|
176 | }
|
---|
177 | };
|
---|
178 |
|
---|
179 | public static final Predicate<OsmPrimitive> nonDeletedPhysicalPredicate = new Predicate<OsmPrimitive>() {
|
---|
180 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
181 | return !primitive.isDeleted() && !primitive.isIncomplete() && !(primitive instanceof Relation);
|
---|
182 | }
|
---|
183 | };
|
---|
184 |
|
---|
185 | public static final Predicate<OsmPrimitive> modifiedPredicate = new Predicate<OsmPrimitive>() {
|
---|
186 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
187 | return primitive.isModified();
|
---|
188 | }
|
---|
189 | };
|
---|
190 |
|
---|
191 | public static final Predicate<OsmPrimitive> nodePredicate = new Predicate<OsmPrimitive>() {
|
---|
192 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
193 | return primitive.getClass() == Node.class;
|
---|
194 | }
|
---|
195 | };
|
---|
196 |
|
---|
197 | public static final Predicate<OsmPrimitive> wayPredicate = new Predicate<OsmPrimitive>() {
|
---|
198 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
199 | return primitive.getClass() == Way.class;
|
---|
200 | }
|
---|
201 | };
|
---|
202 |
|
---|
203 | public static final Predicate<OsmPrimitive> relationPredicate = new Predicate<OsmPrimitive>() {
|
---|
204 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
205 | return primitive.getClass() == Relation.class;
|
---|
206 | }
|
---|
207 | };
|
---|
208 |
|
---|
209 | public static final Predicate<OsmPrimitive> allPredicate = new Predicate<OsmPrimitive>() {
|
---|
210 | @Override public boolean evaluate(OsmPrimitive primitive) {
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 | };
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Creates a new primitive for the given id.
|
---|
217 | *
|
---|
218 | * If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing.
|
---|
219 | * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
|
---|
220 | * positive number.
|
---|
221 | *
|
---|
222 | * @param id the id
|
---|
223 | * @param allowNegativeId
|
---|
224 | * @throws IllegalArgumentException thrown if id < 0 and allowNegativeId is false
|
---|
225 | */
|
---|
226 | protected OsmPrimitive(long id, boolean allowNegativeId) throws IllegalArgumentException {
|
---|
227 | if (allowNegativeId) {
|
---|
228 | this.id = id;
|
---|
229 | } else {
|
---|
230 | if (id < 0)
|
---|
231 | throw new IllegalArgumentException(MessageFormat.format("Expected ID >= 0. Got {0}.", id));
|
---|
232 | else if (id == 0) {
|
---|
233 | this.id = generateUniqueId();
|
---|
234 | } else {
|
---|
235 | this.id = id;
|
---|
236 | }
|
---|
237 |
|
---|
238 | }
|
---|
239 | this.version = 0;
|
---|
240 | this.setIncomplete(id > 0);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Creates a new primitive for the given id and version.
|
---|
245 | *
|
---|
246 | * If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing.
|
---|
247 | * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
|
---|
248 | * positive number.
|
---|
249 | *
|
---|
250 | * If id is not > 0 version is ignored and set to 0.
|
---|
251 | *
|
---|
252 | * @param id
|
---|
253 | * @param version
|
---|
254 | * @param allowNegativeId
|
---|
255 | * @throws IllegalArgumentException thrown if id < 0 and allowNegativeId is false
|
---|
256 | */
|
---|
257 | protected OsmPrimitive(long id, int version, boolean allowNegativeId) throws IllegalArgumentException {
|
---|
258 | this(id, allowNegativeId);
|
---|
259 | this.version = (id > 0 ? version : 0);
|
---|
260 | setIncomplete(id > 0 && version == 0);
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /*----------
|
---|
265 | * MAPPAINT
|
---|
266 | *--------*/
|
---|
267 | public StyleCache mappaintStyle = null;
|
---|
268 | public int mappaintCacheIdx;
|
---|
269 |
|
---|
270 | /* This should not be called from outside. Fixing the UI to add relevant
|
---|
271 | get/set functions calling this implicitely is preferred, so we can have
|
---|
272 | transparent cache handling in the future. */
|
---|
273 | public void clearCachedStyle()
|
---|
274 | {
|
---|
275 | mappaintStyle = null;
|
---|
276 | }
|
---|
277 | /* end of mappaint data */
|
---|
278 |
|
---|
279 | /*---------
|
---|
280 | * DATASET
|
---|
281 | *---------*/
|
---|
282 |
|
---|
283 | /** the parent dataset */
|
---|
284 | private DataSet dataSet;
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * This method should never ever by called from somewhere else than Dataset.addPrimitive or removePrimitive methods
|
---|
288 | * @param dataSet
|
---|
289 | */
|
---|
290 | void setDataset(DataSet dataSet) {
|
---|
291 | if (this.dataSet != null && dataSet != null && this.dataSet != dataSet)
|
---|
292 | throw new DataIntegrityProblemException("Primitive cannot be included in more than one Dataset");
|
---|
293 | this.dataSet = dataSet;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /**
|
---|
297 | *
|
---|
298 | * @return DataSet this primitive is part of.
|
---|
299 | */
|
---|
300 | public DataSet getDataSet() {
|
---|
301 | return dataSet;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Throws exception if primitive is not part of the dataset
|
---|
306 | */
|
---|
307 | public void checkDataset() {
|
---|
308 | if (dataSet == null)
|
---|
309 | throw new DataIntegrityProblemException("Primitive must be part of the dataset: " + toString());
|
---|
310 | }
|
---|
311 |
|
---|
312 | protected boolean writeLock() {
|
---|
313 | if (dataSet != null) {
|
---|
314 | dataSet.beginUpdate();
|
---|
315 | return true;
|
---|
316 | } else
|
---|
317 | return false;
|
---|
318 | }
|
---|
319 |
|
---|
320 | protected void writeUnlock(boolean locked) {
|
---|
321 | if (locked) {
|
---|
322 | // It shouldn't be possible for dataset to become null because method calling setDataset would need write lock which is owned by this thread
|
---|
323 | dataSet.endUpdate();
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Sets the id and the version of this primitive if it is known to the OSM API.
|
---|
329 | *
|
---|
330 | * Since we know the id and its version it can't be incomplete anymore. incomplete
|
---|
331 | * is set to false.
|
---|
332 | *
|
---|
333 | * @param id the id. > 0 required
|
---|
334 | * @param version the version > 0 required
|
---|
335 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
336 | * @throws IllegalArgumentException thrown if version <= 0
|
---|
337 | * @throws DataIntegrityProblemException If id is changed and primitive was already added to the dataset
|
---|
338 | */
|
---|
339 | @Override
|
---|
340 | public void setOsmId(long id, int version) {
|
---|
341 | boolean locked = writeLock();
|
---|
342 | try {
|
---|
343 | if (id <= 0)
|
---|
344 | throw new IllegalArgumentException(tr("ID > 0 expected. Got {0}.", id));
|
---|
345 | if (version <= 0)
|
---|
346 | throw new IllegalArgumentException(tr("Version > 0 expected. Got {0}.", version));
|
---|
347 | if (dataSet != null && id != this.id) {
|
---|
348 | DataSet datasetCopy = dataSet;
|
---|
349 | // Reindex primitive
|
---|
350 | datasetCopy.removePrimitive(this);
|
---|
351 | this.id = id;
|
---|
352 | datasetCopy.addPrimitive(this);
|
---|
353 | }
|
---|
354 | super.setOsmId(id, version);
|
---|
355 | } finally {
|
---|
356 | writeUnlock(locked);
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Clears the id and version known to the OSM API. The id and the version is set to 0.
|
---|
362 | * incomplete is set to false. It's preferred to use copy constructor with clearId set to true instead
|
---|
363 | * of calling this method.
|
---|
364 | *
|
---|
365 | * <strong>Caution</strong>: Do not use this method on primitives which are already added to a {@see DataSet}.
|
---|
366 | *
|
---|
367 | * @throws DataIntegrityProblemException If primitive was already added to the dataset
|
---|
368 | */
|
---|
369 | @Override
|
---|
370 | public void clearOsmId() {
|
---|
371 | if (dataSet != null)
|
---|
372 | throw new DataIntegrityProblemException("Method cannot be called after primitive was added to the dataset");
|
---|
373 | super.clearOsmId();
|
---|
374 | }
|
---|
375 |
|
---|
376 | @Override
|
---|
377 | public void setUser(User user) {
|
---|
378 | boolean locked = writeLock();
|
---|
379 | try {
|
---|
380 | super.setUser(user);
|
---|
381 | } finally {
|
---|
382 | writeUnlock(locked);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | @Override
|
---|
387 | public void setChangesetId(int changesetId) throws IllegalStateException, IllegalArgumentException {
|
---|
388 | boolean locked = writeLock();
|
---|
389 | try {
|
---|
390 | int old = this.changesetId;
|
---|
391 | super.setChangesetId(changesetId);
|
---|
392 | if (dataSet != null) {
|
---|
393 | dataSet.fireChangesetIdChanged(this, old, changesetId);
|
---|
394 | }
|
---|
395 | } finally {
|
---|
396 | writeUnlock(locked);
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 | @Override
|
---|
401 | public void setTimestamp(Date timestamp) {
|
---|
402 | boolean locked = writeLock();
|
---|
403 | try {
|
---|
404 | super.setTimestamp(timestamp);
|
---|
405 | } finally {
|
---|
406 | writeUnlock(locked);
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | /* -------
|
---|
412 | /* FLAGS
|
---|
413 | /* ------*/
|
---|
414 |
|
---|
415 | private void updateFlagsNoLock (int flag, boolean value) {
|
---|
416 | super.updateFlags(flag, value);
|
---|
417 | }
|
---|
418 |
|
---|
419 | @Override
|
---|
420 | protected final void updateFlags(int flag, boolean value) {
|
---|
421 | boolean locked = writeLock();
|
---|
422 | try {
|
---|
423 | updateFlagsNoLock(flag, value);
|
---|
424 | } finally {
|
---|
425 | writeUnlock(locked);
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Make the primitive disabled (e.g. if a filter applies).
|
---|
431 | * To enable the primitive again, use unsetDisabledState.
|
---|
432 | * @param hide if the primitive should be completely hidden from view or
|
---|
433 | * just shown in gray color.
|
---|
434 | */
|
---|
435 | public boolean setDisabledState(boolean hide) {
|
---|
436 | boolean locked = writeLock();
|
---|
437 | try {
|
---|
438 | int oldFlags = flags;
|
---|
439 | updateFlagsNoLock(FLAG_DISABLED, true);
|
---|
440 | updateFlagsNoLock(FLAG_HIDE_IF_DISABLED, hide);
|
---|
441 | return oldFlags != flags;
|
---|
442 | } finally {
|
---|
443 | writeUnlock(locked);
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Remove the disabled flag from the primitive.
|
---|
449 | * Afterwards, the primitive is displayed normally and can be selected
|
---|
450 | * again.
|
---|
451 | */
|
---|
452 | public boolean unsetDisabledState() {
|
---|
453 | boolean locked = writeLock();
|
---|
454 | try {
|
---|
455 | int oldFlags = flags;
|
---|
456 | updateFlagsNoLock(FLAG_DISABLED + FLAG_HIDE_IF_DISABLED, false);
|
---|
457 | return oldFlags != flags;
|
---|
458 | } finally {
|
---|
459 | writeUnlock(locked);
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Replies true, if this primitive is disabled. (E.g. a filter
|
---|
465 | * applies)
|
---|
466 | */
|
---|
467 | public boolean isDisabled() {
|
---|
468 | return (flags & FLAG_DISABLED) != 0;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * Replies true, if this primitive is disabled and marked as
|
---|
473 | * completely hidden on the map.
|
---|
474 | */
|
---|
475 | public boolean isDisabledAndHidden() {
|
---|
476 | return (((flags & FLAG_DISABLED) != 0) && ((flags & FLAG_HIDE_IF_DISABLED) != 0));
|
---|
477 | }
|
---|
478 |
|
---|
479 | @Deprecated
|
---|
480 | public boolean isFiltered() {
|
---|
481 | return isDisabledAndHidden();
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | public boolean isSelectable() {
|
---|
486 | return (flags & (FLAG_DELETED + FLAG_INCOMPLETE + FLAG_DISABLED + FLAG_HIDE_IF_DISABLED)) == 0;
|
---|
487 | }
|
---|
488 |
|
---|
489 | public boolean isDrawable() {
|
---|
490 | return (flags & (FLAG_DELETED + FLAG_INCOMPLETE + FLAG_HIDE_IF_DISABLED)) == 0;
|
---|
491 | }
|
---|
492 |
|
---|
493 | @Override
|
---|
494 | public void setVisible(boolean visible) throws IllegalStateException {
|
---|
495 | boolean locked = writeLock();
|
---|
496 | try {
|
---|
497 | super.setVisible(visible);
|
---|
498 | } finally {
|
---|
499 | writeUnlock(locked);
|
---|
500 | }
|
---|
501 | }
|
---|
502 |
|
---|
503 | @Override
|
---|
504 | public void setDeleted(boolean deleted) {
|
---|
505 | boolean locked = writeLock();
|
---|
506 | try {
|
---|
507 | super.setDeleted(deleted);
|
---|
508 | if (dataSet != null) {
|
---|
509 | if (deleted) {
|
---|
510 | dataSet.firePrimitivesRemoved(Collections.singleton(this), false);
|
---|
511 | } else {
|
---|
512 | dataSet.firePrimitivesAdded(Collections.singleton(this), false);
|
---|
513 | }
|
---|
514 | }
|
---|
515 | } finally {
|
---|
516 | writeUnlock(locked);
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | @Override
|
---|
521 | protected void setIncomplete(boolean incomplete) {
|
---|
522 | boolean locked = writeLock();
|
---|
523 | try {
|
---|
524 | if (dataSet != null && incomplete != this.isIncomplete()) {
|
---|
525 | if (incomplete) {
|
---|
526 | dataSet.firePrimitivesRemoved(Collections.singletonList(this), true);
|
---|
527 | } else {
|
---|
528 | dataSet.firePrimitivesAdded(Collections.singletonList(this), true);
|
---|
529 | }
|
---|
530 | }
|
---|
531 | super.setIncomplete(incomplete);
|
---|
532 | } finally {
|
---|
533 | writeUnlock(locked);
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | public boolean isSelected() {
|
---|
538 | return dataSet != null && dataSet.isSelected(this);
|
---|
539 | }
|
---|
540 |
|
---|
541 | public boolean isMemberOfSelected() {
|
---|
542 | if (referrers == null)
|
---|
543 | return false;
|
---|
544 | if (referrers instanceof OsmPrimitive)
|
---|
545 | return referrers instanceof Relation && ((OsmPrimitive) referrers).isSelected();
|
---|
546 | for (OsmPrimitive ref : (OsmPrimitive[]) referrers) {
|
---|
547 | if (ref instanceof Relation && ref.isSelected())
|
---|
548 | return true;
|
---|
549 | }
|
---|
550 | return false;
|
---|
551 | }
|
---|
552 |
|
---|
553 | public void setHighlighted(boolean highlighted) {
|
---|
554 | if (isHighlighted() != highlighted) {
|
---|
555 | updateFlags(FLAG_HIGHLIGHTED, highlighted);
|
---|
556 | if (dataSet != null) {
|
---|
557 | dataSet.fireHighlightingChanged(this);
|
---|
558 | }
|
---|
559 | }
|
---|
560 | }
|
---|
561 |
|
---|
562 | public boolean isHighlighted() {
|
---|
563 | return (flags & FLAG_HIGHLIGHTED) != 0;
|
---|
564 | }
|
---|
565 |
|
---|
566 | /*----------------------------------
|
---|
567 | * UNINTERESTING AND DIRECTION KEYS
|
---|
568 | *----------------------------------*/
|
---|
569 |
|
---|
570 |
|
---|
571 | private static volatile Collection<String> uninteresting = null;
|
---|
572 | /**
|
---|
573 | * Contains a list of "uninteresting" keys that do not make an object
|
---|
574 | * "tagged". Entries that end with ':' are causing a whole namespace to be considered
|
---|
575 | * "uninteresting". Only the first level namespace is considered.
|
---|
576 | * Initialized by isUninterestingKey()
|
---|
577 | */
|
---|
578 | public static Collection<String> getUninterestingKeys() {
|
---|
579 | if (uninteresting == null) {
|
---|
580 | uninteresting = Main.pref.getCollection("tags.uninteresting",
|
---|
581 | Arrays.asList(new String[]{"source", "source_ref", "source:", "note", "comment",
|
---|
582 | "converted_by", "created_by", "watch", "watch:", "fixme", "FIXME",
|
---|
583 | "description", "attribution"}));
|
---|
584 | }
|
---|
585 | return uninteresting;
|
---|
586 | }
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * Returns true if key is considered "uninteresting".
|
---|
590 | */
|
---|
591 | public static boolean isUninterestingKey(String key) {
|
---|
592 | getUninterestingKeys();
|
---|
593 | if (uninteresting.contains(key))
|
---|
594 | return true;
|
---|
595 | int pos = key.indexOf(':');
|
---|
596 | if (pos > 0)
|
---|
597 | return uninteresting.contains(key.substring(0, pos + 1));
|
---|
598 | return false;
|
---|
599 | }
|
---|
600 |
|
---|
601 | private static volatile Match directionKeys = null;
|
---|
602 | private static volatile Match reversedDirectionKeys = null;
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * Contains a list of direction-dependent keys that make an object
|
---|
606 | * direction dependent.
|
---|
607 | * Initialized by checkDirectionTagged()
|
---|
608 | */
|
---|
609 | static {
|
---|
610 | // Legacy support - convert list of keys to search pattern
|
---|
611 | if (Main.pref.isCollection("tags.direction", false)) {
|
---|
612 | System.out.println("Collection of keys in tags.direction is no longer supported, value will converted to search pattern");
|
---|
613 | Collection<String> keys = Main.pref.getCollection("tags.direction", null);
|
---|
614 | StringBuilder builder = new StringBuilder();
|
---|
615 | for (String key:keys) {
|
---|
616 | builder.append(key);
|
---|
617 | builder.append("=* | ");
|
---|
618 | }
|
---|
619 | builder.delete(builder.length() - 3, builder.length());
|
---|
620 | Main.pref.put("tags.direction", builder.toString());
|
---|
621 | }
|
---|
622 |
|
---|
623 | // FIXME: incline=\"-*\" search pattern does not work.
|
---|
624 | String reversedDirectionDefault = "oneway=\"-1\" | incline=down | incline=\"-*\"";
|
---|
625 |
|
---|
626 | String directionDefault = "oneway? | incline=* | aerialway=* | "+
|
---|
627 | "waterway=stream | waterway=river | waterway=canal | waterway=drain | waterway=rapids | "+
|
---|
628 | "\"piste:type\"=downhill | \"piste:type\"=sled | man_made=\"piste:halfpipe\" | "+
|
---|
629 | "junction=roundabout";
|
---|
630 |
|
---|
631 | try {
|
---|
632 | reversedDirectionKeys = SearchCompiler.compile(Main.pref.get("tags.reversed_direction", reversedDirectionDefault), false, false);
|
---|
633 | } catch (ParseError e) {
|
---|
634 | System.err.println("Unable to compile pattern for tags.reversed_direction, trying default pattern: " + e.getMessage());
|
---|
635 |
|
---|
636 | try {
|
---|
637 | reversedDirectionKeys = SearchCompiler.compile(reversedDirectionDefault, false, false);
|
---|
638 | } catch (ParseError e2) {
|
---|
639 | throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage());
|
---|
640 | }
|
---|
641 | }
|
---|
642 | try {
|
---|
643 | directionKeys = SearchCompiler.compile(Main.pref.get("tags.direction", directionDefault), false, false);
|
---|
644 | } catch (ParseError e) {
|
---|
645 | System.err.println("Unable to compile pattern for tags.direction, trying default pattern: " + e.getMessage());
|
---|
646 |
|
---|
647 | try {
|
---|
648 | directionKeys = SearchCompiler.compile(directionDefault, false, false);
|
---|
649 | } catch (ParseError e2) {
|
---|
650 | throw new AssertionError("Unable to compile default pattern for direction keys: " + e2.getMessage());
|
---|
651 | }
|
---|
652 | }
|
---|
653 | }
|
---|
654 |
|
---|
655 | private void updateTagged() {
|
---|
656 | if (keys != null) {
|
---|
657 | for (String key: keySet()) {
|
---|
658 | if (!isUninterestingKey(key)) {
|
---|
659 | updateFlagsNoLock(FLAG_TAGGED, true);
|
---|
660 | return;
|
---|
661 | }
|
---|
662 | }
|
---|
663 | }
|
---|
664 | updateFlagsNoLock(FLAG_TAGGED, false);
|
---|
665 | }
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * true if this object is considered "tagged". To be "tagged", an object
|
---|
669 | * must have one or more "interesting" tags. "created_by" and "source"
|
---|
670 | * are typically considered "uninteresting" and do not make an object
|
---|
671 | * "tagged".
|
---|
672 | */
|
---|
673 | public boolean isTagged() {
|
---|
674 | return (flags & FLAG_TAGGED) != 0;
|
---|
675 | }
|
---|
676 |
|
---|
677 | private void updateDirectionFlags() {
|
---|
678 | boolean hasDirections = false;
|
---|
679 | boolean directionReversed = false;
|
---|
680 | if (reversedDirectionKeys.match(this)) {
|
---|
681 | hasDirections = true;
|
---|
682 | directionReversed = true;
|
---|
683 | }
|
---|
684 | if (directionKeys.match(this)) {
|
---|
685 | hasDirections = true;
|
---|
686 | }
|
---|
687 |
|
---|
688 | updateFlagsNoLock(FLAG_DIRECTION_REVERSED, directionReversed);
|
---|
689 | updateFlagsNoLock(FLAG_HAS_DIRECTIONS, hasDirections);
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * true if this object has direction dependent tags (e.g. oneway)
|
---|
694 | */
|
---|
695 | public boolean hasDirectionKeys() {
|
---|
696 | return (flags & FLAG_HAS_DIRECTIONS) != 0;
|
---|
697 | }
|
---|
698 |
|
---|
699 | public boolean reversedDirection() {
|
---|
700 | return (flags & FLAG_DIRECTION_REVERSED) != 0;
|
---|
701 | }
|
---|
702 |
|
---|
703 | /*------------
|
---|
704 | * Keys handling
|
---|
705 | ------------*/
|
---|
706 |
|
---|
707 | @Override
|
---|
708 | public final void setKeys(Map<String, String> keys) {
|
---|
709 | boolean locked = writeLock();
|
---|
710 | try {
|
---|
711 | super.setKeys(keys);
|
---|
712 | } finally {
|
---|
713 | writeUnlock(locked);
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | @Override
|
---|
718 | public final void put(String key, String value) {
|
---|
719 | boolean locked = writeLock();
|
---|
720 | try {
|
---|
721 | super.put(key, value);
|
---|
722 | } finally {
|
---|
723 | writeUnlock(locked);
|
---|
724 | }
|
---|
725 | }
|
---|
726 |
|
---|
727 | @Override
|
---|
728 | public final void remove(String key) {
|
---|
729 | boolean locked = writeLock();
|
---|
730 | try {
|
---|
731 | super.remove(key);
|
---|
732 | } finally {
|
---|
733 | writeUnlock(locked);
|
---|
734 | }
|
---|
735 | }
|
---|
736 |
|
---|
737 | @Override
|
---|
738 | public final void removeAll() {
|
---|
739 | boolean locked = writeLock();
|
---|
740 | try {
|
---|
741 | super.removeAll();
|
---|
742 | } finally {
|
---|
743 | writeUnlock(locked);
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | @Override
|
---|
748 | protected final void keysChangedImpl(Map<String, String> originalKeys) {
|
---|
749 | clearCachedStyle();
|
---|
750 | if (dataSet != null) {
|
---|
751 | for (OsmPrimitive ref : getReferrers()) {
|
---|
752 | ref.clearCachedStyle();
|
---|
753 | }
|
---|
754 | }
|
---|
755 | updateDirectionFlags();
|
---|
756 | updateTagged();
|
---|
757 | if (dataSet != null) {
|
---|
758 | dataSet.fireTagsChanged(this, originalKeys);
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | /*------------
|
---|
763 | * Referrers
|
---|
764 | ------------*/
|
---|
765 |
|
---|
766 | private Object referrers;
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * Add new referrer. If referrer is already included then no action is taken
|
---|
770 | * @param referrer
|
---|
771 | */
|
---|
772 | protected void addReferrer(OsmPrimitive referrer) {
|
---|
773 | // Based on methods from josm-ng
|
---|
774 | if (referrers == null) {
|
---|
775 | referrers = referrer;
|
---|
776 | } else if (referrers instanceof OsmPrimitive) {
|
---|
777 | if (referrers != referrer) {
|
---|
778 | referrers = new OsmPrimitive[] { (OsmPrimitive)referrers, referrer };
|
---|
779 | }
|
---|
780 | } else {
|
---|
781 | for (OsmPrimitive primitive:(OsmPrimitive[])referrers) {
|
---|
782 | if (primitive == referrer)
|
---|
783 | return;
|
---|
784 | }
|
---|
785 | OsmPrimitive[] orig = (OsmPrimitive[])referrers;
|
---|
786 | OsmPrimitive[] bigger = new OsmPrimitive[orig.length+1];
|
---|
787 | System.arraycopy(orig, 0, bigger, 0, orig.length);
|
---|
788 | bigger[orig.length] = referrer;
|
---|
789 | referrers = bigger;
|
---|
790 | }
|
---|
791 | }
|
---|
792 |
|
---|
793 | /**
|
---|
794 | * Remove referrer. No action is taken if referrer is not registered
|
---|
795 | * @param referrer
|
---|
796 | */
|
---|
797 | protected void removeReferrer(OsmPrimitive referrer) {
|
---|
798 | // Based on methods from josm-ng
|
---|
799 | if (referrers instanceof OsmPrimitive) {
|
---|
800 | if (referrers == referrer) {
|
---|
801 | referrers = null;
|
---|
802 | }
|
---|
803 | } else if (referrers instanceof OsmPrimitive[]) {
|
---|
804 | OsmPrimitive[] orig = (OsmPrimitive[])referrers;
|
---|
805 | int idx = -1;
|
---|
806 | for (int i=0; i<orig.length; i++) {
|
---|
807 | if (orig[i] == referrer) {
|
---|
808 | idx = i;
|
---|
809 | break;
|
---|
810 | }
|
---|
811 | }
|
---|
812 | if (idx == -1)
|
---|
813 | return;
|
---|
814 |
|
---|
815 | if (orig.length == 2) {
|
---|
816 | referrers = orig[1-idx]; // idx is either 0 or 1, take the other
|
---|
817 | } else { // downsize the array
|
---|
818 | OsmPrimitive[] smaller = new OsmPrimitive[orig.length-1];
|
---|
819 | System.arraycopy(orig, 0, smaller, 0, idx);
|
---|
820 | System.arraycopy(orig, idx+1, smaller, idx, smaller.length-idx);
|
---|
821 | referrers = smaller;
|
---|
822 | }
|
---|
823 | }
|
---|
824 | }
|
---|
825 | /**
|
---|
826 | * Find primitives that reference this primitive. Returns only primitives that are included in the same
|
---|
827 | * dataset as this primitive. <br>
|
---|
828 | *
|
---|
829 | * For example following code will add wnew as referer to all nodes of existingWay, but this method will
|
---|
830 | * not return wnew because it's not part of the dataset <br>
|
---|
831 | *
|
---|
832 | * <code>Way wnew = new Way(existingWay)</code>
|
---|
833 | *
|
---|
834 | * @return a collection of all primitives that reference this primitive.
|
---|
835 | */
|
---|
836 |
|
---|
837 | public final List<OsmPrimitive> getReferrers() {
|
---|
838 | // Method copied from OsmPrimitive in josm-ng
|
---|
839 | // Returns only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
840 | // when way is cloned
|
---|
841 | checkDataset();
|
---|
842 | Object referrers = this.referrers;
|
---|
843 | List<OsmPrimitive> result = new ArrayList<OsmPrimitive>();
|
---|
844 | if (referrers != null) {
|
---|
845 | if (referrers instanceof OsmPrimitive) {
|
---|
846 | OsmPrimitive ref = (OsmPrimitive)referrers;
|
---|
847 | if (ref.dataSet == dataSet) {
|
---|
848 | result.add(ref);
|
---|
849 | }
|
---|
850 | } else {
|
---|
851 | for (OsmPrimitive o:(OsmPrimitive[])referrers) {
|
---|
852 | if (dataSet == o.dataSet) {
|
---|
853 | result.add(o);
|
---|
854 | }
|
---|
855 | }
|
---|
856 | }
|
---|
857 | }
|
---|
858 | return result;
|
---|
859 | }
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * <p>Visits {@code visitor} for all referrers.</p>
|
---|
863 | *
|
---|
864 | * @param visitor the visitor. Ignored, if null.
|
---|
865 | */
|
---|
866 | public void visitReferrers(Visitor visitor){
|
---|
867 | if (visitor == null) return;
|
---|
868 | if (this.referrers == null)
|
---|
869 | return;
|
---|
870 | else if (this.referrers instanceof OsmPrimitive) {
|
---|
871 | OsmPrimitive ref = (OsmPrimitive) this.referrers;
|
---|
872 | if (ref.dataSet == dataSet) {
|
---|
873 | ref.visit(visitor);
|
---|
874 | }
|
---|
875 | } else if (this.referrers instanceof OsmPrimitive[]) {
|
---|
876 | OsmPrimitive[] refs = (OsmPrimitive[]) this.referrers;
|
---|
877 | for (OsmPrimitive ref: refs) {
|
---|
878 | if (ref.dataSet == dataSet) {
|
---|
879 | ref.visit(visitor);
|
---|
880 | }
|
---|
881 | }
|
---|
882 | }
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | Return true, if this primitive is referred by at least n ways
|
---|
887 | @param n Minimal number of ways to return true. Must be positive
|
---|
888 | */
|
---|
889 | public final boolean isReferredByWays(int n) {
|
---|
890 | // Count only referrers that are members of the same dataset (primitive can have some fake references, for example
|
---|
891 | // when way is cloned
|
---|
892 | Object referrers = this.referrers;
|
---|
893 | if (referrers == null) return false;
|
---|
894 | checkDataset();
|
---|
895 | if (referrers instanceof OsmPrimitive)
|
---|
896 | return n<=1 && referrers instanceof Way && ((OsmPrimitive)referrers).dataSet == dataSet;
|
---|
897 | else {
|
---|
898 | int counter=0;
|
---|
899 | for (OsmPrimitive o : (OsmPrimitive[])referrers) {
|
---|
900 | if (dataSet == o.dataSet && o instanceof Way) {
|
---|
901 | if (++counter >= n)
|
---|
902 | return true;
|
---|
903 | }
|
---|
904 | }
|
---|
905 | return false;
|
---|
906 | }
|
---|
907 | }
|
---|
908 |
|
---|
909 |
|
---|
910 | /*-----------------
|
---|
911 | * OTHER METHODS
|
---|
912 | *----------------/
|
---|
913 |
|
---|
914 | /**
|
---|
915 | * Implementation of the visitor scheme. Subclasses have to call the correct
|
---|
916 | * visitor function.
|
---|
917 | * @param visitor The visitor from which the visit() function must be called.
|
---|
918 | */
|
---|
919 | abstract public void visit(Visitor visitor);
|
---|
920 |
|
---|
921 | /**
|
---|
922 | * Get and write all attributes from the parameter. Does not fire any listener, so
|
---|
923 | * use this only in the data initializing phase
|
---|
924 | */
|
---|
925 | public void cloneFrom(OsmPrimitive other) {
|
---|
926 | // write lock is provided by subclasses
|
---|
927 | if (id != other.id && dataSet != null)
|
---|
928 | throw new DataIntegrityProblemException("Osm id cannot be changed after primitive was added to the dataset");
|
---|
929 |
|
---|
930 | super.cloneFrom(other);
|
---|
931 | clearCachedStyle();
|
---|
932 | }
|
---|
933 |
|
---|
934 | /**
|
---|
935 | * Merges the technical and semantical attributes from <code>other</code> onto this.
|
---|
936 | *
|
---|
937 | * Both this and other must be new, or both must be assigned an OSM ID. If both this and <code>other</code>
|
---|
938 | * have an assigend OSM id, the IDs have to be the same.
|
---|
939 | *
|
---|
940 | * @param other the other primitive. Must not be null.
|
---|
941 | * @throws IllegalArgumentException thrown if other is null.
|
---|
942 | * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not
|
---|
943 | * @throws DataIntegrityProblemException thrown if other isn't new and other.getId() != this.getId()
|
---|
944 | */
|
---|
945 | public void mergeFrom(OsmPrimitive other) {
|
---|
946 | boolean locked = writeLock();
|
---|
947 | try {
|
---|
948 | CheckParameterUtil.ensureParameterNotNull(other, "other");
|
---|
949 | if (other.isNew() ^ isNew())
|
---|
950 | throw new DataIntegrityProblemException(tr("Cannot merge because either of the participating primitives is new and the other is not"));
|
---|
951 | if (! other.isNew() && other.getId() != id)
|
---|
952 | throw new DataIntegrityProblemException(tr("Cannot merge primitives with different ids. This id is {0}, the other is {1}", id, other.getId()));
|
---|
953 |
|
---|
954 | setKeys(other.getKeys());
|
---|
955 | timestamp = other.timestamp;
|
---|
956 | version = other.version;
|
---|
957 | setIncomplete(other.isIncomplete());
|
---|
958 | flags = other.flags;
|
---|
959 | user= other.user;
|
---|
960 | changesetId = other.changesetId;
|
---|
961 | } finally {
|
---|
962 | writeUnlock(locked);
|
---|
963 | }
|
---|
964 | }
|
---|
965 |
|
---|
966 | /**
|
---|
967 | * Replies true if this primitive and other are equal with respect to their
|
---|
968 | * semantic attributes.
|
---|
969 | * <ol>
|
---|
970 | * <li>equal id</ol>
|
---|
971 | * <li>both are complete or both are incomplete</li>
|
---|
972 | * <li>both have the same tags</li>
|
---|
973 | * </ol>
|
---|
974 | * @param other
|
---|
975 | * @return true if this primitive and other are equal with respect to their
|
---|
976 | * semantic attributes.
|
---|
977 | */
|
---|
978 | public boolean hasEqualSemanticAttributes(OsmPrimitive other) {
|
---|
979 | if (!isNew() && id != other.id)
|
---|
980 | return false;
|
---|
981 | if (isIncomplete() && ! other.isIncomplete() || !isIncomplete() && other.isIncomplete())
|
---|
982 | return false;
|
---|
983 | // can't do an equals check on the internal keys array because it is not ordered
|
---|
984 | //
|
---|
985 | return hasSameTags(other);
|
---|
986 | }
|
---|
987 |
|
---|
988 | /**
|
---|
989 | * Replies true if this primitive and other are equal with respect to their
|
---|
990 | * technical attributes. The attributes:
|
---|
991 | * <ol>
|
---|
992 | * <li>deleted</ol>
|
---|
993 | * <li>modified</ol>
|
---|
994 | * <li>timestamp</ol>
|
---|
995 | * <li>version</ol>
|
---|
996 | * <li>visible</ol>
|
---|
997 | * <li>user</ol>
|
---|
998 | * </ol>
|
---|
999 | * have to be equal
|
---|
1000 | * @param other the other primitive
|
---|
1001 | * @return true if this primitive and other are equal with respect to their
|
---|
1002 | * technical attributes
|
---|
1003 | */
|
---|
1004 | public boolean hasEqualTechnicalAttributes(OsmPrimitive other) {
|
---|
1005 | if (other == null) return false;
|
---|
1006 |
|
---|
1007 | return
|
---|
1008 | isDeleted() == other.isDeleted()
|
---|
1009 | && isModified() == other.isModified()
|
---|
1010 | && timestamp == other.timestamp
|
---|
1011 | && version == other.version
|
---|
1012 | && isVisible() == other.isVisible()
|
---|
1013 | && (user == null ? other.user==null : user==other.user)
|
---|
1014 | && changesetId == other.changesetId;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | /**
|
---|
1018 | * Loads (clone) this primitive from provided PrimitiveData
|
---|
1019 | * @param data
|
---|
1020 | */
|
---|
1021 | public void load(PrimitiveData data) {
|
---|
1022 | // Write lock is provided by subclasses
|
---|
1023 | setKeys(data.getKeys());
|
---|
1024 | setTimestamp(data.getTimestamp());
|
---|
1025 | user = data.getUser();
|
---|
1026 | setChangesetId(data.getChangesetId());
|
---|
1027 | setDeleted(data.isDeleted());
|
---|
1028 | setModified(data.isModified());
|
---|
1029 | setIncomplete(data.isIncomplete());
|
---|
1030 | version = data.getVersion();
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /**
|
---|
1034 | * Save parameters of this primitive to the transport object
|
---|
1035 | * @return
|
---|
1036 | */
|
---|
1037 | public abstract PrimitiveData save();
|
---|
1038 |
|
---|
1039 | protected void saveCommonAttributes(PrimitiveData data) {
|
---|
1040 | data.setId(id);
|
---|
1041 | data.setKeys(getKeys());
|
---|
1042 | data.setTimestamp(getTimestamp());
|
---|
1043 | data.setUser(user);
|
---|
1044 | data.setDeleted(isDeleted());
|
---|
1045 | data.setModified(isModified());
|
---|
1046 | data.setVisible(isVisible());
|
---|
1047 | data.setIncomplete(isIncomplete());
|
---|
1048 | data.setChangesetId(changesetId);
|
---|
1049 | data.setVersion(version);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | public abstract BBox getBBox();
|
---|
1053 |
|
---|
1054 | /**
|
---|
1055 | * Called by Dataset to update cached position information of primitive (bbox, cached EarthNorth, ...)
|
---|
1056 | */
|
---|
1057 | public abstract void updatePosition();
|
---|
1058 |
|
---|
1059 | /*----------------
|
---|
1060 | * OBJECT METHODS
|
---|
1061 | *---------------*/
|
---|
1062 |
|
---|
1063 | @Override
|
---|
1064 | protected String getFlagsAsString() {
|
---|
1065 | StringBuilder builder = new StringBuilder(super.getFlagsAsString());
|
---|
1066 |
|
---|
1067 | if (isDisabled()) {
|
---|
1068 | if (isDisabledAndHidden()) {
|
---|
1069 | builder.append("h");
|
---|
1070 | } else {
|
---|
1071 | builder.append("d");
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 | if (isTagged()) {
|
---|
1075 | builder.append("T");
|
---|
1076 | }
|
---|
1077 | if (hasDirectionKeys()) {
|
---|
1078 | if (reversedDirection()) {
|
---|
1079 | builder.append("<");
|
---|
1080 | } else {
|
---|
1081 | builder.append(">");
|
---|
1082 | }
|
---|
1083 | }
|
---|
1084 | return builder.toString();
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | /**
|
---|
1088 | * Equal, if the id (and class) is equal.
|
---|
1089 | *
|
---|
1090 | * An primitive is equal to its incomplete counter part.
|
---|
1091 | */
|
---|
1092 | @Override public boolean equals(Object obj) {
|
---|
1093 | if (obj instanceof OsmPrimitive)
|
---|
1094 | return ((OsmPrimitive)obj).id == id && obj.getClass() == getClass();
|
---|
1095 | return false;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.
|
---|
1100 | *
|
---|
1101 | * An primitive has the same hashcode as its incomplete counterpart.
|
---|
1102 | */
|
---|
1103 | @Override public final int hashCode() {
|
---|
1104 | return (int)id;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Replies the display name of a primitive formatted by <code>formatter</code>
|
---|
1109 | *
|
---|
1110 | * @return the display name
|
---|
1111 | */
|
---|
1112 | public abstract String getDisplayName(NameFormatter formatter);
|
---|
1113 |
|
---|
1114 | @Override
|
---|
1115 | public Collection<String> getTemplateKeys() {
|
---|
1116 | Collection<String> keySet = keySet();
|
---|
1117 | List<String> result = new ArrayList<String>(keySet.size() + 2);
|
---|
1118 | result.add(SPECIAL_VALUE_ID);
|
---|
1119 | result.add(SPECIAL_VALUE_LOCAL_NAME);
|
---|
1120 | result.addAll(keySet);
|
---|
1121 | return result;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | @Override
|
---|
1125 | public Object getTemplateValue(String name, boolean special) {
|
---|
1126 | if (special) {
|
---|
1127 | String lc = name.toLowerCase();
|
---|
1128 | if (SPECIAL_VALUE_ID.equals(lc))
|
---|
1129 | return getId();
|
---|
1130 | else if (SPECIAL_VALUE_LOCAL_NAME.equals(lc))
|
---|
1131 | return getLocalName();
|
---|
1132 | else
|
---|
1133 | return null;
|
---|
1134 |
|
---|
1135 | } else
|
---|
1136 | return getIgnoreCase(name);
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | @Override
|
---|
1140 | public boolean evaluateCondition(Match condition) {
|
---|
1141 | return condition.match(this);
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | }
|
---|