001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import java.util.Collection;
005import java.util.LinkedList;
006import java.util.Objects;
007import java.util.Set;
008import java.util.TreeSet;
009
010import org.openstreetmap.josm.data.osm.DataSet;
011import org.openstreetmap.josm.data.osm.Node;
012import org.openstreetmap.josm.data.osm.OsmPrimitive;
013import org.openstreetmap.josm.data.osm.Way;
014import org.openstreetmap.josm.tools.Utils;
015
016/**
017 * Auxiliary class for the {@link SelectNonBranchingWaySequencesAction}.
018 *
019 * @author Marko Mäkelä
020 */
021public class SelectNonBranchingWaySequences {
022    /**
023     * outer endpoints of selected ways
024     */
025    private final Set<Node> outerNodes;
026    /**
027     * endpoints of selected ways
028     */
029    private final Set<Node> nodes;
030
031    /**
032     * Creates a way selection
033     *
034     * @param ways selection a selection of ways
035     */
036    public SelectNonBranchingWaySequences(final Collection<Way> ways) {
037        if (ways.isEmpty()) {
038            // The selection cannot be extended.
039            outerNodes = null;
040            nodes = null;
041        } else {
042            nodes = new TreeSet<>();
043            outerNodes = new TreeSet<>();
044
045            for (Way way : ways) {
046                addNodes(way);
047            }
048        }
049    }
050
051    /**
052     * Add a way endpoint to nodes, outerNodes
053     *
054     * @param node a way endpoint
055     */
056    private void addNodes(Node node) {
057        if (node == null) return;
058        if (!nodes.add(node)) {
059            outerNodes.remove(node);
060        } else {
061            outerNodes.add(node);
062        }
063    }
064
065    /**
066     * Add the endpoints of the way to nodes, outerNodes
067     *
068     * @param way a way whose endpoints are added
069     */
070    private void addNodes(Way way) {
071        addNodes(way.firstNode());
072        addNodes(way.lastNode());
073    }
074
075    /**
076     * Find out if the selection can be extended
077     *
078     * @return true if the selection can be extended
079     */
080    public boolean canExtend() {
081        return !Utils.isEmpty(outerNodes);
082    }
083
084    /**
085     * Finds out if the current selection can be extended.
086     *
087     * @param selection current selection (ways and others)
088     * @param node      perimeter node from which to extend the selection
089     * @return a way by which to extend the selection, or null
090     */
091    private static Way findWay(Collection<OsmPrimitive> selection, Node node) {
092        Way foundWay = null;
093
094        for (Way way : node.getParentWays()) {
095            if (way.getNodesCount() < 2 || !way.isFirstLastNode(node)
096                    || !way.isSelectable()
097                    || selection.contains(way))
098                continue;
099
100            /* A previously unselected way was found that is connected
101            to the node. */
102            if (foundWay != null)
103                /* This is not the only qualifying way. There is a
104                branch at the node, and we cannot extend the selection. */
105                return null;
106
107            /* Remember the first found qualifying way. */
108            foundWay = way;
109        }
110
111        /* Return the only way found, or null if none was found. */
112        return foundWay;
113    }
114
115    /**
116     * Finds out if the current selection can be extended.
117     * <p>
118     * The members outerNodes, nodes must have been initialized.
119     * How to update these members when extending the selection, @see extend().
120     * </p>
121     * @param selection current selection
122     * @return a way by which to extend the selection, or null
123     */
124    private Way findWay(Collection<OsmPrimitive> selection) {
125        return outerNodes.stream()
126                .map(node -> findWay(selection, node))
127                .filter(Objects::nonNull)
128                .findFirst().orElse(null);
129
130    }
131
132    /**
133     * Extend the current selection
134     *
135     * @param data the data set in which to extend the selection
136     */
137    public void extend(DataSet data) {
138        if (!canExtend())
139            return;
140
141        Collection<OsmPrimitive> currentSelection = data.getSelected();
142
143        Way way = findWay(currentSelection);
144
145        if (way == null)
146            return;
147
148        boolean selectionChanged = false;
149        Collection<OsmPrimitive> selection = new LinkedList<>(currentSelection);
150
151        do {
152            if (!selection.add(way))
153                break;
154
155            selectionChanged = true;
156            addNodes(way);
157
158            way = findWay(selection);
159        } while (way != null);
160
161        if (selectionChanged)
162            data.setSelected(selection);
163    }
164}