001 // License: GPL. Copyright 2007 by Immanuel Scholz and others
002 package org.openstreetmap.josm.data.osm.visitor;
003
004 import java.util.Collection;
005 import java.util.HashSet;
006
007 import org.openstreetmap.josm.data.osm.Node;
008 import org.openstreetmap.josm.data.osm.OsmPrimitive;
009 import org.openstreetmap.josm.data.osm.Relation;
010 import org.openstreetmap.josm.data.osm.RelationMember;
011 import org.openstreetmap.josm.data.osm.Way;
012
013 /**
014 * Collect all nodes a specific osm primitive has.
015 *
016 * @author imi
017 */
018 public class AllNodesVisitor extends AbstractVisitor {
019
020 /**
021 * The resulting nodes collected so far.
022 */
023 public Collection<Node> nodes = new HashSet<Node>();
024
025 /**
026 * Nodes have only itself as nodes.
027 */
028 public void visit(Node n) {
029 nodes.add(n);
030 }
031
032 /**
033 * Ways have their way nodes.
034 */
035 public void visit(Way w) {
036 if (w.isIncomplete()) return;
037 for (Node n : w.getNodes())
038 visit(n);
039 }
040
041 /**
042 * Relations may have any number of nodes.
043 * FIXME: do we want to collect nodes from segs/ways that are relation members?
044 * if so, use AutomatchVisitor!
045 */
046 public void visit(Relation e) {
047 for (RelationMember m : e.getMembers())
048 if (m.isNode()) visit(m.getNode());
049 }
050 /**
051 * @return All nodes the given primitive has.
052 */
053 public static Collection<Node> getAllNodes(Collection<? extends OsmPrimitive> osms) {
054 AllNodesVisitor v = new AllNodesVisitor();
055 for (OsmPrimitive osm : osms)
056 osm.visit(v);
057 return v.nodes;
058 }
059 }