001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.pair;
003import static org.openstreetmap.josm.gui.conflict.pair.ListRole.MERGED_ENTRIES;
004import static org.openstreetmap.josm.gui.conflict.pair.ListRole.MY_ENTRIES;
005import static org.openstreetmap.josm.gui.conflict.pair.ListRole.THEIR_ENTRIES;
006import static org.openstreetmap.josm.tools.I18n.tr;
007
008import org.openstreetmap.josm.tools.Utils;
009
010/**
011 * Enumeration of the possible comparison pairs
012 * @since 1650
013 */
014public enum ComparePairType {
015
016    /**
017     * compare my version of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} with their version
018     */
019    MY_WITH_THEIR(tr("My with Their"), MY_ENTRIES, THEIR_ENTRIES),
020
021    /**
022     * compare my version of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} with the merged version
023     */
024    MY_WITH_MERGED(tr("My with Merged"), MY_ENTRIES, MERGED_ENTRIES),
025
026    /**
027     * compare their version of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} with the merged veresion
028     */
029    THEIR_WITH_MERGED(tr("Their with Merged"), THEIR_ENTRIES, MERGED_ENTRIES);
030
031    /** the localized display name */
032    private final String displayName;
033    private final ListRole[] participatingRoles;
034
035    ComparePairType(String displayName, ListRole... participatingRoles) {
036        this.displayName = displayName;
037        this.participatingRoles = Utils.copyArray(participatingRoles);
038    }
039
040    /**
041     * replies the display name
042     *
043     * @return the display name
044     */
045    public String getDisplayName() {
046        return displayName;
047    }
048
049    /**
050     * replies true, if <code>role</code> is participating in this comparison pair
051     *
052     * @param role  the list role
053     * @return true, if <code>role</code> is participating in this comparison pair; false, otherwise
054     */
055    public boolean isParticipatingIn(ListRole role) {
056        for (ListRole r: participatingRoles) {
057            if (r.equals(role)) return true;
058        }
059        return false;
060    }
061
062    /**
063     * replies the pair of {@link ListRole}s participating in this comparison pair
064     *
065     * @return  the pair of list roles
066     */
067    public ListRole[] getParticipatingRoles() {
068        return Utils.copyArray(participatingRoles);
069    }
070
071    /**
072     * replies the opposite role of <code>role</code> participating in this comparison pair
073     *
074     * @param role one of the two roles in this pair
075     * @return the opposite role
076     * @throws IllegalStateException  if role is not participating in this pair
077     */
078    public ListRole getOppositeRole(ListRole role) {
079        if (!isParticipatingIn(role))
080            throw new IllegalStateException(tr("Role {0} is not participating in compare pair {1}.", role.toString(), this.toString()));
081        if (participatingRoles[0].equals(role))
082            return participatingRoles[1];
083        else
084            return participatingRoles[0];
085    }
086}