Changeset 12478 in josm


Ignore:
Timestamp:
2017-07-16T12:33:37+02:00 (7 years ago)
Author:
Don-vip
Message:

NodeGraph: add javadoc, unit tests

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/NodeGraph.java

    r12463 r12478  
    2121 */
    2222public class NodeGraph {
     23
     24    /**
     25     * Builds a list of pair of nodes from the given way.
     26     * @param way way
     27     * @param directed if {@code true} each pair of nodes will occur once, in the way nodes order.
     28     *                 if {@code false} each pair of nodes will occur twice (the pair and its inversed copy)
     29     * @return a list of pair of nodes from the given way
     30     */
    2331    public static List<NodePair> buildNodePairs(Way way, boolean directed) {
    2432        List<NodePair> pairs = new ArrayList<>();
     
    3240    }
    3341
     42    /**
     43     * Builds a list of pair of nodes from the given ways.
     44     * @param ways ways
     45     * @param directed if {@code true} each pair of nodes will occur once, in the way nodes order.
     46     *                 if {@code false} each pair of nodes will occur twice (the pair and its inversed copy)
     47     * @return a list of pair of nodes from the given ways
     48     */
    3449    public static List<NodePair> buildNodePairs(List<Way> ways, boolean directed) {
    3550        List<NodePair> pairs = new ArrayList<>();
     
    4055    }
    4156
     57    /**
     58     * Builds a new list of pair nodes without the duplicated pairs (including inversed copies).
     59     * @param pairs existing list of pairs
     60     * @return a new list of pair nodes without the duplicated pairs
     61     */
    4262    public static List<NodePair> eliminateDuplicateNodePairs(List<NodePair> pairs) {
    4363        List<NodePair> cleaned = new ArrayList<>();
     
    242262     */
    243263    protected List<Node> buildSpanningPath(Node startNode) {
    244         if (startNode == null)
    245             return null;
    246         Stack<NodePair> path = new Stack<>();
    247         Stack<NodePair> nextPairs = new Stack<>();
    248         nextPairs.addAll(getOutboundPairs(startNode));
    249         while (!nextPairs.isEmpty()) {
    250             NodePair cur = nextPairs.pop();
    251             if (!path.contains(cur) && !path.contains(cur.swap())) {
    252                 while (!path.isEmpty() && !path.peek().isPredecessorOf(cur)) {
    253                     path.pop();
     264        if (startNode != null) {
     265            Stack<NodePair> path = new Stack<>();
     266            Stack<NodePair> nextPairs = new Stack<>();
     267            nextPairs.addAll(getOutboundPairs(startNode));
     268            while (!nextPairs.isEmpty()) {
     269                NodePair cur = nextPairs.pop();
     270                if (!path.contains(cur) && !path.contains(cur.swap())) {
     271                    while (!path.isEmpty() && !path.peek().isPredecessorOf(cur)) {
     272                        path.pop();
     273                    }
     274                    path.push(cur);
     275                    if (isSpanningWay(path)) return buildPathFromNodePairs(path);
     276                    nextPairs.addAll(getOutboundPairs(path.peek()));
    254277                }
    255                 path.push(cur);
    256                 if (isSpanningWay(path)) return buildPathFromNodePairs(path);
    257                 nextPairs.addAll(getOutboundPairs(path.peek()));
    258             }
    259         }
    260         return null;
     278            }
     279        }
     280        return Collections.emptyList();
    261281    }
    262282
     
    280300        for (Node n: nodes) {
    281301            List<Node> path = buildSpanningPath(n);
    282             if (path != null)
     302            if (!path.isEmpty())
    283303                return path;
    284304        }
Note: See TracChangeset for help on using the changeset viewer.