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 |
|
---|
10 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * This class allows to create and keep a deep copy of primitives. Provides methods to access directly added
|
---|
14 | * primitives and reference primitives
|
---|
15 | *
|
---|
16 | */
|
---|
17 | public class PrimitiveDeepCopy {
|
---|
18 |
|
---|
19 | private final List<PrimitiveData> directlyAdded = new ArrayList<PrimitiveData>();
|
---|
20 | private final List<PrimitiveData> referenced = new ArrayList<PrimitiveData>();
|
---|
21 |
|
---|
22 | public PrimitiveDeepCopy() {
|
---|
23 |
|
---|
24 | }
|
---|
25 |
|
---|
26 | public PrimitiveDeepCopy(final Collection<OsmPrimitive> primitives) {
|
---|
27 | makeCopy(primitives);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Replace content of the object with copy of provided primitives
|
---|
32 | * @param primitives
|
---|
33 | */
|
---|
34 | public final void makeCopy(final Collection<OsmPrimitive> primitives) {
|
---|
35 | directlyAdded.clear();
|
---|
36 | referenced.clear();
|
---|
37 |
|
---|
38 | final Set<Long> visitedNodeIds = new HashSet<Long>();
|
---|
39 | final Set<Long> visitedWayIds = new HashSet<Long>();
|
---|
40 | final Set<Long> visitedRelationIds = new HashSet<Long>();
|
---|
41 |
|
---|
42 | new AbstractVisitor() {
|
---|
43 | boolean firstIteration;
|
---|
44 |
|
---|
45 | public void visit(Node n) {
|
---|
46 | if (!visitedNodeIds.add(n.getUniqueId()))
|
---|
47 | return;
|
---|
48 | (firstIteration ? directlyAdded : referenced).add(n.save());
|
---|
49 | }
|
---|
50 | public void visit(Way w) {
|
---|
51 | if (!visitedWayIds.add(w.getUniqueId()))
|
---|
52 | return;
|
---|
53 | (firstIteration ? directlyAdded : referenced).add(w.save());
|
---|
54 | firstIteration = false;
|
---|
55 | for (Node n : w.getNodes()) {
|
---|
56 | visit(n);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | public void visit(Relation r) {
|
---|
60 | if (!visitedRelationIds.add(r.getUniqueId()))
|
---|
61 | return;
|
---|
62 | (firstIteration ? directlyAdded : referenced).add(r.save());
|
---|
63 | firstIteration = false;
|
---|
64 | for (RelationMember m : r.getMembers()) {
|
---|
65 | m.getMember().visit(this);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void visitAll() {
|
---|
70 | for (OsmPrimitive osm : primitives) {
|
---|
71 | firstIteration = true;
|
---|
72 | osm.visit(this);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }.visitAll();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public List<PrimitiveData> getDirectlyAdded() {
|
---|
79 | return directlyAdded;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public List<PrimitiveData> getReferenced() {
|
---|
83 | return referenced;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public List<PrimitiveData> getAll() {
|
---|
87 | List<PrimitiveData> result = new ArrayList<PrimitiveData>(directlyAdded.size() + referenced.size());
|
---|
88 | result.addAll(directlyAdded);
|
---|
89 | result.addAll(referenced);
|
---|
90 | return result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public boolean isEmpty() {
|
---|
94 | return directlyAdded.isEmpty() && referenced.isEmpty();
|
---|
95 | }
|
---|
96 |
|
---|
97 | }
|
---|