001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.data.osm;
003
004 import java.util.Collection;
005 import java.util.HashSet;
006 import java.util.Set;
007
008 public class RelationToChildReference {
009
010 /**
011 * Replies a set of all {@link RelationToChildReference}s for a given child primitive.
012 *
013 * @param child the child primitive
014 * @return a set of all {@link RelationToChildReference}s for a given child primitive
015 */
016 static public Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
017 Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
018 Set<RelationToChildReference> references = new HashSet<RelationToChildReference>();
019 for (Relation parent: parents) {
020 for (int i=0; i < parent.getMembersCount(); i++) {
021 if (parent.getMember(i).refersTo(child)) {
022 references.add(new RelationToChildReference(parent, i, parent.getMember(i)));
023 }
024 }
025 }
026 return references;
027 }
028
029 /**
030 * Replies a set of all {@link RelationToChildReference}s for a collection of child primitives
031 *
032 * @param children the collection of child primitives
033 * @return a set of all {@link RelationToChildReference}s to the children in the collection of child
034 * primitives
035 */
036 static public Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
037 Set<RelationToChildReference> references = new HashSet<RelationToChildReference>();
038 for (OsmPrimitive child: children) {
039 references.addAll(getRelationToChildReferences(child));
040 }
041 return references;
042 }
043
044 private Relation parent;
045 private int position;
046 private String role;
047 private OsmPrimitive child;
048
049 public RelationToChildReference(Relation parent, int position, String role, OsmPrimitive child) {
050 this.parent = parent;
051 this.position = position;
052 this.role = role;
053 this.child = child;
054 }
055
056 public RelationToChildReference(Relation parent, int position, RelationMember member) {
057 this.parent = parent;
058 this.position = position;
059 this.role = member.getRole();
060 this.child = member.getMember();
061 }
062
063 public Relation getParent() {
064 return parent;
065 }
066
067 public int getPosition() {
068 return position;
069 }
070
071 public String getRole() {
072 return role;
073 }
074
075 public OsmPrimitive getChild() {
076 return child;
077 }
078
079 @Override
080 public int hashCode() {
081 final int prime = 31;
082 int result = 1;
083 result = prime * result + ((child == null) ? 0 : child.hashCode());
084 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
085 result = prime * result + position;
086 result = prime * result + ((role == null) ? 0 : role.hashCode());
087 return result;
088 }
089 @Override
090 public boolean equals(Object obj) {
091 if (this == obj)
092 return true;
093 if (obj == null)
094 return false;
095 if (getClass() != obj.getClass())
096 return false;
097 RelationToChildReference other = (RelationToChildReference) obj;
098 if (child == null) {
099 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 }