001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import java.util.Collection;
005import java.util.HashSet;
006import java.util.Set;
007
008public 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    public static Set<RelationToChildReference> getRelationToChildReferences(OsmPrimitive child) {
017        Set<Relation> parents = OsmPrimitive.getFilteredSet(child.getReferrers(), Relation.class);
018        Set<RelationToChildReference> references = new HashSet<>();
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    public static Set<RelationToChildReference> getRelationToChildReferences(Collection<? extends OsmPrimitive> children) {
037        Set<RelationToChildReference> references = new HashSet<>();
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
090    @Override
091    public boolean equals(Object obj) {
092        if (this == obj)
093            return true;
094        if (obj == null)
095            return false;
096        if (getClass() != obj.getClass())
097            return false;
098        RelationToChildReference other = (RelationToChildReference) obj;
099        if (child == null) {
100            if (other.child != null)
101                return false;
102        } else if (!child.equals(other.child))
103            return false;
104        if (parent == null) {
105            if (other.parent != null)
106                return false;
107        } else if (!parent.equals(other.parent))
108            return false;
109        if (position != other.position)
110            return false;
111        if (role == null) {
112            if (other.role != null)
113                return false;
114        } else if (!role.equals(other.role))
115            return false;
116        return true;
117    }
118}