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.HashSet;
|
---|
6 | import java.util.Objects;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | public class RelationToChildReference {
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Replies a set of all {@link RelationToChildReference}s for a given child primitive.
|
---|
13 | *
|
---|
14 | * @param child the child primitive
|
---|
15 | * @return a set of all {@link RelationToChildReference}s for a given child primitive
|
---|
16 | */
|
---|
17 | public static Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
|
---|
18 | Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
|
---|
19 | Set<RelationToChildReference> references = new HashSet<>();
|
---|
20 | for (Relation parent: parents) {
|
---|
21 | for (int i = 0; i < parent.getMembersCount(); i++) {
|
---|
22 | if (parent.getMember(i).refersTo(child)) {
|
---|
23 | references.add(new RelationToChildReference(parent, i, parent.getMember(i)));
|
---|
24 | }
|
---|
25 | }
|
---|
26 | }
|
---|
27 | return references;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Replies a set of all {@link RelationToChildReference}s for a collection of child primitives
|
---|
32 | *
|
---|
33 | * @param children the collection of child primitives
|
---|
34 | * @return a set of all {@link RelationToChildReference}s to the children in the collection of child
|
---|
35 | * primitives
|
---|
36 | */
|
---|
37 | public static Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
|
---|
38 | Set<RelationToChildReference> references = new HashSet<>();
|
---|
39 | for (OsmPrimitive child: children) {
|
---|
40 | references.addAll(getRelationToChildReferences(child));
|
---|
41 | }
|
---|
42 | return references;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private final Relation parent;
|
---|
46 | private final int position;
|
---|
47 | private final String role;
|
---|
48 | private final OsmPrimitive child;
|
---|
49 |
|
---|
50 | public RelationToChildReference(Relation parent, int position, String role, OsmPrimitive child) {
|
---|
51 | this.parent = parent;
|
---|
52 | this.position = position;
|
---|
53 | this.role = role;
|
---|
54 | this.child = child;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public RelationToChildReference(Relation parent, int position, RelationMember member) {
|
---|
58 | this.parent = parent;
|
---|
59 | this.position = position;
|
---|
60 | this.role = member.getRole();
|
---|
61 | this.child = member.getMember();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public Relation getParent() {
|
---|
65 | return parent;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public int getPosition() {
|
---|
69 | return position;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public String getRole() {
|
---|
73 | return role;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public OsmPrimitive getChild() {
|
---|
77 | return child;
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public boolean equals(Object obj) {
|
---|
82 | if (this == obj) return true;
|
---|
83 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
84 | RelationToChildReference that = (RelationToChildReference) obj;
|
---|
85 | return position == that.position &&
|
---|
86 | Objects.equals(parent, that.parent) &&
|
---|
87 | Objects.equals(role, that.role) &&
|
---|
88 | Objects.equals(child, that.child);
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public int hashCode() {
|
---|
93 | return Objects.hash(parent, position, role, child);
|
---|
94 | }
|
---|
95 | }
|
---|