1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.util;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashSet;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
9 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
10 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * A visitor that aggregates all primitives it visits.
|
---|
16 | * <p>
|
---|
17 | * The primitives are sorted according to their type: first nodes, then ways.
|
---|
18 | *
|
---|
19 | * @author frsantos
|
---|
20 | */
|
---|
21 | public class AggregatePrimitivesVisitor extends AbstractVisitor {
|
---|
22 | /** Aggregated data */
|
---|
23 | final Collection<OsmPrimitive> aggregatedData = new HashSet<>();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Visits a collection of primitives
|
---|
27 | * @param data The collection of primitives
|
---|
28 | * @return The aggregated primitives
|
---|
29 | */
|
---|
30 | public Collection<OsmPrimitive> visit(Collection<OsmPrimitive> data) {
|
---|
31 | for (OsmPrimitive osm : data) {
|
---|
32 | osm.accept(this);
|
---|
33 | }
|
---|
34 |
|
---|
35 | return aggregatedData;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void visit(Node n) {
|
---|
40 | if (!aggregatedData.contains(n)) {
|
---|
41 | aggregatedData.add(n);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void visit(Way w) {
|
---|
47 | if (!aggregatedData.contains(w)) {
|
---|
48 | aggregatedData.add(w);
|
---|
49 | for (Node n : w.getNodes()) {
|
---|
50 | visit(n);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void visit(Relation r) {
|
---|
57 | if (!aggregatedData.contains(r)) {
|
---|
58 | aggregatedData.add(r);
|
---|
59 | for (RelationMember m : r.getMembers()) {
|
---|
60 | m.getMember().accept(this);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|