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