source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/CollectBackReferencesVisitor.java@ 2407

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

Added support for referrers

  • Property svn:eol-style set to native
File size: 2.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.data.osm.visitor;
3
4import java.util.Collection;
5import java.util.HashSet;
6
7import org.openstreetmap.josm.data.osm.DataSet;
8import org.openstreetmap.josm.data.osm.Node;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.data.osm.Relation;
11import org.openstreetmap.josm.data.osm.Way;
12
13/**
14 * Helper that collect all ways a node is part of.
15 *
16 * Deleted objects are not collected.
17 *
18 * @author imi, Petr Dlouhý
19 */
20public class CollectBackReferencesVisitor extends AbstractVisitor {
21
22 private final boolean indirectRefs;
23
24 private Collection<OsmPrimitive> data = new HashSet<OsmPrimitive>();
25
26
27 /**
28 * @param ds This parameter is ignored
29 */
30 public CollectBackReferencesVisitor(DataSet ds) {
31 this(true);
32 }
33
34 /**
35 * @param ds This parameter is ignored
36 * @param indirectRefs Make also indirect references?
37 */
38 public CollectBackReferencesVisitor(DataSet ds, boolean indirectRefs) {
39 this.indirectRefs = indirectRefs;
40 }
41
42 public CollectBackReferencesVisitor(boolean indirectRefs) {
43 this.indirectRefs = indirectRefs;
44 }
45
46
47 /**
48 * Get the result collection
49 */
50 public Collection<OsmPrimitive> getData(){
51 return data;
52 }
53
54 /**
55 * Initialize data before associated visit calls
56 */
57 public void initialize(){
58 data = new HashSet<OsmPrimitive>();
59 }
60
61 public void visit(OsmPrimitive o) {
62 Collection<OsmPrimitive> c = o.getReferrers();
63 Collection<OsmPrimitive> oldData = new HashSet<OsmPrimitive>(data);
64 data.addAll(c);
65 if(indirectRefs) {
66 for(OsmPrimitive oo : c)
67 if(!oldData.contains(oo)) {
68 visit(oo);
69 }
70 }
71
72 }
73
74 public void visit(Node n) {
75 visit((OsmPrimitive)n);
76 }
77
78 public void visit(Way w) {
79 visit((OsmPrimitive)w);
80 }
81
82 public void visit(Relation r) {
83 visit((OsmPrimitive)r);
84 }
85}
Note: See TracBrowser for help on using the repository browser.