001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.tags;
003
004import static org.openstreetmap.josm.gui.conflict.tags.RelationMemberConflictDecisionType.UNDECIDED;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import org.openstreetmap.josm.data.osm.OsmPrimitive;
008import org.openstreetmap.josm.data.osm.Relation;
009import org.openstreetmap.josm.data.osm.RelationMember;
010import org.openstreetmap.josm.tools.CheckParameterUtil;
011
012public class RelationMemberConflictDecision {
013
014    private Relation relation;
015    private int pos;
016    private OsmPrimitive originalPrimitive;
017    private String role;
018    private RelationMemberConflictDecisionType decision;
019
020    public RelationMemberConflictDecision(Relation relation, int pos) throws IllegalArgumentException {
021        CheckParameterUtil.ensureParameterNotNull(relation, "relation");
022        RelationMember member = relation.getMember(pos);
023        if (member == null)
024            throw new IndexOutOfBoundsException(tr("Position {0} is out of range. Current number of members is {1}.", pos, relation.getMembersCount()));
025        this.relation = relation;
026        this.pos  = pos;
027        this.originalPrimitive = member.getMember();
028        this.role = member.hasRole()? member.getRole() : "";
029        this.decision = UNDECIDED;
030    }
031
032    public Relation getRelation() {
033        return relation;
034    }
035
036    public int getPos() {
037        return pos;
038    }
039
040    public OsmPrimitive getOriginalPrimitive() {
041        return originalPrimitive;
042    }
043
044    public String getRole() {
045        return role;
046    }
047
048    public RelationMemberConflictDecisionType getDecision() {
049        return decision;
050    }
051
052    public void setRole(String role) {
053        this.role = role == null ? "" : role;
054    }
055
056    public void decide(RelationMemberConflictDecisionType decision) {
057        if (decision == null) {
058            decision = UNDECIDED;
059        }
060        this.decision = decision;
061    }
062
063    public boolean isDecided() {
064        return ! UNDECIDED.equals(decision);
065    }
066
067    public boolean matches(Relation relation, int pos) {
068        return this.relation == relation && this.pos == pos;
069    }
070
071    @Override
072    public int hashCode() {
073        final int prime = 31;
074        int result = 1;
075        result = prime * result + ((decision == null) ? 0 : decision.hashCode());
076        result = prime * result + ((originalPrimitive == null) ? 0 : originalPrimitive.hashCode());
077        result = prime * result + pos;
078        result = prime * result + ((relation == null) ? 0 : relation.hashCode());
079        result = prime * result + ((role == null) ? 0 : role.hashCode());
080        return result;
081    }
082
083    @Override
084    public boolean equals(Object obj) {
085        if (this == obj)
086            return true;
087        if (obj == null)
088            return false;
089        if (getClass() != obj.getClass())
090            return false;
091        RelationMemberConflictDecision other = (RelationMemberConflictDecision) obj;
092        if (decision == null) {
093            if (other.decision != null)
094                return false;
095        } else if (!decision.equals(other.decision))
096            return false;
097        if (originalPrimitive == null) {
098            if (other.originalPrimitive != null)
099                return false;
100        } else if (!originalPrimitive.equals(other.originalPrimitive))
101            return false;
102        if (pos != other.pos)
103            return false;
104        if (relation == null) {
105            if (other.relation != null)
106                return false;
107        } else if (!relation.equals(other.relation))
108            return false;
109        if (role == null) {
110            if (other.role != null)
111                return false;
112        } else if (!role.equals(other.role))
113            return false;
114        return true;
115    }
116}