1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | public class BackreferencedDataSet {
|
---|
10 | public static class RelationToChildReference {
|
---|
11 | private Relation parent;
|
---|
12 | private int position;
|
---|
13 | private String role;
|
---|
14 | private OsmPrimitive child;
|
---|
15 |
|
---|
16 | public RelationToChildReference(Relation parent, int position, String role, OsmPrimitive child) {
|
---|
17 | this.parent = parent;
|
---|
18 | this.position = position;
|
---|
19 | this.role = role;
|
---|
20 | this.child = child;
|
---|
21 | }
|
---|
22 |
|
---|
23 | public RelationToChildReference(Relation parent, int position, RelationMember member) {
|
---|
24 | this.parent = parent;
|
---|
25 | this.position = position;
|
---|
26 | this.role = member.getRole();
|
---|
27 | this.child = member.getMember();
|
---|
28 | }
|
---|
29 |
|
---|
30 | public Relation getParent() {
|
---|
31 | return parent;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public int getPosition() {
|
---|
35 | return position;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public String getRole() {
|
---|
39 | return role;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public OsmPrimitive getChild() {
|
---|
43 | return child;
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | public int hashCode() {
|
---|
48 | final int prime = 31;
|
---|
49 | int result = 1;
|
---|
50 | result = prime * result + ((child == null) ? 0 : child.hashCode());
|
---|
51 | result = prime * result + ((parent == null) ? 0 : parent.hashCode());
|
---|
52 | result = prime * result + position;
|
---|
53 | result = prime * result + ((role == null) ? 0 : role.hashCode());
|
---|
54 | return result;
|
---|
55 | }
|
---|
56 | @Override
|
---|
57 | public boolean equals(Object obj) {
|
---|
58 | if (this == obj)
|
---|
59 | return true;
|
---|
60 | if (obj == null)
|
---|
61 | return false;
|
---|
62 | if (getClass() != obj.getClass())
|
---|
63 | return false;
|
---|
64 | RelationToChildReference other = (RelationToChildReference) obj;
|
---|
65 | if (child == null) {
|
---|
66 | if (other.child != null)
|
---|
67 | return false;
|
---|
68 | } else if (!child.equals(other.child))
|
---|
69 | return false;
|
---|
70 | if (parent == null) {
|
---|
71 | if (other.parent != null)
|
---|
72 | return false;
|
---|
73 | } else if (!parent.equals(other.parent))
|
---|
74 | return false;
|
---|
75 | if (position != other.position)
|
---|
76 | return false;
|
---|
77 | if (role == null) {
|
---|
78 | if (other.role != null)
|
---|
79 | return false;
|
---|
80 | } else if (!role.equals(other.role))
|
---|
81 | return false;
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Replies the set of parent primitives for a given child primitive. Replies
|
---|
88 | * an empty set if no parents refer to the child.
|
---|
89 | *
|
---|
90 | * @param child the child primitive
|
---|
91 | * @return the set of parent primitives for a given child primitive.
|
---|
92 | */
|
---|
93 | public Set<OsmPrimitive> getParents(OsmPrimitive child) {
|
---|
94 | return new HashSet<OsmPrimitive>(child.getReferrers());
|
---|
95 | }
|
---|
96 |
|
---|
97 | public Set<OsmPrimitive> getParents(Collection<? extends OsmPrimitive> children) {
|
---|
98 | if (children == null) return Collections.emptySet();
|
---|
99 | children.remove(null);
|
---|
100 |
|
---|
101 | Set<OsmPrimitive> parents = new HashSet<OsmPrimitive>();
|
---|
102 | for(OsmPrimitive child: children) {
|
---|
103 | parents.addAll(child.getReferrers());
|
---|
104 | }
|
---|
105 | return parents;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Replies true if there is at least one parent referring to child;
|
---|
110 | * false otherwise
|
---|
111 | *
|
---|
112 | * @param child the child primitive
|
---|
113 | * @return true if there is at least one parent referring to child;
|
---|
114 | */
|
---|
115 | public boolean hasParents(OsmPrimitive child) {
|
---|
116 | return ! getParents(child).isEmpty();
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Replies a set of all {@see RelationToChildReference}s for a given child primitive.
|
---|
121 | *
|
---|
122 | * @param child the child primitive
|
---|
123 | * @return a set of all {@see RelationToChildReference}s for a given child primitive
|
---|
124 | */
|
---|
125 | public Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
|
---|
126 | Set<Relation> parents = OsmPrimitive.getFilteredSet(getParents(child), Relation.class);
|
---|
127 | Set<RelationToChildReference> references = new HashSet<RelationToChildReference>();
|
---|
128 | for (Relation parent: parents) {
|
---|
129 | for (int i=0; i < parent.getMembersCount(); i++) {
|
---|
130 | if (parent.getMember(i).refersTo(child)) {
|
---|
131 | references.add(new RelationToChildReference(parent, i, parent.getMember(i)));
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return references;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Replies a set of all {@see RelationToChildReference}s for a collection of child primitives
|
---|
140 | *
|
---|
141 | * @param children the collection of child primitives
|
---|
142 | * @return a set of all {@see RelationToChildReference}s to the children in the collection of child
|
---|
143 | * primitives
|
---|
144 | */
|
---|
145 | public Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
|
---|
146 | Set<RelationToChildReference> references = new HashSet<RelationToChildReference>();
|
---|
147 | for (OsmPrimitive child: children) {
|
---|
148 | references.addAll(getRelationToChildReferences(child));
|
---|
149 | }
|
---|
150 | return references;
|
---|
151 | }
|
---|
152 | }
|
---|