001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command.conflict;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.List;
008import java.util.Objects;
009
010import javax.swing.Icon;
011
012import org.openstreetmap.josm.data.conflict.Conflict;
013import org.openstreetmap.josm.data.osm.Node;
014import org.openstreetmap.josm.data.osm.OsmPrimitive;
015import org.openstreetmap.josm.data.osm.Way;
016import org.openstreetmap.josm.tools.ImageProvider;
017import org.openstreetmap.josm.tools.Logging;
018
019/**
020 * Represents the resolution of conflicts in the node list of two {@link Way}s.
021 *
022 */
023public class WayNodesConflictResolverCommand extends ConflictResolveCommand {
024    /** the conflict to resolve */
025    private final Conflict<Way> conflict;
026
027    /** the list of merged nodes. This becomes the list of news of my way after the
028     *  command is executed
029     */
030    private final List<Node> mergedNodeList;
031
032    /**
033     * Constructs a new {@code WayNodesConflictResolverCommand}.
034     * @param conflict the conflict data set
035     * @param mergedNodeList the list of merged nodes
036     */
037    @SuppressWarnings("unchecked")
038    public WayNodesConflictResolverCommand(Conflict<? extends OsmPrimitive> conflict, List<Node> mergedNodeList) {
039        super(conflict.getMy().getDataSet());
040        this.conflict = (Conflict<Way>) conflict;
041        this.mergedNodeList = mergedNodeList;
042    }
043
044    @Override
045    public String getDescriptionText() {
046        return tr("Resolve conflicts in node list of way {0}", conflict.getMy().getId());
047    }
048
049    @Override
050    public Icon getDescriptionIcon() {
051        return ImageProvider.get("data", "object");
052    }
053
054    @Override
055    public boolean executeCommand() {
056        // remember the current state of 'my' way
057        //
058        super.executeCommand();
059
060        // replace the list of nodes of 'my' way by the list of merged nodes
061        //
062        for (Node n:mergedNodeList) {
063            if (!getAffectedDataSet().getNodes().contains(n)) {
064                Logging.warn(tr("Main dataset does not include node {0}", n.toString()));
065            }
066        }
067        conflict.getMy().setNodes(mergedNodeList);
068        rememberConflict(conflict);
069        return true;
070    }
071
072    @Override
073    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted,
074            Collection<OsmPrimitive> added) {
075        modified.add(conflict.getMy());
076    }
077
078    @Override
079    public int hashCode() {
080        return Objects.hash(super.hashCode(), conflict, mergedNodeList);
081    }
082
083    @Override
084    public boolean equals(Object obj) {
085        if (this == obj) return true;
086        if (obj == null || getClass() != obj.getClass()) return false;
087        if (!super.equals(obj)) return false;
088        WayNodesConflictResolverCommand that = (WayNodesConflictResolverCommand) obj;
089        return Objects.equals(conflict, that.conflict) &&
090                Objects.equals(mergedNodeList, that.mergedNodeList);
091    }
092}