@VisibleForTesting void addSourcePointInternal(Point2D source, boolean isCity) { synchronized (lock) { if (!heuristic.containsKey(source)) { heuristicProvider.addSourceNodeToHeuristic(source); } if (graph.containsNode(source)) { return; } List<Point2D> nearest = new ArrayList<>(); for (Point2D node : graph) { double distance = node.distance(source); if (distance <= DIAG_CHECK_DISTANCE) { nearest.add(node); } } graph.addNode(source, false); for (Point2D point : nearest) { addEdges(source, point, isCity, false); } } } @Override
private void addEdges(Point2D from, Point2D to, boolean isCity, boolean initial) { if (isCity || isOnSea(to)) { if (!graph.containsNode(to)) { if (initial) { graph.addNodeInternal(to); } else { graph.addNode(to, true); } } heuristicProvider.getHeuristic(); double weight = calculateWeight(from, to); if (initial) { graph.addEdgeInternal(from, to, weight); } else { graph.addEdge(from, to, weight); } weight = calculateWeight(to, from); if (initial) { graph.addEdgeInternal(to, from, weight); } else { graph.addEdge(to, from, weight); } } }
@VisibleForTesting void addDestinationPointInternal(Point2D newPoint, boolean isCity) { synchronized (lock) { if (!heuristic.containsKey(newPoint)) { heuristicProvider.addSourceNodeToHeuristic(newPoint); // once reached there we must leave again. heuristicProvider.addTargetNodeToHeuristic(newPoint); heuristicProvider.getHeuristic(); } Preconditions.checkArgument(heuristic.containsKey(newPoint), "Destination not part of the heuristic: "+newPoint); if (graph.containsNode(newPoint)) { return; } List<Point2D> nearest = new ArrayList<>(); for (Point2D node : graph) { double distance = node.distance(newPoint); if (distance <= DIAG_CHECK_DISTANCE) { nearest.add(node); } } graph.addNode(newPoint, true); for (Point2D point : nearest) { addEdges(newPoint, point, isCity, false); } } } @VisibleForTesting