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 java.util.Objects;
008
009import org.openstreetmap.josm.data.osm.OsmPrimitive;
010import org.openstreetmap.josm.data.osm.Relation;
011import org.openstreetmap.josm.data.osm.RelationMember;
012import org.openstreetmap.josm.tools.CheckParameterUtil;
013
014public class RelationMemberConflictDecision {
015
016    private final Relation relation;
017    private final int pos;
018    private final OsmPrimitive originalPrimitive;
019    private String role;
020    private RelationMemberConflictDecisionType decision;
021
022    public RelationMemberConflictDecision(Relation relation, int pos) {
023        CheckParameterUtil.ensureParameterNotNull(relation, "relation");
024        RelationMember member = relation.getMember(pos);
025        if (member == null)
026            throw new IndexOutOfBoundsException(
027                    tr("Position {0} is out of range. Current number of members is {1}.", pos, relation.getMembersCount()));
028        this.relation = relation;
029        this.pos = pos;
030        this.originalPrimitive = member.getMember();
031        this.role = member.hasRole() ? member.getRole() : "";
032        this.decision = UNDECIDED;
033    }
034
035    public Relation getRelation() {
036        return relation;
037    }
038
039    public int getPos() {
040        return pos;
041    }
042
043    public OsmPrimitive getOriginalPrimitive() {
044        return originalPrimitive;
045    }
046
047    public String getRole() {
048        return role;
049    }
050
051    public RelationMemberConflictDecisionType getDecision() {
052        return decision;
053    }
054
055    public void setRole(String role) {
056        this.role = role == null ? "" : role;
057    }
058
059    public void decide(RelationMemberConflictDecisionType decision) {
060        if (decision == null) {
061            decision = UNDECIDED;
062        }
063        this.decision = decision;
064    }
065
066    public boolean isDecided() {
067        return !UNDECIDED.equals(decision);
068    }
069
070    public boolean matches(Relation relation, int pos) {
071        return this.relation == relation && this.pos == pos;
072    }
073
074    @Override
075    public int hashCode() {
076        return Objects.hash(relation, pos, originalPrimitive, role, decision);
077    }
078
079    @Override
080    public boolean equals(Object obj) {
081        if (this == obj) return true;
082        if (obj == null || getClass() != obj.getClass()) return false;
083        RelationMemberConflictDecision that = (RelationMemberConflictDecision) obj;
084        return pos == that.pos &&
085                Objects.equals(relation, that.relation) &&
086                Objects.equals(originalPrimitive, that.originalPrimitive) &&
087                Objects.equals(role, that.role) &&
088                decision == that.decision;
089    }
090
091    @Override
092    public String toString() {
093        return originalPrimitive.getPrimitiveId() + " at index " + pos + " with role " + role + " in " + relation.getUniqueId()
094            + " => " + decision;
095    }
096}