1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Set;
|
---|
9 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * This class allows to create and keep a deep copy of primitives. Provides methods to access directly added
|
---|
15 | * primitives and reference primitives
|
---|
16 | * @since 2305
|
---|
17 | */
|
---|
18 | public class PrimitiveDeepCopy {
|
---|
19 |
|
---|
20 | @FunctionalInterface
|
---|
21 | public interface PasteBufferChangedListener {
|
---|
22 | void pasteBufferChanged(PrimitiveDeepCopy pasteBuffer);
|
---|
23 | }
|
---|
24 |
|
---|
25 | private final List<PrimitiveData> directlyAdded = new ArrayList<>();
|
---|
26 | private final List<PrimitiveData> referenced = new ArrayList<>();
|
---|
27 | private final CopyOnWriteArrayList<PasteBufferChangedListener> listeners = new CopyOnWriteArrayList<>();
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code PrimitiveDeepCopy} without data. Use {@link #makeCopy(Collection)} after that.
|
---|
31 | */
|
---|
32 | public PrimitiveDeepCopy() {
|
---|
33 | // Do nothing
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code PrimitiveDeepCopy} of given OSM primitives.
|
---|
38 | * @param primitives OSM primitives to copy
|
---|
39 | * @since 7961
|
---|
40 | */
|
---|
41 | public PrimitiveDeepCopy(final Collection<? extends OsmPrimitive> primitives) {
|
---|
42 | makeCopy(primitives);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Replace content of the object with copy of provided primitives.
|
---|
47 | * @param primitives OSM primitives to copy
|
---|
48 | * @since 7961
|
---|
49 | */
|
---|
50 | public final void makeCopy(final Collection<? extends OsmPrimitive> primitives) {
|
---|
51 | directlyAdded.clear();
|
---|
52 | referenced.clear();
|
---|
53 |
|
---|
54 | final Set<Long> visitedNodeIds = new HashSet<>();
|
---|
55 | final Set<Long> visitedWayIds = new HashSet<>();
|
---|
56 | final Set<Long> visitedRelationIds = new HashSet<>();
|
---|
57 |
|
---|
58 | new AbstractVisitor() {
|
---|
59 | private boolean firstIteration;
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void visit(Node n) {
|
---|
63 | if (!visitedNodeIds.add(n.getUniqueId()))
|
---|
64 | return;
|
---|
65 | (firstIteration ? directlyAdded : referenced).add(n.save());
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void visit(Way w) {
|
---|
70 | if (!visitedWayIds.add(w.getUniqueId()))
|
---|
71 | return;
|
---|
72 | (firstIteration ? directlyAdded : referenced).add(w.save());
|
---|
73 | firstIteration = false;
|
---|
74 | for (Node n : w.getNodes()) {
|
---|
75 | visit(n);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void visit(Relation r) {
|
---|
81 | if (!visitedRelationIds.add(r.getUniqueId()))
|
---|
82 | return;
|
---|
83 | (firstIteration ? directlyAdded : referenced).add(r.save());
|
---|
84 | firstIteration = false;
|
---|
85 | for (RelationMember m : r.getMembers()) {
|
---|
86 | m.getMember().accept(this);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void visitAll() {
|
---|
91 | for (OsmPrimitive osm : primitives) {
|
---|
92 | firstIteration = true;
|
---|
93 | osm.accept(this);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }.visitAll();
|
---|
97 |
|
---|
98 | firePasteBufferChanged();
|
---|
99 | }
|
---|
100 |
|
---|
101 | public List<PrimitiveData> getDirectlyAdded() {
|
---|
102 | return directlyAdded;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public List<PrimitiveData> getReferenced() {
|
---|
106 | return referenced;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public List<PrimitiveData> getAll() {
|
---|
110 | List<PrimitiveData> result = new ArrayList<>(directlyAdded.size() + referenced.size());
|
---|
111 | result.addAll(directlyAdded);
|
---|
112 | result.addAll(referenced);
|
---|
113 | return result;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public boolean isEmpty() {
|
---|
117 | return directlyAdded.isEmpty() && referenced.isEmpty();
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void firePasteBufferChanged() {
|
---|
121 | for (PasteBufferChangedListener listener: listeners) {
|
---|
122 | listener.pasteBufferChanged(this);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void addPasteBufferChangedListener(PasteBufferChangedListener listener) {
|
---|
127 | listeners.addIfAbsent(listener);
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void removePasteBufferChangedListener(PasteBufferChangedListener listener) {
|
---|
131 | listeners.remove(listener);
|
---|
132 | }
|
---|
133 | }
|
---|