source: josm/trunk/src/org/openstreetmap/josm/data/osm/DataSet.java @ 14169

Last change on this file since 14169 was 14169, checked in by Don-vip, 2 months ago

fix #16575 - make sure download/upload policies are never null and set to normal by default

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