source: josm/trunk/src/org/openstreetmap/josm/data/osm/PrimitiveDeepCopy.java@ 2656

Last change on this file since 2656 was 2305, checked in by jttt, 14 years ago

Use PrimitiveData for Copy, Paste and Paste tags actions

  • Property svn:mime-type set to text/plain
File size: 2.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Set;
9
10import 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 */
17public 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> visitedIds = new HashSet<Long>();
39
40 new AbstractVisitor() {
41 boolean firstIteration;
42
43 public void visit(Node n) {
44 if (!visitedIds.add(n.getUniqueId()))
45 return;
46 (firstIteration?directlyAdded:referenced).add(n.save());
47 }
48 public void visit(Way w) {
49 if (!visitedIds.add(w.getUniqueId()))
50 return;
51 (firstIteration?directlyAdded:referenced).add(w.save());
52 firstIteration = false;
53 for (Node n : w.getNodes()) {
54 visit(n);
55 }
56 }
57 public void visit(Relation e) {
58 if (!visitedIds.add(e.getUniqueId()))
59 return;
60 (firstIteration?directlyAdded:referenced).add(e.save());
61 firstIteration = false;
62 for (RelationMember m : e.getMembers()) {
63 m.getMember().visit(this);
64 }
65 }
66
67 public void visitAll() {
68 for (OsmPrimitive osm : primitives) {
69 firstIteration = true;
70 osm.visit(this);
71 }
72 }
73 }.visitAll();
74 }
75
76 public List<PrimitiveData> getDirectlyAdded() {
77 return directlyAdded;
78 }
79
80 public List<PrimitiveData> getReferenced() {
81 return referenced;
82 }
83
84 public List<PrimitiveData> getAll() {
85 List<PrimitiveData> result = new ArrayList<PrimitiveData>(directlyAdded.size() + referenced.size());
86 result.addAll(directlyAdded);
87 result.addAll(referenced);
88 return result;
89 }
90
91 public boolean isEmpty() {
92 return directlyAdded.isEmpty() && referenced.isEmpty();
93 }
94
95}
Note: See TracBrowser for help on using the repository browser.