Repository: graphhopper/graphhopper
Pull request #2318 (2021-05-14 08:35:08)

Convert LocationIndex.Visitor into a functional interface


Patch:
diff --git a/core/src/main/java/com/graphhopper/storage/GraphEdgeIdFinder.java b/core/src/main/java/com/graphhopper/storage/GraphEdgeIdFinder.java
index 22ce3ab78..fbc15a996 100644
--- a/core/src/main/java/com/graphhopper/storage/GraphEdgeIdFinder.java
+++ b/core/src/main/java/com/graphhopper/storage/GraphEdgeIdFinder.java
@@ -83,13 +83,10 @@ public class GraphEdgeIdFinder {
      */
     private GHIntHashSet findEdgesInShape(final Shape shape, EdgeFilter filter) {
         GHIntHashSet edgeIds = new GHIntHashSet();
-        locationIndex.query(shape.getBounds(), new LocationIndex.Visitor() {
-            @Override
-            public void onEdge(int edgeId) {
-                EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
-                if (filter.accept(edge) && shape.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()))
-                    edgeIds.add(edge.getEdge());
-            }
+        locationIndex.query(shape.getBounds(), edgeId -> {
+            EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
+            if (filter.accept(edge) && shape.intersects(edge.fetchWayGeometry(FetchMode.ALL).makeImmutable()))
+                edgeIds.add(edge.getEdge());
         });
         return edgeIds;
     }
diff --git a/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java b/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java
index acf892f13..7a9f9c568 100644
--- a/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java
+++ b/core/src/main/java/com/graphhopper/storage/index/LocationIndex.java
@@ -57,18 +57,20 @@ public interface LocationIndex extends Storable<LocationIndex> {
     /**
      * This interface allows to visit edges stored in the LocationIndex.
      */
-    abstract class Visitor {
-        public boolean isTileInfo() {
+    @FunctionalInterface
+    interface Visitor {
+
+        void onEdge(int edgeId);
+        
+        default boolean isTileInfo() {
             return false;
         }
 
         /**
          * This method is called if isTileInfo returns true.
          */
-        public void onTile(BBox bbox, int depth) {
+        default void onTile(BBox bbox, int depth) {
         }
-
-        public abstract void onEdge(int edgeId);
     }
 
 }
diff --git a/map-matching/src/main/java/com/graphhopper/matching/MapMatching.java b/map-matching/src/main/java/com/graphhopper/matching/MapMatching.java
index e3009c717..fb2fa01c1 100644
--- a/map-matching/src/main/java/com/graphhopper/matching/MapMatching.java
+++ b/map-matching/src/main/java/com/graphhopper/matching/MapMatching.java
@@ -236,33 +236,29 @@ public class MapMatching {
         List<Snap> snaps = new ArrayList<>();
         IntHashSet seenEdges = new IntHashSet();
         IntHashSet seenNodes = new IntHashSet();
-        locationIndex.query(queryShape,
-                new LocationIndex.Visitor() {
-                    @Override
-                    public void onEdge(int edgeId) {
-                        EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
-                        if (seenEdges.add(edgeId) && edgeFilter.accept(edge)) {
-                            Snap snap = new Snap(queryLat, queryLon);
-                            locationIndex.traverseEdge(queryLat, queryLon, edge, (node, normedDist, wayIndex, pos) -> {
-                                if (normedDist < snap.getQueryDistance()) {
-                                    snap.setQueryDistance(normedDist);
-                                    snap.setClosestNode(node);
-                                    snap.setWayIndex(wayIndex);
-                                    snap.setSnappedPosition(pos);
-                                }
-                            });
-                            double dist = DIST_PLANE.calcDenormalizedDist(snap.getQueryDistance());
-                            snap.setClosestEdge(edge);
-                            snap.setQueryDistance(dist);
-                            if (snap.isValid() && (snap.getSnappedPosition() != Snap.Position.TOWER || seenNodes.add(snap.getClosestNode()))) {
-                                snap.calcSnappedPoint(DistanceCalcEarth.DIST_EARTH);
-                                if (queryShape.contains(snap.getSnappedPoint().lat, snap.getSnappedPoint().lon)) {
-                                    snaps.add(snap);
-                                }
-                            }
-                        }
+        locationIndex.query(queryShape, edgeId -> {
+            EdgeIteratorState edge = graph.getEdgeIteratorStateForKey(edgeId * 2);
+            if (seenEdges.add(edgeId) && edgeFilter.accept(edge)) {
+                Snap snap = new Snap(queryLat, queryLon);
+                locationIndex.traverseEdge(queryLat, queryLon, edge, (node, normedDist, wayIndex, pos) -> {
+                    if (normedDist < snap.getQueryDistance()) {
+                        snap.setQueryDistance(normedDist);
+                        snap.setClosestNode(node);
+                        snap.setWayIndex(wayIndex);
+                        snap.setSnappedPosition(pos);
                     }
                 });
+                double dist = DIST_PLANE.calcDenormalizedDist(snap.getQueryDistance());
+                snap.setClosestEdge(edge);
+                snap.setQueryDistance(dist);
+                if (snap.isValid() && (snap.getSnappedPosition() != Snap.Position.TOWER || seenNodes.add(snap.getClosestNode()))) {
+                    snap.calcSnappedPoint(DistanceCalcEarth.DIST_EARTH);
+                    if (queryShape.contains(snap.getSnappedPoint().lat, snap.getSnappedPoint().lon)) {
+                        snaps.add(snap);
+                    }
+                }
+            }
+        });
         return snaps;
     }
 
diff --git a/web-bundle/src/main/java/com/graphhopper/resources/MVTResource.java b/web-bundle/src/main/java/com/graphhopper/resources/MVTResource.java
index d7ff0f3f1..dab321440 100644
--- a/web-bundle/src/main/java/com/graphhopper/resources/MVTResource.java
+++ b/web-bundle/src/main/java/com/graphhopper/resources/MVTResource.java
@@ -143,10 +143,6 @@ public class MVTResource {
 
 
             }
-            @Override
-            public void onTile(BBox bbox, int depth) {
-            }
-
         });
 
         MvtLayerBuild.writeProps(layerBuilder, layerProps);
diff --git a/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java b/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java
index 2b1220352..65790777d 100644
--- a/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java
+++ b/web-bundle/src/main/java/com/graphhopper/resources/PtIsochroneResource.java
@@ -128,13 +128,10 @@ public class PtIsochroneResource {
 
             // Get at least all nodes within our bounding box (I think convex hull would be enough.)
             // I think then we should have all possible encroaching points. (Proof needed.)
-            locationIndex.query(BBox.fromEnvelope(exploredPoints.getEnvelopeInternal()), new LocationIndex.Visitor() {
-                @Override
-                public void onEdge(int edgeId) {
-                    EdgeIteratorState edge = queryGraph.getEdgeIteratorStateForKey(edgeId * 2);
-                    z1.merge(new Coordinate(nodeAccess.getLon(edge.getBaseNode()), nodeAccess.getLat(edge.getBaseNode())), Double.MAX_VALUE, Math::min);
-                    z1.merge(new Coordinate(nodeAccess.getLon(edge.getAdjNode()), nodeAccess.getLat(edge.getAdjNode())), Double.MAX_VALUE, Math::min);
-                }
+            locationIndex.query(BBox.fromEnvelope(exploredPoints.getEnvelopeInternal()), edgeId -> {
+                EdgeIteratorState edge = queryGraph.getEdgeIteratorStateForKey(edgeId * 2);
+                z1.merge(new Coordinate(nodeAccess.getLon(edge.getBaseNode()), nodeAccess.getLat(edge.getBaseNode())), Double.MAX_VALUE, Math::min);
+                z1.merge(new Coordinate(nodeAccess.getLon(edge.getAdjNode()), nodeAccess.getLat(edge.getAdjNode())), Double.MAX_VALUE, Math::min);
             });
             exploredPoints = geometryFactory.createMultiPointFromCoords(z1.keySet().toArray(new Coordinate[0]));
 


Test patch:
diff --git a/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java b/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java
index 9f0751c4e..c2e92c4c5 100644
--- a/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java
+++ b/core/src/test/java/com/graphhopper/reader/osm/GraphHopperOSMTest.java
@@ -174,21 +174,14 @@ public class GraphHopperOSMTest {
         LocationIndexTree index = (LocationIndexTree) gh.getLocationIndex();
         final EdgeExplorer edgeExplorer = gh.getGraphHopperStorage().createEdgeExplorer();
         final BBox bbox = new BBox(7.422, 7.429, 43.729, 43.734);
-        index.query(bbox, new LocationIndexTree.Visitor() {
-            @Override
-            public void onTile(BBox bbox, int width) {
-            }
-
-            @Override
-            public void onEdge(int edgeId) {
-                EdgeIteratorState edge = gh.getGraphHopperStorage().getEdgeIteratorStateForKey(edgeId * 2);
-                for (int i = 0; i < 2; i++) {
-                    int nodeId = i == 0 ? edge.getBaseNode() : edge.getAdjNode();
-                    double lat = na.getLat(nodeId);
-                    double lon = na.getLon(nodeId);
-                    if (bbox.contains(lat, lon))
-                        indexNodeList.add(nodeId);
-                }
+        index.query(bbox, edgeId -> {
+            EdgeIteratorState edge = gh.getGraphHopperStorage().getEdgeIteratorStateForKey(edgeId * 2);
+            for (int i = 0; i < 2; i++) {
+                int nodeId = i == 0 ? edge.getBaseNode() : edge.getAdjNode();
+                double lat = na.getLat(nodeId);
+                double lon = na.getLon(nodeId);
+                if (bbox.contains(lat, lon))
+                    indexNodeList.add(nodeId);
             }
         });
 
diff --git a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java
index 7bbe8f7e8..4b6c272c4 100644
--- a/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java
+++ b/core/src/test/java/com/graphhopper/storage/index/LocationIndexTreeTest.java
@@ -131,12 +131,7 @@ public class LocationIndexTreeTest {
         Graph graph = createTestGraph2();
         LocationIndexTree index = (LocationIndexTree) createIndexNoPrepare(graph, 500).prepareIndex();
         final HashSet<Integer> edges = new HashSet<>();
-        index.query(graph.getBounds(), new LocationIndex.Visitor() {
-            @Override
-            public void onEdge(int edgeId) {
-                edges.add(edgeId);
-            }
-        });
+        index.query(graph.getBounds(), edges::add);
         // All edges (see testgraph2.jpg)
         assertEquals(edges.size(), graph.getEdges());
     }
@@ -147,12 +142,7 @@ public class LocationIndexTreeTest {
         LocationIndexTree index = (LocationIndexTree) createIndexNoPrepare(graph, 500).prepareIndex();
         final IntArrayList edges = new IntArrayList();
         BBox bbox = new BBox(11.57314, 11.57614, 49.94553, 49.94853);
-        index.query(bbox, new LocationIndex.Visitor() {
-            @Override
-            public void onEdge(int edgeId) {
-                edges.add(edgeId);
-            }
-        });
+        index.query(bbox, edges::add);
         // Also all edges (see testgraph2.jpg)
         assertEquals(edges.size(), graph.getEdges());
     }