MapElementGridSet extends AbstractGISGridSet<P> implements GISElementSet<P> { @Override @Pure public final P getNearest(Point2D<?, ?> position) { final Pair<P, Double> pair = getNearestData(position); if (pair != null) { return pair.getKey(); } return null; } MapElementGridSet(int nRows, int nColumns, double boundsX, double boundsY, double boundsWidth, double boundsHeight);  MapElementGridSet(int nRows, int nColumns, Rectangle2d bounds); @Override boolean add(P point); @Override @Pure final P getNearest(Point2D<?, ?> position); @Override @Pure final P getNearest(double x, double y); @Override @Pure final Pair<P, Double> getNearestData(double x, double y); @Override @Pure Pair<P, Double> getNearestData(Point2D<?, ?> position);  }
@Test public void testGetNearest() { getLogger().info("Preparing the benchmark..."); assertNotNull(this.bounds); MapElementGridSet<MapPolyline> test = new MapElementGridSet<>(100, 100, this.bounds); test.addAll(this.reference); assertEquals(this.reference.size(), test.size()); getLogger().info("Run test..."); Random rnd = new Random(); MapPolyline nearestData; double minDistance, distance, x, y; Point2d p; final ArrayList<MapPolyline> nearest = new ArrayList<>(); boolean found = false; int tries = 5; do { nearest.clear(); x = rnd.nextDouble()*this.bounds.getWidth()+this.bounds.getMinX(); y = rnd.nextDouble()*this.bounds.getHeight()+this.bounds.getMinY(); p = new Point2d(x,y); minDistance = Double.MAX_VALUE; for (MapPolyline line : this.reference) { distance = line.getDistance(p); if (distance<minDistance) { minDistance = distance; } } for (MapPolyline line : this.reference) { distance = line.getDistance(p); if (isEpsilonEquals(distance, minDistance)) { nearest.add(line); } } nearestData = test.getNearest(x, y); assertNotNull(nearestData); found = false; for (MapPolyline poly : nearest) { if (poly == nearestData) { found = true; break; } } --tries; } while (!found && tries > 0); assertTrue(found, "Polyline not found"); }