[2711] | 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;
|
---|
[9371] | 6 | import java.util.Objects;
|
---|
[2711] | 7 | import java.util.Set;
|
---|
| 8 |
|
---|
[12190] | 9 | /**
|
---|
| 10 | * This is an extension of {@link RelationMember} that stores the parent relation and the index in it in addition to the role/child.
|
---|
| 11 | */
|
---|
[2711] | 12 | public class RelationToChildReference {
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
[5266] | 15 | * Replies a set of all {@link RelationToChildReference}s for a given child primitive.
|
---|
[2711] | 16 | *
|
---|
| 17 | * @param child the child primitive
|
---|
[5266] | 18 | * @return a set of all {@link RelationToChildReference}s for a given child primitive
|
---|
[2711] | 19 | */
|
---|
[6889] | 20 | public static Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
|
---|
[2711] | 21 | Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
|
---|
[7005] | 22 | Set<RelationToChildReference> references = new HashSet<>();
|
---|
[2711] | 23 | for (Relation parent: parents) {
|
---|
[8510] | 24 | for (int i = 0; i < parent.getMembersCount(); i++) {
|
---|
[2711] | 25 | if (parent.getMember(i).refersTo(child)) {
|
---|
| 26 | references.add(new RelationToChildReference(parent, i, parent.getMember(i)));
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 | return references;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[5266] | 34 | * Replies a set of all {@link RelationToChildReference}s for a collection of child primitives
|
---|
[2711] | 35 | *
|
---|
| 36 | * @param children the collection of child primitives
|
---|
[5266] | 37 | * @return a set of all {@link RelationToChildReference}s to the children in the collection of child
|
---|
[2711] | 38 | * primitives
|
---|
| 39 | */
|
---|
[6889] | 40 | public static Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
|
---|
[7005] | 41 | Set<RelationToChildReference> references = new HashSet<>();
|
---|
[2711] | 42 | for (OsmPrimitive child: children) {
|
---|
| 43 | references.addAll(getRelationToChildReferences(child));
|
---|
| 44 | }
|
---|
| 45 | return references;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[9067] | 48 | private final Relation parent;
|
---|
| 49 | private final int position;
|
---|
| 50 | private final String role;
|
---|
| 51 | private final OsmPrimitive child;
|
---|
[2711] | 52 |
|
---|
[12190] | 53 | /**
|
---|
| 54 | * Create a new {@link RelationToChildReference}
|
---|
| 55 | * @param parent The parent relation
|
---|
| 56 | * @param position The position of the child in the parent
|
---|
| 57 | * @param role The role of the child
|
---|
| 58 | * @param child The actual child (member of parent)
|
---|
| 59 | */
|
---|
[2711] | 60 | public RelationToChildReference(Relation parent, int position, String role, OsmPrimitive child) {
|
---|
| 61 | this.parent = parent;
|
---|
| 62 | this.position = position;
|
---|
| 63 | this.role = role;
|
---|
| 64 | this.child = child;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[12190] | 67 | /**
|
---|
| 68 | * Create a new {@link RelationToChildReference}
|
---|
| 69 | * @param parent The parent relation
|
---|
| 70 | * @param position The position of the child in the parent
|
---|
| 71 | * @param member The role and relation for the child
|
---|
| 72 | */
|
---|
[2711] | 73 | public RelationToChildReference(Relation parent, int position, RelationMember member) {
|
---|
[12190] | 74 | this(parent, position, member.getRole(), member.getMember());
|
---|
[2711] | 75 | }
|
---|
| 76 |
|
---|
[12190] | 77 | /**
|
---|
| 78 | * Get the parent relation
|
---|
| 79 | * @return The parent
|
---|
| 80 | */
|
---|
[2711] | 81 | public Relation getParent() {
|
---|
| 82 | return parent;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[12190] | 85 | /**
|
---|
| 86 | * Get the position of the child in the parent
|
---|
| 87 | * @return The position of the child
|
---|
| 88 | */
|
---|
[2711] | 89 | public int getPosition() {
|
---|
| 90 | return position;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[12190] | 93 | /**
|
---|
| 94 | * Get the role of the child
|
---|
| 95 | * @return The role
|
---|
| 96 | */
|
---|
[2711] | 97 | public String getRole() {
|
---|
| 98 | return role;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[12190] | 101 | /**
|
---|
| 102 | * Get the actual child
|
---|
| 103 | * @return The child
|
---|
| 104 | */
|
---|
[2711] | 105 | public OsmPrimitive getChild() {
|
---|
| 106 | return child;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Override
|
---|
[9371] | 110 | public boolean equals(Object obj) {
|
---|
| 111 | if (this == obj) return true;
|
---|
| 112 | if (obj == null || getClass() != obj.getClass()) return false;
|
---|
| 113 | RelationToChildReference that = (RelationToChildReference) obj;
|
---|
| 114 | return position == that.position &&
|
---|
| 115 | Objects.equals(parent, that.parent) &&
|
---|
| 116 | Objects.equals(role, that.role) &&
|
---|
| 117 | Objects.equals(child, that.child);
|
---|
[2711] | 118 | }
|
---|
[8510] | 119 |
|
---|
[2711] | 120 | @Override
|
---|
[9371] | 121 | public int hashCode() {
|
---|
| 122 | return Objects.hash(parent, position, role, child);
|
---|
[2711] | 123 | }
|
---|
| 124 | }
|
---|