TrimAreaMeasurementsFn implements Function<ILink, Optional<ILink>> { @Override public Optional<ILink> apply(ILink link) { if (AreaMeasurement.IS_AREA_MEASUREMENT_PREDICATE.test(link)) { Polygon polygon = toPolygon.apply(link); Area area = new Area(polygon); area.intersect(acceptableBounds); Polygon trimmedPolygon = areaToPolygon(area); if (trimmedPolygon.npoints >= 3) { AreaMeasurement trimmedAreaMeasurement = polygonToAreaMeasurement(trimmedPolygon); return Optional.of(trimmedAreaMeasurement.toLink()); } else { return Optional.empty(); } } else { return Optional.of(link); } } TrimAreaMeasurementsFn(java.util.List<Point2D<Integer>> bounds); @Override Optional<ILink> apply(ILink link);  }
@Test public void testParialIntersection() { List<Point2D<Integer>> bounds = new ArrayList<>(); bounds.add(new Point2D<>(0, 0)); bounds.add(new Point2D<>(15, 0)); bounds.add(new Point2D<>(10, 10)); bounds.add(new Point2D<>(0, 10)); AreaMeasurement am = new AreaMeasurement(bounds); ILink link = am.toLink(); Optional<ILink> result = fn.apply(link); ILink link0 = result.get(); AreaMeasurement am0 = AreaMeasurement.fromLink(link0); List<Point2D<Integer>> bounds0 = am0.getCoordinates(); assertTrue(bounds0.size() == myBounds.size()); bounds0.stream() .forEach(p -> assertTrue(myBounds.contains(p))); }
@Test public void testNoIntersection() { List<Point2D<Integer>> bounds = new ArrayList<>(); bounds.add(new Point2D<>(11, 0)); bounds.add(new Point2D<>(15, 0)); bounds.add(new Point2D<>(11, 10)); AreaMeasurement am = new AreaMeasurement(bounds); ILink link = am.toLink(); Optional<ILink> result = fn.apply(link); assertTrue(!result.isPresent()); }
@Test public void testCompleteIntersection() { List<Point2D<Integer>> bounds = new ArrayList<>(); bounds.add(new Point2D<>(1, 1)); bounds.add(new Point2D<>(9, 1)); bounds.add(new Point2D<>(9, 9)); bounds.add(new Point2D<>(1, 9)); AreaMeasurement am = new AreaMeasurement(bounds); ILink link = am.toLink(); Optional<ILink> result = fn.apply(link); assertTrue(result.isPresent()); ILink link0 = result.get(); AreaMeasurement am0 = AreaMeasurement.fromLink(link0); List<Point2D<Integer>> bounds0 = am0.getCoordinates(); assertTrue(bounds0.size() == bounds.size()); bounds0.stream() .forEach(p -> assertTrue(bounds.contains(p))); }