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 final Relation relation;
015    private final int pos;
016    private final OsmPrimitive originalPrimitive;
017    private String role;
018    private RelationMemberConflictDecisionType decision;
019
020    public RelationMemberConflictDecision(Relation relation, int pos) {
021        CheckParameterUtil.ensureParameterNotNull(relation, "relation");
022        RelationMember member = relation.getMember(pos);
023        if (member == null)
024            throw new IndexOutOfBoundsException(
025                    tr("Position {0} is out of range. Current number of members is {1}.", pos, relation.getMembersCount()));
026        this.relation = relation;
027        this.pos  = pos;
028        this.originalPrimitive = member.getMember();
029        this.role = member.hasRole() ? member.getRole() : "";
030        this.decision = UNDECIDED;
031    }
032
033    public Relation getRelation() {
034        return relation;
035    }
036
037    public int getPos() {
038        return pos;
039    }
040
041    public OsmPrimitive getOriginalPrimitive() {
042        return originalPrimitive;
043    }
044
045    public String getRole() {
046        return role;
047    }
048
049    public RelationMemberConflictDecisionType getDecision() {
050        return decision;
051    }
052
053    public void setRole(String role) {
054        this.role = role == null ? "" : role;
055    }
056
057    public void decide(RelationMemberConflictDecisionType decision) {
058        if (decision == null) {
059            decision = UNDECIDED;
060        }
061        this.decision = decision;
062    }
063
064    public boolean isDecided() {
065        return !UNDECIDED.equals(decision);
066    }
067
068    public boolean matches(Relation relation, int pos) {
069        return this.relation == relation && this.pos == pos;
070    }
071
072    @Override
073    public int hashCode() {
074        final int prime = 31;
075        int result = 1;
076        result = prime * result + ((decision == null) ? 0 : decision.hashCode());
077        result = prime * result + ((originalPrimitive == null) ? 0 : originalPrimitive.hashCode());
078        result = prime * result + pos;
079        result = prime * result + ((relation == null) ? 0 : relation.hashCode());
080        result = prime * result + ((role == null) ? 0 : role.hashCode());
081        return result;
082    }
083
084    @Override
085    public boolean equals(Object obj) {
086        if (this == obj)
087            return true;
088        if (obj == null)
089            return false;
090        if (getClass() != obj.getClass())
091            return false;
092        RelationMemberConflictDecision other = (RelationMemberConflictDecision) obj;
093        if (decision == null) {
094            if (other.decision != null)
095                return false;
096        } else if (!decision.equals(other.decision))
097            return false;
098        if (originalPrimitive == null) {
099            if (other.originalPrimitive != null)
100                return false;
101        } else if (!originalPrimitive.equals(other.originalPrimitive))
102            return false;
103        if (pos != other.pos)
104            return false;
105        if (relation == null) {
106            if (other.relation != null)
107                return false;
108        } else if (!relation.equals(other.relation))
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
118    @Override
119    public String toString() {
120        return originalPrimitive.getPrimitiveId() + " at index " + pos + " with role " + role + " in " + relation.getUniqueId()
121            + " => " + decision;
122    }
123}