/** * Returns a list of objects from {@link RoadModel} <code>rm</code> ordered by * its distance to position <code>pos</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static List<RoadUser> findClosestObjects(Point pos, RoadModel rm) { return RoadModels.findClosestObjects(pos, rm, RoadUser.class, Integer.MAX_VALUE); }
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * {@link RoadModel} <code>rm</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @param n The maximum number of objects to return where n must be ≥ 0. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static List<RoadUser> findClosestObjects(Point pos, RoadModel rm, int n) { return RoadModels.findClosestObjects(pos, rm, RoadUser.class, n); }
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * {@link RoadModel} <code>rm</code>. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @param type The type of objects which are included in the search. * @param n The maximum number of objects to return where n must be ≥ 0. * @param <T> The type of the objects in the returned collection. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static <T extends RoadUser> List<T> findClosestObjects(Point pos, RoadModel rm, Class<T> type, int n) { return RoadModels.findClosestObjects(pos, rm, rm.getObjectsOfType(type), n); }
/** * Searches the closest <code>n</code> objects to position <code>pos</code> in * {@link RoadModel} <code>rm</code>. Only the objects that satisfy * <code>predicate</code> are included in the search. * @param pos The {@link Point} which is used as a reference point. * @param rm The {@link RoadModel} instance in which the closest objects are * searched. * @param predicate Only objects that satisfy this predicate will be returned. * @param n The maximum number of objects to return where n must be ≥ 0. * @return A list of objects that are closest to <code>pos</code>. The list is * ordered such that the closest object appears first. An empty list * is returned when <code>objects</code> is empty. */ public static List<RoadUser> findClosestObjects(Point pos, RoadModel rm, Predicate<RoadUser> predicate, int n) { final Collection<RoadUser> filtered = Collections2.filter(rm.getObjects(), predicate); return RoadModels.findClosestObjects(pos, rm, filtered, n); }
/** * Test non positive n value. */ @Test(expected = IllegalArgumentException.class) public void findClosestObjectsTestFail() { RoadModels.findClosestObjects(new Point(5, 5), rm, 0); }
@Test public void findClosestObjectsTest() { assertEquals(Arrays.asList(o1, o2, o3, o6, o5, o4), RoadModels.findClosestObjects(new Point(5, 5), rm)); assertEquals(Arrays.asList(o1, o2, o3), RoadModels.findClosestObjects(new Point(5, 5), rm, 3)); assertEquals(Arrays.asList(o6, o5, o4), RoadModels.findClosestObjects( new Point(5, 5), rm, LongRoadUser.class, 300)); assertEquals(Arrays.asList(), RoadModels.findClosestObjects( new Point(5, 5), rm, EmptyRoadUser.class, 1)); assertEquals(Arrays.asList(o3, o6, o4, o5), RoadModels.findClosestObjects( new Point(8, 8), rm, new Predicate<RoadUser>() { @Override public boolean apply(RoadUser input) { return input instanceof LongRoadUser || rm.getPosition(input).equals(new Point(15, 15)); } }, 99)); }