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

Last change on this file since 3157 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 3.0 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> 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}
Note: See TracBrowser for help on using the repository browser.