1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.geom.Area;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.LinkedHashSet;
|
---|
13 | import java.util.LinkedList;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.Objects;
|
---|
17 | import java.util.Set;
|
---|
18 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
19 | import java.util.concurrent.atomic.AtomicBoolean;
|
---|
20 | import java.util.concurrent.locks.Lock;
|
---|
21 | import java.util.concurrent.locks.ReadWriteLock;
|
---|
22 | import java.util.concurrent.locks.ReentrantReadWriteLock;
|
---|
23 | import java.util.function.Function;
|
---|
24 | import java.util.function.Predicate;
|
---|
25 | import java.util.function.Supplier;
|
---|
26 | import java.util.stream.Collectors;
|
---|
27 | import java.util.stream.Stream;
|
---|
28 |
|
---|
29 | import org.openstreetmap.josm.data.APIDataSet.APIOperation;
|
---|
30 | import org.openstreetmap.josm.data.Bounds;
|
---|
31 | import org.openstreetmap.josm.data.DataSource;
|
---|
32 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
33 | import org.openstreetmap.josm.data.conflict.ConflictCollection;
|
---|
34 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
35 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
36 | import org.openstreetmap.josm.data.gpx.GpxData.XMLNamespace;
|
---|
37 | import org.openstreetmap.josm.data.osm.DataSelectionListener.SelectionAddEvent;
|
---|
38 | import org.openstreetmap.josm.data.osm.DataSelectionListener.SelectionChangeEvent;
|
---|
39 | import org.openstreetmap.josm.data.osm.DataSelectionListener.SelectionRemoveEvent;
|
---|
40 | import org.openstreetmap.josm.data.osm.DataSelectionListener.SelectionReplaceEvent;
|
---|
41 | import org.openstreetmap.josm.data.osm.DataSelectionListener.SelectionToggleEvent;
|
---|
42 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
43 | import org.openstreetmap.josm.data.osm.event.ChangesetIdChangedEvent;
|
---|
44 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
45 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
46 | import org.openstreetmap.josm.data.osm.event.DataSourceAddedEvent;
|
---|
47 | import org.openstreetmap.josm.data.osm.event.DataSourceRemovedEvent;
|
---|
48 | import org.openstreetmap.josm.data.osm.event.FilterChangedEvent;
|
---|
49 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
50 | import org.openstreetmap.josm.data.osm.event.PrimitiveFlagsChangedEvent;
|
---|
51 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
52 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
53 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
54 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
55 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
56 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
57 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
58 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
59 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
60 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
61 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
62 | import org.openstreetmap.josm.tools.ListenerList;
|
---|
63 | import org.openstreetmap.josm.tools.Logging;
|
---|
64 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * DataSet is the data behind the application. It can consists of only a few points up to the whole
|
---|
68 | * osm database. DataSet's can be merged together, saved, (up/down/disk)loaded etc.
|
---|
69 | *
|
---|
70 | * Note that DataSet is not an osm-primitive and so has no key association but a few members to
|
---|
71 | * store some information.
|
---|
72 | *
|
---|
73 | * Dataset is threadsafe - accessing Dataset simultaneously from different threads should never
|
---|
74 | * lead to data corruption or ConcurrentModificationException. However when for example one thread
|
---|
75 | * removes primitive and other thread try to add another primitive referring to the removed primitive,
|
---|
76 | * DataIntegrityException will occur.
|
---|
77 | *
|
---|
78 | * To prevent such situations, read/write lock is provided. While read lock is used, it's guaranteed that
|
---|
79 | * Dataset will not change. Sample usage:
|
---|
80 | * <code>
|
---|
81 | * ds.getReadLock().lock();
|
---|
82 | * try {
|
---|
83 | * // .. do something with dataset
|
---|
84 | * } finally {
|
---|
85 | * ds.getReadLock().unlock();
|
---|
86 | * }
|
---|
87 | * </code>
|
---|
88 | *
|
---|
89 | * Write lock should be used in case of bulk operations. In addition to ensuring that other threads can't
|
---|
90 | * use dataset in the middle of modifications it also stops sending of dataset events. That's good for performance
|
---|
91 | * reasons - GUI can be updated after all changes are done.
|
---|
92 | * Sample usage:
|
---|
93 | * <code>
|
---|
94 | * ds.beginUpdate()
|
---|
95 | * try {
|
---|
96 | * // .. do modifications
|
---|
97 | * } finally {
|
---|
98 | * ds.endUpdate();
|
---|
99 | * }
|
---|
100 | * </code>
|
---|
101 | *
|
---|
102 | * Note that it is not necessary to call beginUpdate/endUpdate for every dataset modification - dataset will get locked
|
---|
103 | * automatically.
|
---|
104 | *
|
---|
105 | * Note that locks cannot be upgraded - if one threads use read lock and and then write lock, dead lock will occur - see #5814 for
|
---|
106 | * sample ticket
|
---|
107 | *
|
---|
108 | * @author imi
|
---|
109 | */
|
---|
110 | public final class DataSet implements OsmData<OsmPrimitive, Node, Way, Relation>, ProjectionChangeListener {
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Maximum number of events that can be fired between beginUpdate/endUpdate to be send as single events (ie without DatasetChangedEvent)
|
---|
114 | */
|
---|
115 | private static final int MAX_SINGLE_EVENTS = 30;
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Maximum number of events to kept between beginUpdate/endUpdate. When more events are created, that simple DatasetChangedEvent is sent)
|
---|
119 | */
|
---|
120 | private static final int MAX_EVENTS = 1000;
|
---|
121 |
|
---|
122 | private final QuadBucketPrimitiveStore<Node, Way, Relation> store = new QuadBucketPrimitiveStore<>();
|
---|
123 |
|
---|
124 | private final Storage<OsmPrimitive> allPrimitives = new Storage<>(new Storage.PrimitiveIdHash(), true);
|
---|
125 | private final Map<PrimitiveId, OsmPrimitive> primitivesMap = allPrimitives
|
---|
126 | .foreignKey(new Storage.PrimitiveIdHash());
|
---|
127 | private final CopyOnWriteArrayList<DataSetListener> listeners = new CopyOnWriteArrayList<>();
|
---|
128 |
|
---|
129 | // provide means to highlight map elements that are not osm primitives
|
---|
130 | private Collection<WaySegment> highlightedVirtualNodes = new LinkedList<>();
|
---|
131 | private Collection<WaySegment> highlightedWaySegments = new LinkedList<>();
|
---|
132 | private final ListenerList<HighlightUpdateListener> highlightUpdateListeners = ListenerList.create();
|
---|
133 |
|
---|
134 | // Number of open calls to beginUpdate
|
---|
135 | private int updateCount;
|
---|
136 | // Events that occurred while dataset was locked but should be fired after write lock is released
|
---|
137 | private final List<AbstractDatasetChangedEvent> cachedEvents = new ArrayList<>();
|
---|
138 |
|
---|
139 | private String name;
|
---|
140 | private DownloadPolicy downloadPolicy = DownloadPolicy.NORMAL;
|
---|
141 | private UploadPolicy uploadPolicy = UploadPolicy.NORMAL;
|
---|
142 | /** Flag used to know if the dataset should not be editable */
|
---|
143 | private final AtomicBoolean isReadOnly = new AtomicBoolean(false);
|
---|
144 |
|
---|
145 | private final ReadWriteLock lock = new ReentrantReadWriteLock();
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * The mutex lock that is used to synchronize selection changes.
|
---|
149 | */
|
---|
150 | private final Object selectionLock = new Object();
|
---|
151 | /**
|
---|
152 | * The current selected primitives. This is always a unmodifiable set.
|
---|
153 | *
|
---|
154 | * The set should be ordered in the order in which the primitives have been added to the selection.
|
---|
155 | */
|
---|
156 | private Set<OsmPrimitive> currentSelectedPrimitives = Collections.emptySet();
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * A list of listeners that listen to selection changes on this layer.
|
---|
160 | */
|
---|
161 | private final ListenerList<DataSelectionListener> selectionListeners = ListenerList.create();
|
---|
162 |
|
---|
163 | private Area cachedDataSourceArea;
|
---|
164 | private List<Bounds> cachedDataSourceBounds;
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * All data sources of this DataSet.
|
---|
168 | */
|
---|
169 | private final Collection<DataSource> dataSources = new LinkedList<>();
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * A list of listeners that listen to DataSource changes on this layer
|
---|
173 | */
|
---|
174 | private final ListenerList<DataSourceListener> dataSourceListeners = ListenerList.create();
|
---|
175 |
|
---|
176 | private final ConflictCollection conflicts = new ConflictCollection();
|
---|
177 |
|
---|
178 | private short mappaintCacheIdx = 1;
|
---|
179 | private String remark;
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Used to temporarily store namespaces from the GPX file in case the user converts back and forth.
|
---|
183 | * Will not be saved to .osm files, but that's not necessary because GPX files won't automatically be overridden after that.
|
---|
184 | */
|
---|
185 | private List<XMLNamespace> gpxNamespaces;
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Constructs a new {@code DataSet}.
|
---|
189 | */
|
---|
190 | public DataSet() {
|
---|
191 | // Transparently register as projection change listener. No need to explicitly remove
|
---|
192 | // the listener, projection change listeners are managed as WeakReferences.
|
---|
193 | ProjectionRegistry.addProjectionChangeListener(this);
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Creates a new {@link DataSet}.
|
---|
198 | * @param copyFrom An other {@link DataSet} to copy the contents of this dataset from.
|
---|
199 | * @since 10346
|
---|
200 | */
|
---|
201 | public DataSet(DataSet copyFrom) {
|
---|
202 | this();
|
---|
203 | copyFrom.getReadLock().lock();
|
---|
204 | try {
|
---|
205 | clonePrimitives(copyFrom.getNodes(), copyFrom.getWays(), copyFrom.getRelations());
|
---|
206 | DataSourceAddedEvent addedEvent = new DataSourceAddedEvent(this,
|
---|
207 | new LinkedHashSet<>(dataSources), copyFrom.dataSources.stream());
|
---|
208 | for (DataSource source : copyFrom.dataSources) {
|
---|
209 | dataSources.add(new DataSource(source));
|
---|
210 | }
|
---|
211 | dataSourceListeners.fireEvent(d -> d.dataSourceChange(addedEvent));
|
---|
212 | version = copyFrom.version;
|
---|
213 | uploadPolicy = copyFrom.uploadPolicy;
|
---|
214 | downloadPolicy = copyFrom.downloadPolicy;
|
---|
215 | isReadOnly.set(copyFrom.isReadOnly.get());
|
---|
216 | } finally {
|
---|
217 | copyFrom.getReadLock().unlock();
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Constructs a new {@code DataSet} initially filled with the given primitives.
|
---|
223 | * @param osmPrimitives primitives to add to this data set
|
---|
224 | * @since 12726
|
---|
225 | */
|
---|
226 | public DataSet(OsmPrimitive... osmPrimitives) {
|
---|
227 | this();
|
---|
228 | update(() -> {
|
---|
229 | for (OsmPrimitive o : osmPrimitives) {
|
---|
230 | addPrimitive(o);
|
---|
231 | }
|
---|
232 | });
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Clones the specified primitives into this data set.
|
---|
237 | * @param nodes nodes to clone
|
---|
238 | * @param ways ways to clone
|
---|
239 | * @param relations relations to clone
|
---|
240 | * @return the map of cloned primitives indexed by their original version
|
---|
241 | * @since 18001
|
---|
242 | */
|
---|
243 | public Map<OsmPrimitive, OsmPrimitive> clonePrimitives(Iterable<Node> nodes, Iterable<Way> ways, Iterable<Relation> relations) {
|
---|
244 | Map<OsmPrimitive, OsmPrimitive> primMap = new HashMap<>();
|
---|
245 | for (Node n : nodes) {
|
---|
246 | Node newNode = new Node(n);
|
---|
247 | primMap.put(n, newNode);
|
---|
248 | addPrimitive(newNode);
|
---|
249 | }
|
---|
250 | for (Way w : ways) {
|
---|
251 | Way newWay = new Way(w, false, false);
|
---|
252 | primMap.put(w, newWay);
|
---|
253 | List<Node> newNodes = w.getNodes().stream()
|
---|
254 | .map(n -> (Node) primMap.get(n))
|
---|
255 | .collect(Collectors.toList());
|
---|
256 | newWay.setNodes(newNodes);
|
---|
257 | addPrimitive(newWay);
|
---|
258 | }
|
---|
259 | // Because relations can have other relations as members we first clone all relations
|
---|
260 | // and then get the cloned members
|
---|
261 | for (Relation r : relations) {
|
---|
262 | Relation newRelation = new Relation(r, false, false);
|
---|
263 | primMap.put(r, newRelation);
|
---|
264 | addPrimitive(newRelation);
|
---|
265 | }
|
---|
266 | for (Relation r : relations) {
|
---|
267 | ((Relation) primMap.get(r)).setMembers(r.getMembers().stream()
|
---|
268 | .map(rm -> new RelationMember(rm.getRole(), primMap.get(rm.getMember())))
|
---|
269 | .collect(Collectors.toList()));
|
---|
270 | }
|
---|
271 | return primMap;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Adds a new data source.
|
---|
276 | * @param source data source to add
|
---|
277 | * @return {@code true} if the collection changed as a result of the call
|
---|
278 | * @since 11626
|
---|
279 | */
|
---|
280 | public synchronized boolean addDataSource(DataSource source) {
|
---|
281 | return addDataSources(Collections.singleton(source));
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Adds new data sources.
|
---|
286 | * @param sources data sources to add
|
---|
287 | * @return {@code true} if the collection changed as a result of the call
|
---|
288 | * @since 11626
|
---|
289 | */
|
---|
290 | public synchronized boolean addDataSources(Collection<DataSource> sources) {
|
---|
291 | DataSourceAddedEvent addedEvent = new DataSourceAddedEvent(this,
|
---|
292 | new LinkedHashSet<>(dataSources), sources.stream());
|
---|
293 | boolean changed = dataSources.addAll(sources);
|
---|
294 | if (changed) {
|
---|
295 | cachedDataSourceArea = null;
|
---|
296 | cachedDataSourceBounds = null;
|
---|
297 | }
|
---|
298 | dataSourceListeners.fireEvent(d -> d.dataSourceChange(addedEvent));
|
---|
299 | return changed;
|
---|
300 | }
|
---|
301 |
|
---|
302 | @Override
|
---|
303 | public Lock getReadLock() {
|
---|
304 | return lock.readLock();
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * History of selections - shared by plugins and SelectionListDialog
|
---|
309 | */
|
---|
310 | private final LinkedList<Collection<? extends OsmPrimitive>> selectionHistory = new LinkedList<>();
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Replies the history of JOSM selections
|
---|
314 | *
|
---|
315 | * @return list of history entries
|
---|
316 | */
|
---|
317 | public LinkedList<Collection<? extends OsmPrimitive>> getSelectionHistory() {
|
---|
318 | return selectionHistory;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Clears selection history list
|
---|
323 | */
|
---|
324 | public void clearSelectionHistory() {
|
---|
325 | selectionHistory.clear();
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * The API version that created this data set, if any.
|
---|
330 | */
|
---|
331 | private String version;
|
---|
332 |
|
---|
333 | @Override
|
---|
334 | public String getVersion() {
|
---|
335 | return version;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Sets the API version this dataset was created from.
|
---|
340 | *
|
---|
341 | * @param version the API version, i.e. "0.6"
|
---|
342 | * @throws IllegalStateException if the dataset is read-only
|
---|
343 | */
|
---|
344 | public void setVersion(String version) {
|
---|
345 | checkModifiable();
|
---|
346 | this.version = version;
|
---|
347 | }
|
---|
348 |
|
---|
349 | @Override
|
---|
350 | public DownloadPolicy getDownloadPolicy() {
|
---|
351 | return this.downloadPolicy;
|
---|
352 | }
|
---|
353 |
|
---|
354 | @Override
|
---|
355 | public void setDownloadPolicy(DownloadPolicy downloadPolicy) {
|
---|
356 | this.downloadPolicy = Objects.requireNonNull(downloadPolicy);
|
---|
357 | }
|
---|
358 |
|
---|
359 | @Override
|
---|
360 | public UploadPolicy getUploadPolicy() {
|
---|
361 | return this.uploadPolicy;
|
---|
362 | }
|
---|
363 |
|
---|
364 | @Override
|
---|
365 | public void setUploadPolicy(UploadPolicy uploadPolicy) {
|
---|
366 | this.uploadPolicy = Objects.requireNonNull(uploadPolicy);
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Holding bin for changeset tag information, to be applied when or if this is ever uploaded.
|
---|
371 | */
|
---|
372 | private final Map<String, String> changeSetTags = new HashMap<>();
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Replies the set of changeset tags to be applied when or if this is ever uploaded.
|
---|
376 | * @return the set of changeset tags
|
---|
377 | * @see #addChangeSetTag
|
---|
378 | */
|
---|
379 | public Map<String, String> getChangeSetTags() {
|
---|
380 | return changeSetTags;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Adds a new changeset tag.
|
---|
385 | * @param k Key
|
---|
386 | * @param v Value
|
---|
387 | * @see #getChangeSetTags
|
---|
388 | */
|
---|
389 | public void addChangeSetTag(String k, String v) {
|
---|
390 | if (v != null) {
|
---|
391 | this.changeSetTags.put(k, v);
|
---|
392 | } else {
|
---|
393 | this.changeSetTags.remove(k);
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | @Override
|
---|
398 | public <T extends OsmPrimitive> Collection<T> getPrimitives(Predicate<? super OsmPrimitive> predicate) {
|
---|
399 | return new SubclassFilteredCollection<>(allPrimitives, predicate);
|
---|
400 | }
|
---|
401 |
|
---|
402 | @Override
|
---|
403 | public Collection<Node> getNodes() {
|
---|
404 | return getPrimitives(Node.class::isInstance);
|
---|
405 | }
|
---|
406 |
|
---|
407 | @Override
|
---|
408 | public List<Node> searchNodes(BBox bbox) {
|
---|
409 | lock.readLock().lock();
|
---|
410 | try {
|
---|
411 | return store.searchNodes(bbox);
|
---|
412 | } finally {
|
---|
413 | lock.readLock().unlock();
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | @Override
|
---|
418 | public Collection<Way> getWays() {
|
---|
419 | return getPrimitives(Way.class::isInstance);
|
---|
420 | }
|
---|
421 |
|
---|
422 | @Override
|
---|
423 | public List<Way> searchWays(BBox bbox) {
|
---|
424 | lock.readLock().lock();
|
---|
425 | try {
|
---|
426 | return store.searchWays(bbox);
|
---|
427 | } finally {
|
---|
428 | lock.readLock().unlock();
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | @Override
|
---|
433 | public List<Relation> searchRelations(BBox bbox) {
|
---|
434 | lock.readLock().lock();
|
---|
435 | try {
|
---|
436 | return store.searchRelations(bbox);
|
---|
437 | } finally {
|
---|
438 | lock.readLock().unlock();
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Searches for all primitives in the given bounding box
|
---|
444 | *
|
---|
445 | * @param bbox the bounding box
|
---|
446 | * @return List of primitives in the given bbox. Can be empty but not null
|
---|
447 | * @since 15891
|
---|
448 | */
|
---|
449 | public List<OsmPrimitive> searchPrimitives(BBox bbox) {
|
---|
450 | List<OsmPrimitive> primitiveList = new ArrayList<>();
|
---|
451 | primitiveList.addAll(searchNodes(bbox));
|
---|
452 | primitiveList.addAll(searchWays(bbox));
|
---|
453 | primitiveList.addAll(searchRelations(bbox));
|
---|
454 | return primitiveList;
|
---|
455 | }
|
---|
456 |
|
---|
457 | @Override
|
---|
458 | public Collection<Relation> getRelations() {
|
---|
459 | return getPrimitives(Relation.class::isInstance);
|
---|
460 | }
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Determines if the given node can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
464 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
465 | *
|
---|
466 | * @param n The node to search
|
---|
467 | * @return {@code true} if {@code n} can be retrieved in this data set, {@code false} otherwise
|
---|
468 | * @since 7501
|
---|
469 | */
|
---|
470 | @Override
|
---|
471 | public boolean containsNode(Node n) {
|
---|
472 | return store.containsNode(n);
|
---|
473 | }
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Determines if the given way can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
477 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
478 | *
|
---|
479 | * @param w The way to search
|
---|
480 | * @return {@code true} if {@code w} can be retrieved in this data set, {@code false} otherwise
|
---|
481 | * @since 7501
|
---|
482 | */
|
---|
483 | @Override
|
---|
484 | public boolean containsWay(Way w) {
|
---|
485 | return store.containsWay(w);
|
---|
486 | }
|
---|
487 |
|
---|
488 | /**
|
---|
489 | * Determines if the given relation can be retrieved in the data set through its bounding box. Useful for dataset consistency test.
|
---|
490 | * For efficiency reasons this method does not lock the dataset, you have to lock it manually.
|
---|
491 | *
|
---|
492 | * @param r The relation to search
|
---|
493 | * @return {@code true} if {@code r} can be retrieved in this data set, {@code false} otherwise
|
---|
494 | * @since 7501
|
---|
495 | */
|
---|
496 | @Override
|
---|
497 | public boolean containsRelation(Relation r) {
|
---|
498 | return store.containsRelation(r);
|
---|
499 | }
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Adds a primitive to the dataset.
|
---|
503 | *
|
---|
504 | * @param primitive the primitive.
|
---|
505 | * @throws IllegalStateException if the dataset is read-only
|
---|
506 | */
|
---|
507 | @Override
|
---|
508 | public void addPrimitive(OsmPrimitive primitive) {
|
---|
509 | Objects.requireNonNull(primitive, "primitive");
|
---|
510 | checkModifiable();
|
---|
511 | update(() -> {
|
---|
512 | if (getPrimitiveById(primitive) != null)
|
---|
513 | throw new DataIntegrityProblemException(
|
---|
514 | tr("Unable to add primitive {0} to the dataset because it is already included", primitive.toString()),
|
---|
515 | null, primitive);
|
---|
516 |
|
---|
517 | allPrimitives.add(primitive);
|
---|
518 | primitive.setDataset(this);
|
---|
519 | primitive.updatePosition(); // Set cached bbox for way and relation (required for reindexWay and reindexRelation to work properly)
|
---|
520 | store.addPrimitive(primitive);
|
---|
521 | firePrimitivesAdded(Collections.singletonList(primitive), false);
|
---|
522 | });
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Adds recursively a primitive, and all its children, to the dataset.
|
---|
527 | *
|
---|
528 | * @param primitive the primitive.
|
---|
529 | * @throws IllegalStateException if the dataset is read-only
|
---|
530 | * @since 17981
|
---|
531 | */
|
---|
532 | public void addPrimitiveRecursive(OsmPrimitive primitive) {
|
---|
533 | Stream<OsmPrimitive> children;
|
---|
534 | if (primitive instanceof Way) {
|
---|
535 | children = ((Way) primitive).getNodes().stream().map(OsmPrimitive.class::cast);
|
---|
536 | } else if (primitive instanceof Relation) {
|
---|
537 | children = ((Relation) primitive).getMembers().stream().map(RelationMember::getMember);
|
---|
538 | } else {
|
---|
539 | children = Stream.empty();
|
---|
540 | }
|
---|
541 | // Relations may have the same member multiple times.
|
---|
542 | children.filter(p -> !Objects.equals(this, p.getDataSet())).forEach(this::addPrimitiveRecursive);
|
---|
543 | addPrimitive(primitive);
|
---|
544 | }
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * Removes a primitive from the dataset. This method only removes the
|
---|
548 | * primitive form the respective collection of primitives managed
|
---|
549 | * by this dataset, i.e. from {@code store.nodes}, {@code store.ways}, or
|
---|
550 | * {@code store.relations}. References from other primitives to this
|
---|
551 | * primitive are left unchanged.
|
---|
552 | *
|
---|
553 | * @param primitiveId the id of the primitive
|
---|
554 | * @throws IllegalStateException if the dataset is read-only
|
---|
555 | */
|
---|
556 | public void removePrimitive(PrimitiveId primitiveId) {
|
---|
557 | checkModifiable();
|
---|
558 | update(() -> {
|
---|
559 | OsmPrimitive primitive = getPrimitiveByIdChecked(primitiveId);
|
---|
560 | if (primitive == null)
|
---|
561 | return;
|
---|
562 | removePrimitiveImpl(primitive);
|
---|
563 | firePrimitivesRemoved(Collections.singletonList(primitive), false);
|
---|
564 | });
|
---|
565 | }
|
---|
566 |
|
---|
567 | private void removePrimitiveImpl(OsmPrimitive primitive) {
|
---|
568 | clearSelection(primitive.getPrimitiveId());
|
---|
569 | if (primitive.isSelected()) {
|
---|
570 | throw new DataIntegrityProblemException("Primitive was re-selected by a selection listener: " + primitive);
|
---|
571 | }
|
---|
572 | store.removePrimitive(primitive);
|
---|
573 | allPrimitives.remove(primitive);
|
---|
574 | primitive.setDataset(null);
|
---|
575 | }
|
---|
576 |
|
---|
577 | void removePrimitive(OsmPrimitive primitive) {
|
---|
578 | checkModifiable();
|
---|
579 | update(() -> {
|
---|
580 | removePrimitiveImpl(primitive);
|
---|
581 | firePrimitivesRemoved(Collections.singletonList(primitive), false);
|
---|
582 | });
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*---------------------------------------------------
|
---|
586 | * SELECTION HANDLING
|
---|
587 | *---------------------------------------------------*/
|
---|
588 |
|
---|
589 | @Override
|
---|
590 | public void addSelectionListener(DataSelectionListener listener) {
|
---|
591 | selectionListeners.addListener(listener);
|
---|
592 | }
|
---|
593 |
|
---|
594 | @Override
|
---|
595 | public void removeSelectionListener(DataSelectionListener listener) {
|
---|
596 | selectionListeners.removeListener(listener);
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Returns selected nodes and ways.
|
---|
601 | * @return selected nodes and ways
|
---|
602 | */
|
---|
603 | public Collection<OsmPrimitive> getSelectedNodesAndWays() {
|
---|
604 | return new SubclassFilteredCollection<>(getSelected(),
|
---|
605 | primitive -> primitive instanceof Node || primitive instanceof Way);
|
---|
606 | }
|
---|
607 |
|
---|
608 | /**
|
---|
609 | * Returns the way selected last or null if no way primitives were selected or selection is empty.
|
---|
610 | * @return last way in selection or null
|
---|
611 | * @since 18468
|
---|
612 | */
|
---|
613 | public Way getLastSelectedWay() {
|
---|
614 | Way lastWay = null;
|
---|
615 | for (Way way: getSelectedWays()) lastWay = way;
|
---|
616 | return lastWay;
|
---|
617 | }
|
---|
618 |
|
---|
619 | @Override
|
---|
620 | public Collection<WaySegment> getHighlightedVirtualNodes() {
|
---|
621 | return Collections.unmodifiableCollection(highlightedVirtualNodes);
|
---|
622 | }
|
---|
623 |
|
---|
624 | @Override
|
---|
625 | public Collection<WaySegment> getHighlightedWaySegments() {
|
---|
626 | return Collections.unmodifiableCollection(highlightedWaySegments);
|
---|
627 | }
|
---|
628 |
|
---|
629 | @Override
|
---|
630 | public void addHighlightUpdateListener(HighlightUpdateListener listener) {
|
---|
631 | highlightUpdateListeners.addListener(listener);
|
---|
632 | }
|
---|
633 |
|
---|
634 | @Override
|
---|
635 | public void removeHighlightUpdateListener(HighlightUpdateListener listener) {
|
---|
636 | highlightUpdateListeners.removeListener(listener);
|
---|
637 | }
|
---|
638 |
|
---|
639 | /**
|
---|
640 | * Adds a listener that gets notified whenever the data sources change
|
---|
641 | *
|
---|
642 | * @param listener The listener
|
---|
643 | * @see #removeDataSourceListener
|
---|
644 | * @see #getDataSources
|
---|
645 | * @since 15609
|
---|
646 | */
|
---|
647 | public void addDataSourceListener(DataSourceListener listener) {
|
---|
648 | dataSourceListeners.addListener(listener);
|
---|
649 | }
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Removes a listener that gets notified whenever the data sources change
|
---|
653 | *
|
---|
654 | * @param listener The listener
|
---|
655 | * @see #addDataSourceListener
|
---|
656 | * @see #getDataSources
|
---|
657 | * @since 15609
|
---|
658 | */
|
---|
659 | public void removeDataSourceListener(DataSourceListener listener) {
|
---|
660 | dataSourceListeners.removeListener(listener);
|
---|
661 | }
|
---|
662 |
|
---|
663 | @Override
|
---|
664 | public Collection<OsmPrimitive> getAllSelected() {
|
---|
665 | return currentSelectedPrimitives;
|
---|
666 | }
|
---|
667 |
|
---|
668 | @Override
|
---|
669 | public boolean selectionEmpty() {
|
---|
670 | return currentSelectedPrimitives.isEmpty();
|
---|
671 | }
|
---|
672 |
|
---|
673 | @Override
|
---|
674 | public boolean isSelected(OsmPrimitive osm) {
|
---|
675 | return currentSelectedPrimitives.contains(osm);
|
---|
676 | }
|
---|
677 |
|
---|
678 | @Override
|
---|
679 | public void setHighlightedVirtualNodes(Collection<WaySegment> waySegments) {
|
---|
680 | if (highlightedVirtualNodes.isEmpty() && waySegments.isEmpty())
|
---|
681 | return;
|
---|
682 |
|
---|
683 | highlightedVirtualNodes = waySegments;
|
---|
684 | fireHighlightingChanged();
|
---|
685 | }
|
---|
686 |
|
---|
687 | @Override
|
---|
688 | public void setHighlightedWaySegments(Collection<WaySegment> waySegments) {
|
---|
689 | if (highlightedWaySegments.isEmpty() && waySegments.isEmpty())
|
---|
690 | return;
|
---|
691 |
|
---|
692 | highlightedWaySegments = waySegments;
|
---|
693 | fireHighlightingChanged();
|
---|
694 | }
|
---|
695 |
|
---|
696 | @Override
|
---|
697 | public void setSelected(Collection<? extends PrimitiveId> selection) {
|
---|
698 | setSelected(selection.stream());
|
---|
699 | }
|
---|
700 |
|
---|
701 | @Override
|
---|
702 | public void setSelected(PrimitiveId... osm) {
|
---|
703 | setSelected(Stream.of(osm).filter(Objects::nonNull));
|
---|
704 | }
|
---|
705 |
|
---|
706 | private void setSelected(Stream<? extends PrimitiveId> stream) {
|
---|
707 | doSelectionChange(old -> new SelectionReplaceEvent(this, old,
|
---|
708 | stream.map(this::getPrimitiveByIdChecked).filter(Objects::nonNull)));
|
---|
709 | }
|
---|
710 |
|
---|
711 | @Override
|
---|
712 | public void addSelected(Collection<? extends PrimitiveId> selection) {
|
---|
713 | addSelected(selection.stream());
|
---|
714 | }
|
---|
715 |
|
---|
716 | @Override
|
---|
717 | public void addSelected(PrimitiveId... osm) {
|
---|
718 | addSelected(Stream.of(osm));
|
---|
719 | }
|
---|
720 |
|
---|
721 | private void addSelected(Stream<? extends PrimitiveId> stream) {
|
---|
722 | doSelectionChange(old -> new SelectionAddEvent(this, old,
|
---|
723 | stream.map(this::getPrimitiveByIdChecked).filter(Objects::nonNull)));
|
---|
724 | }
|
---|
725 |
|
---|
726 | @Override
|
---|
727 | public void clearSelection(PrimitiveId... osm) {
|
---|
728 | clearSelection(Stream.of(osm));
|
---|
729 | }
|
---|
730 |
|
---|
731 | @Override
|
---|
732 | public void clearSelection(Collection<? extends PrimitiveId> list) {
|
---|
733 | clearSelection(list.stream());
|
---|
734 | }
|
---|
735 |
|
---|
736 | @Override
|
---|
737 | public void clearSelection() {
|
---|
738 | setSelected(Stream.empty());
|
---|
739 | }
|
---|
740 |
|
---|
741 | private void clearSelection(Stream<? extends PrimitiveId> stream) {
|
---|
742 | doSelectionChange(old -> new SelectionRemoveEvent(this, old,
|
---|
743 | stream.map(this::getPrimitiveByIdChecked).filter(Objects::nonNull)));
|
---|
744 | }
|
---|
745 |
|
---|
746 | @Override
|
---|
747 | public void toggleSelected(Collection<? extends PrimitiveId> osm) {
|
---|
748 | toggleSelected(osm.stream());
|
---|
749 | }
|
---|
750 |
|
---|
751 | @Override
|
---|
752 | public void toggleSelected(PrimitiveId... osm) {
|
---|
753 | toggleSelected(Stream.of(osm));
|
---|
754 | }
|
---|
755 |
|
---|
756 | private void toggleSelected(Stream<? extends PrimitiveId> stream) {
|
---|
757 | doSelectionChange(old -> new SelectionToggleEvent(this, old,
|
---|
758 | stream.map(this::getPrimitiveByIdChecked).filter(Objects::nonNull)));
|
---|
759 | }
|
---|
760 |
|
---|
761 | /**
|
---|
762 | * Do a selection change.
|
---|
763 | * <p>
|
---|
764 | * This is the only method that changes the current selection state.
|
---|
765 | * @param command A generator that generates the {@link SelectionChangeEvent} for the given base set of currently selected primitives.
|
---|
766 | * @return true iff the command did change the selection.
|
---|
767 | * @since 12048
|
---|
768 | */
|
---|
769 | private boolean doSelectionChange(Function<Set<OsmPrimitive>, SelectionChangeEvent> command) {
|
---|
770 | synchronized (selectionLock) {
|
---|
771 | SelectionChangeEvent event = command.apply(currentSelectedPrimitives);
|
---|
772 | if (event.isNop()) {
|
---|
773 | return false;
|
---|
774 | }
|
---|
775 | currentSelectedPrimitives = event.getSelection();
|
---|
776 | selectionListeners.fireEvent(l -> l.selectionChanged(event));
|
---|
777 | return true;
|
---|
778 | }
|
---|
779 | }
|
---|
780 |
|
---|
781 | @Override
|
---|
782 | public synchronized Area getDataSourceArea() {
|
---|
783 | if (cachedDataSourceArea == null) {
|
---|
784 | cachedDataSourceArea = OsmData.super.getDataSourceArea();
|
---|
785 | }
|
---|
786 | return cachedDataSourceArea;
|
---|
787 | }
|
---|
788 |
|
---|
789 | @Override
|
---|
790 | public synchronized List<Bounds> getDataSourceBounds() {
|
---|
791 | if (cachedDataSourceBounds == null) {
|
---|
792 | cachedDataSourceBounds = OsmData.super.getDataSourceBounds();
|
---|
793 | }
|
---|
794 | return Collections.unmodifiableList(cachedDataSourceBounds);
|
---|
795 | }
|
---|
796 |
|
---|
797 | @Override
|
---|
798 | public synchronized Collection<DataSource> getDataSources() {
|
---|
799 | return Collections.unmodifiableCollection(dataSources);
|
---|
800 | }
|
---|
801 |
|
---|
802 | @Override
|
---|
803 | public OsmPrimitive getPrimitiveById(PrimitiveId primitiveId) {
|
---|
804 | return primitiveId != null ? primitivesMap.get(primitiveId) : null;
|
---|
805 | }
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Show message and stack trace in log in case primitive is not found
|
---|
809 | * @param primitiveId primitive id to look for
|
---|
810 | * @return Primitive by id.
|
---|
811 | */
|
---|
812 | private OsmPrimitive getPrimitiveByIdChecked(PrimitiveId primitiveId) {
|
---|
813 | OsmPrimitive result = getPrimitiveById(primitiveId);
|
---|
814 | if (result == null && primitiveId != null) {
|
---|
815 | Logging.error(new IllegalStateException(tr(
|
---|
816 | "JOSM expected to find primitive [{0} {1}] in dataset but it is not there. Please report this "
|
---|
817 | + "at {2}. This is not a critical error, it should be safe to continue in your work.",
|
---|
818 | primitiveId.getType(), Long.toString(primitiveId.getUniqueId()), Config.getUrls().getJOSMWebsite())));
|
---|
819 | }
|
---|
820 |
|
---|
821 | return result;
|
---|
822 | }
|
---|
823 |
|
---|
824 | private static void deleteWay(Way way) {
|
---|
825 | way.setNodes(null);
|
---|
826 | way.setDeleted(true);
|
---|
827 | }
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Removes all references from ways in this dataset to a particular node.
|
---|
831 | *
|
---|
832 | * @param node the node
|
---|
833 | * @return The set of ways that have been modified
|
---|
834 | * @throws IllegalStateException if the dataset is read-only
|
---|
835 | */
|
---|
836 | public Set<Way> unlinkNodeFromWays(Node node) {
|
---|
837 | checkModifiable();
|
---|
838 | return update(() -> {
|
---|
839 | Set<Way> result = new HashSet<>();
|
---|
840 | for (Way way : node.getParentWays()) {
|
---|
841 | List<Node> wayNodes;
|
---|
842 | if (!way.isIncomplete()) {
|
---|
843 | wayNodes = way.calculateRemoveNodes(Collections.singleton(node));
|
---|
844 | } else {
|
---|
845 | wayNodes = way.getNodes();
|
---|
846 | wayNodes.removeIf(node::equals);
|
---|
847 | }
|
---|
848 | if (wayNodes.size() < way.getNodesCount()) {
|
---|
849 | if (wayNodes.size() < 2) {
|
---|
850 | deleteWay(way);
|
---|
851 | } else {
|
---|
852 | way.setNodes(wayNodes);
|
---|
853 | }
|
---|
854 | result.add(way);
|
---|
855 | }
|
---|
856 | }
|
---|
857 | return result;
|
---|
858 | });
|
---|
859 | }
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * removes all references from relations in this dataset to this primitive
|
---|
863 | *
|
---|
864 | * @param primitive the primitive
|
---|
865 | * @return The set of relations that have been modified
|
---|
866 | * @throws IllegalStateException if the dataset is read-only
|
---|
867 | */
|
---|
868 | public Set<Relation> unlinkPrimitiveFromRelations(OsmPrimitive primitive) {
|
---|
869 | checkModifiable();
|
---|
870 | return update(() -> {
|
---|
871 | Set<Relation> result = new HashSet<>();
|
---|
872 | for (Relation relation : getRelations()) {
|
---|
873 | List<RelationMember> members = relation.getMembers();
|
---|
874 | boolean removed = members.removeIf(member -> member.getMember().equals(primitive));
|
---|
875 | if (removed) {
|
---|
876 | relation.setMembers(members);
|
---|
877 | result.add(relation);
|
---|
878 | }
|
---|
879 | }
|
---|
880 | return result;
|
---|
881 | });
|
---|
882 | }
|
---|
883 |
|
---|
884 | /**
|
---|
885 | * Removes all references from other primitives to the referenced primitive.
|
---|
886 | *
|
---|
887 | * @param referencedPrimitive the referenced primitive
|
---|
888 | * @return The set of primitives that have been modified
|
---|
889 | * @throws IllegalStateException if the dataset is read-only
|
---|
890 | */
|
---|
891 | public Set<OsmPrimitive> unlinkReferencesToPrimitive(OsmPrimitive referencedPrimitive) {
|
---|
892 | checkModifiable();
|
---|
893 | return update(() -> {
|
---|
894 | Set<OsmPrimitive> result = new HashSet<>();
|
---|
895 | if (referencedPrimitive instanceof Node) {
|
---|
896 | result.addAll(unlinkNodeFromWays((Node) referencedPrimitive));
|
---|
897 | }
|
---|
898 | result.addAll(unlinkPrimitiveFromRelations(referencedPrimitive));
|
---|
899 | return result;
|
---|
900 | });
|
---|
901 | }
|
---|
902 |
|
---|
903 | @Override
|
---|
904 | public boolean isModified() {
|
---|
905 | return allPrimitives.parallelStream().anyMatch(OsmPrimitive::isModified);
|
---|
906 | }
|
---|
907 |
|
---|
908 | /**
|
---|
909 | * Replies true if there is at least one primitive in this dataset which requires to be uploaded to server.
|
---|
910 | * @return true if there is at least one primitive in this dataset which requires to be uploaded to server
|
---|
911 | * @since 13161
|
---|
912 | */
|
---|
913 | public boolean requiresUploadToServer() {
|
---|
914 | return allPrimitives.parallelStream().anyMatch(p -> APIOperation.of(p) != null);
|
---|
915 | }
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Adds a new data set listener.
|
---|
919 | * @param dsl The data set listener to add
|
---|
920 | */
|
---|
921 | public void addDataSetListener(DataSetListener dsl) {
|
---|
922 | listeners.addIfAbsent(dsl);
|
---|
923 | }
|
---|
924 |
|
---|
925 | /**
|
---|
926 | * Removes a data set listener.
|
---|
927 | * @param dsl The data set listener to remove
|
---|
928 | */
|
---|
929 | public void removeDataSetListener(DataSetListener dsl) {
|
---|
930 | listeners.remove(dsl);
|
---|
931 | }
|
---|
932 |
|
---|
933 | /**
|
---|
934 | * Can be called before bigger changes on dataset. Events are disabled until {@link #endUpdate()}.
|
---|
935 | * {@link DataSetListener#dataChanged(DataChangedEvent event)} event is triggered after end of changes
|
---|
936 | * <br>
|
---|
937 | * Typical use case should look like this:
|
---|
938 | * <pre>
|
---|
939 | * ds.beginUpdate();
|
---|
940 | * try {
|
---|
941 | * ...
|
---|
942 | * } finally {
|
---|
943 | * ds.endUpdate();
|
---|
944 | * }
|
---|
945 | * </pre>
|
---|
946 | * @see #endUpdate()
|
---|
947 | */
|
---|
948 | public void beginUpdate() {
|
---|
949 | lock.writeLock().lock();
|
---|
950 | updateCount++;
|
---|
951 | }
|
---|
952 |
|
---|
953 | /**
|
---|
954 | * Must be called after a previous call to {@link #beginUpdate()} to fire change events.
|
---|
955 | * <br>
|
---|
956 | * Typical use case should look like this:
|
---|
957 | * <pre>
|
---|
958 | * ds.beginUpdate();
|
---|
959 | * try {
|
---|
960 | * ...
|
---|
961 | * } finally {
|
---|
962 | * ds.endUpdate();
|
---|
963 | * }
|
---|
964 | * </pre>
|
---|
965 | * @see DataSet#beginUpdate()
|
---|
966 | */
|
---|
967 | public void endUpdate() {
|
---|
968 | if (updateCount > 0) {
|
---|
969 | updateCount--;
|
---|
970 | List<AbstractDatasetChangedEvent> eventsToFire = Collections.emptyList();
|
---|
971 | if (updateCount == 0) {
|
---|
972 | eventsToFire = new ArrayList<>(cachedEvents);
|
---|
973 | cachedEvents.clear();
|
---|
974 | }
|
---|
975 |
|
---|
976 | if (!eventsToFire.isEmpty()) {
|
---|
977 | lock.readLock().lock();
|
---|
978 | try {
|
---|
979 | lock.writeLock().unlock();
|
---|
980 | if (eventsToFire.size() < MAX_SINGLE_EVENTS) {
|
---|
981 | for (AbstractDatasetChangedEvent event : eventsToFire) {
|
---|
982 | fireEventToListeners(event);
|
---|
983 | }
|
---|
984 | } else if (eventsToFire.size() == MAX_EVENTS) {
|
---|
985 | fireEventToListeners(new DataChangedEvent(this));
|
---|
986 | } else {
|
---|
987 | fireEventToListeners(new DataChangedEvent(this, eventsToFire));
|
---|
988 | }
|
---|
989 | } finally {
|
---|
990 | lock.readLock().unlock();
|
---|
991 | }
|
---|
992 | } else {
|
---|
993 | lock.writeLock().unlock();
|
---|
994 | }
|
---|
995 |
|
---|
996 | } else
|
---|
997 | throw new AssertionError("endUpdate called without beginUpdate");
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * Performs the update runnable between {@link #beginUpdate()} / {@link #endUpdate()} calls.
|
---|
1002 | * @param runnable update action
|
---|
1003 | * @since 16187
|
---|
1004 | */
|
---|
1005 | public void update(Runnable runnable) {
|
---|
1006 | beginUpdate();
|
---|
1007 | try {
|
---|
1008 | runnable.run();
|
---|
1009 | } finally {
|
---|
1010 | endUpdate();
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | /**
|
---|
1015 | * Performs the update function between {@link #beginUpdate()} / {@link #endUpdate()} calls.
|
---|
1016 | * @param function update function
|
---|
1017 | * @param t function argument
|
---|
1018 | * @param <T> argument type
|
---|
1019 | * @param <R> result type
|
---|
1020 | * @return function result
|
---|
1021 | * @since 16187
|
---|
1022 | */
|
---|
1023 | public <T, R> R update(Function<T, R> function, T t) {
|
---|
1024 | beginUpdate();
|
---|
1025 | try {
|
---|
1026 | return function.apply(t);
|
---|
1027 | } finally {
|
---|
1028 | endUpdate();
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Performs the update supplier between {@link #beginUpdate()} / {@link #endUpdate()} calls.
|
---|
1034 | * @param supplier update supplier
|
---|
1035 | * @param <R> result type
|
---|
1036 | * @return supplier result
|
---|
1037 | * @since 16187
|
---|
1038 | */
|
---|
1039 | public <R> R update(Supplier<R> supplier) {
|
---|
1040 | beginUpdate();
|
---|
1041 | try {
|
---|
1042 | return supplier.get();
|
---|
1043 | } finally {
|
---|
1044 | endUpdate();
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | private void fireEventToListeners(AbstractDatasetChangedEvent event) {
|
---|
1049 | for (DataSetListener listener : listeners) {
|
---|
1050 | Logging.trace("Firing {0} to {1} (dataset)", event, listener);
|
---|
1051 | event.fire(listener);
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | private void fireEvent(AbstractDatasetChangedEvent event) {
|
---|
1056 | if (updateCount == 0)
|
---|
1057 | throw new AssertionError("dataset events can be fired only when dataset is locked");
|
---|
1058 | if (cachedEvents.size() < MAX_EVENTS) {
|
---|
1059 | cachedEvents.add(event);
|
---|
1060 | }
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | void firePrimitivesAdded(Collection<? extends OsmPrimitive> added, boolean wasIncomplete) {
|
---|
1064 | fireEvent(new PrimitivesAddedEvent(this, added, wasIncomplete));
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | void firePrimitivesRemoved(Collection<? extends OsmPrimitive> removed, boolean wasComplete) {
|
---|
1068 | fireEvent(new PrimitivesRemovedEvent(this, removed, wasComplete));
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | void fireTagsChanged(OsmPrimitive prim, Map<String, String> originalKeys) {
|
---|
1072 | fireEvent(new TagsChangedEvent(this, prim, originalKeys));
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | void fireRelationMembersChanged(Relation r) {
|
---|
1076 | store.reindexRelation(r, Relation::updatePosition);
|
---|
1077 | fireEvent(new RelationMembersChangedEvent(this, r));
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | void fireNodeMoved(Node node, LatLon newCoor, EastNorth eastNorth) {
|
---|
1081 | store.reindexNode(node, n -> n.setCoorInternal(newCoor, eastNorth), Way::updatePosition, Relation::updatePosition);
|
---|
1082 | fireEvent(new NodeMovedEvent(this, node));
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | void fireWayNodesChanged(Way way) {
|
---|
1086 | if (!way.isEmpty()) {
|
---|
1087 | store.reindexWay(way, Way::updatePosition, Relation::updatePosition);
|
---|
1088 | }
|
---|
1089 | fireEvent(new WayNodesChangedEvent(this, way));
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | void fireChangesetIdChanged(OsmPrimitive primitive, int oldChangesetId, int newChangesetId) {
|
---|
1093 | fireEvent(new ChangesetIdChangedEvent(this, Collections.singletonList(primitive), oldChangesetId,
|
---|
1094 | newChangesetId));
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | void firePrimitiveFlagsChanged(OsmPrimitive primitive) {
|
---|
1098 | fireEvent(new PrimitiveFlagsChangedEvent(this, primitive));
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | void fireFilterChanged() {
|
---|
1102 | fireEvent(new FilterChangedEvent(this));
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | void fireHighlightingChanged() {
|
---|
1106 | HighlightUpdateListener.HighlightUpdateEvent e = new HighlightUpdateListener.HighlightUpdateEvent(this);
|
---|
1107 | highlightUpdateListeners.fireEvent(l -> l.highlightUpdated(e));
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /**
|
---|
1111 | * Invalidates the internal cache of projected east/north coordinates.
|
---|
1112 | *
|
---|
1113 | * This method can be invoked after the globally configured projection method changed.
|
---|
1114 | */
|
---|
1115 | public void invalidateEastNorthCache() {
|
---|
1116 | if (ProjectionRegistry.getProjection() == null)
|
---|
1117 | return; // sanity check
|
---|
1118 | update(() -> getNodes().forEach(Node::invalidateEastNorthCache));
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /**
|
---|
1122 | * Cleanups all deleted primitives (really delete them from the dataset).
|
---|
1123 | */
|
---|
1124 | public void cleanupDeletedPrimitives() {
|
---|
1125 | update(() -> {
|
---|
1126 | Collection<OsmPrimitive> toCleanUp = getPrimitives(
|
---|
1127 | primitive -> primitive.isDeleted() && (!primitive.isVisible() || primitive.isNew()));
|
---|
1128 | if (!toCleanUp.isEmpty()) {
|
---|
1129 | // We unselect them in advance to not fire a selection change for every primitive
|
---|
1130 | clearSelection(toCleanUp.stream().map(OsmPrimitive::getPrimitiveId));
|
---|
1131 | for (OsmPrimitive primitive : toCleanUp) {
|
---|
1132 | removePrimitiveImpl(primitive);
|
---|
1133 | }
|
---|
1134 | firePrimitivesRemoved(toCleanUp, false);
|
---|
1135 | }
|
---|
1136 | });
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | /**
|
---|
1140 | * Removes all primitives from the dataset and resets the currently selected primitives
|
---|
1141 | * to the empty collection. Also notifies selection change listeners if necessary.
|
---|
1142 | * @throws IllegalStateException if the dataset is read-only
|
---|
1143 | */
|
---|
1144 | @Override
|
---|
1145 | public void clear() {
|
---|
1146 | //TODO: Why can't we clear a dataset that is locked?
|
---|
1147 | //TODO: Report listeners that are still active (should be none)
|
---|
1148 | checkModifiable();
|
---|
1149 | update(() -> {
|
---|
1150 | clearSelection();
|
---|
1151 | clearSelectionHistory();
|
---|
1152 | for (OsmPrimitive primitive : allPrimitives) {
|
---|
1153 | primitive.setDataset(null);
|
---|
1154 | }
|
---|
1155 | store.clear();
|
---|
1156 | allPrimitives.clear();
|
---|
1157 | conflicts.get().clear();
|
---|
1158 | });
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | /**
|
---|
1162 | * Marks all "invisible" objects as deleted. These objects should be always marked as
|
---|
1163 | * deleted when downloaded from the server. They can be undeleted later if necessary.
|
---|
1164 | * @throws IllegalStateException if the dataset is read-only
|
---|
1165 | */
|
---|
1166 | public void deleteInvisible() {
|
---|
1167 | checkModifiable();
|
---|
1168 | for (OsmPrimitive primitive : allPrimitives) {
|
---|
1169 | if (!primitive.isVisible()) {
|
---|
1170 | primitive.setDeleted(true);
|
---|
1171 | }
|
---|
1172 | }
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | /**
|
---|
1176 | * Moves all primitives and datasources from DataSet "from" to this DataSet.
|
---|
1177 | * @param from The source DataSet
|
---|
1178 | */
|
---|
1179 | public void mergeFrom(DataSet from) {
|
---|
1180 | mergeFrom(from, null);
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * Moves all primitives and datasources from DataSet "from" to this DataSet.
|
---|
1185 | * @param from The source DataSet
|
---|
1186 | * @param progressMonitor The progress monitor
|
---|
1187 | * @throws IllegalStateException if the dataset is read-only
|
---|
1188 | */
|
---|
1189 | public synchronized void mergeFrom(DataSet from, ProgressMonitor progressMonitor) {
|
---|
1190 | if (from != null) {
|
---|
1191 | checkModifiable();
|
---|
1192 | new DataSetMerger(this, from).merge(progressMonitor);
|
---|
1193 | synchronized (from) {
|
---|
1194 | if (!from.dataSources.isEmpty()) {
|
---|
1195 | DataSourceAddedEvent addedEvent = new DataSourceAddedEvent(
|
---|
1196 | this, new LinkedHashSet<>(dataSources), from.dataSources.stream());
|
---|
1197 | DataSourceRemovedEvent clearEvent = new DataSourceRemovedEvent(
|
---|
1198 | this, new LinkedHashSet<>(from.dataSources), from.dataSources.stream());
|
---|
1199 | if (from.dataSources.stream().filter(dataSource -> !dataSources.contains(dataSource))
|
---|
1200 | .anyMatch(dataSources::add)) {
|
---|
1201 | cachedDataSourceArea = null;
|
---|
1202 | cachedDataSourceBounds = null;
|
---|
1203 | }
|
---|
1204 | from.dataSources.clear();
|
---|
1205 | from.cachedDataSourceArea = null;
|
---|
1206 | from.cachedDataSourceBounds = null;
|
---|
1207 | dataSourceListeners.fireEvent(d -> d.dataSourceChange(addedEvent));
|
---|
1208 | from.dataSourceListeners.fireEvent(d -> d.dataSourceChange(clearEvent));
|
---|
1209 | }
|
---|
1210 | }
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /**
|
---|
1215 | * Replies the set of conflicts currently managed in this layer.
|
---|
1216 | *
|
---|
1217 | * @return the set of conflicts currently managed in this layer
|
---|
1218 | * @since 12672
|
---|
1219 | */
|
---|
1220 | public ConflictCollection getConflicts() {
|
---|
1221 | return conflicts;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | @Override
|
---|
1225 | public String getName() {
|
---|
1226 | return name;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | @Override
|
---|
1230 | public void setName(String name) {
|
---|
1231 | this.name = name;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | /* --------------------------------------------------------------------------------- */
|
---|
1235 | /* interface ProjectionChangeListener */
|
---|
1236 | /* --------------------------------------------------------------------------------- */
|
---|
1237 | @Override
|
---|
1238 | public void projectionChanged(Projection oldValue, Projection newValue) {
|
---|
1239 | invalidateEastNorthCache();
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | @Override
|
---|
1243 | public synchronized ProjectionBounds getDataSourceBoundingBox() {
|
---|
1244 | BoundingXYVisitor bbox = new BoundingXYVisitor();
|
---|
1245 | for (DataSource source : dataSources) {
|
---|
1246 | bbox.visit(source.bounds);
|
---|
1247 | }
|
---|
1248 | if (bbox.hasExtend()) {
|
---|
1249 | return bbox.getBounds();
|
---|
1250 | }
|
---|
1251 | return null;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | /**
|
---|
1255 | * Returns mappaint cache index for this DataSet.
|
---|
1256 | *
|
---|
1257 | * If the {@link OsmPrimitive#mappaintCacheIdx} is not equal to the DataSet mappaint
|
---|
1258 | * cache index, this means the cache for that primitive is out of date.
|
---|
1259 | * @return mappaint cache index
|
---|
1260 | * @since 13420
|
---|
1261 | */
|
---|
1262 | public short getMappaintCacheIndex() {
|
---|
1263 | return mappaintCacheIdx;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | @Override
|
---|
1267 | public void clearMappaintCache() {
|
---|
1268 | mappaintCacheIdx++;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | @Override
|
---|
1272 | public void lock() {
|
---|
1273 | if (!isReadOnly.compareAndSet(false, true)) {
|
---|
1274 | Logging.warn("Trying to set readOnly flag on a readOnly dataset ", getName());
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | @Override
|
---|
1279 | public void unlock() {
|
---|
1280 | if (!isReadOnly.compareAndSet(true, false)) {
|
---|
1281 | Logging.warn("Trying to unset readOnly flag on a non-readOnly dataset ", getName());
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | @Override
|
---|
1286 | public boolean isLocked() {
|
---|
1287 | return isReadOnly.get();
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | /**
|
---|
1291 | * Checks the dataset is modifiable (not read-only).
|
---|
1292 | * @throws IllegalStateException if the dataset is read-only
|
---|
1293 | */
|
---|
1294 | private void checkModifiable() {
|
---|
1295 | if (isLocked()) {
|
---|
1296 | throw new IllegalStateException("DataSet is read-only");
|
---|
1297 | }
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | /**
|
---|
1301 | * Returns an optional remark about this data set (used by Overpass API).
|
---|
1302 | * @return a remark about this data set, or {@code null}
|
---|
1303 | * @since 14219
|
---|
1304 | */
|
---|
1305 | public String getRemark() {
|
---|
1306 | return remark;
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | /**
|
---|
1310 | * Sets an optional remark about this data set (used by Overpass API).
|
---|
1311 | * @param remark a remark about this data set, or {@code null}
|
---|
1312 | * @since 14219
|
---|
1313 | */
|
---|
1314 | public void setRemark(String remark) {
|
---|
1315 | this.remark = remark;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | /**
|
---|
1319 | * Gets the GPX (XML) namespaces if this DataSet was created from a GPX file
|
---|
1320 | * @return the GPXNamespaces or <code>null</code>
|
---|
1321 | */
|
---|
1322 | public List<XMLNamespace> getGPXNamespaces() {
|
---|
1323 | return gpxNamespaces;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | /**
|
---|
1327 | * Sets the GPX (XML) namespaces
|
---|
1328 | * @param gpxNamespaces the GPXNamespaces to set
|
---|
1329 | */
|
---|
1330 | public void setGPXNamespaces(List<XMLNamespace> gpxNamespaces) {
|
---|
1331 | this.gpxNamespaces = gpxNamespaces;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | /**
|
---|
1335 | * Determines if this Dataset contains no primitives.
|
---|
1336 | * @return true if this Dataset contains no primitives
|
---|
1337 | * @since 14835
|
---|
1338 | */
|
---|
1339 | public boolean isEmpty() {
|
---|
1340 | return allPrimitives.isEmpty();
|
---|
1341 | }
|
---|
1342 | }
|
---|