-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5366 from ibi-group/remove-duplicate-nearby-stops
De-duplicate stops returned by `stopsByRadius`
- Loading branch information
Showing
8 changed files
with
134 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/org/opentripplanner/framework/lang/PredicateUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.opentripplanner.framework.lang; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Utility for building predicates to be used for filtering streams. | ||
*/ | ||
public class PredicateUtils { | ||
|
||
/** | ||
* Build a predicate that uses the {@code keyExtractor} to remove any key that has already | ||
* been seen by this (stateful) predicate. | ||
* <p> | ||
* This is useful for removing duplicates from a stream where the key to be compared is not the | ||
* entity itself but a field of it. | ||
* <p> | ||
* Note: Duplicate check is based on equality not identity. | ||
*/ | ||
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { | ||
Map<Object, Boolean> seen = new ConcurrentHashMap<>(); | ||
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/org/opentripplanner/framework/lang/PredicateUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.opentripplanner.framework.lang; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PredicateUtilsTest { | ||
|
||
private static String makeHello() { | ||
return new String("HELLO"); | ||
} | ||
|
||
@Test | ||
void distinctByKey() { | ||
var first = new Wrapper(10, makeHello()); | ||
var last = new Wrapper(20, "HI"); | ||
var stream = Stream.of(first, new Wrapper(20, makeHello()), last); | ||
|
||
var deduplicated = stream.filter(PredicateUtils.distinctByKey(w -> w.string)).toList(); | ||
|
||
assertEquals(List.of(first, last), deduplicated); | ||
} | ||
|
||
private record Wrapper(int i, String string) {} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/org/opentripplanner/routing/graphfinder/StopFinderTraverseVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.opentripplanner.routing.graphfinder; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner.street.model.vertex.TransitStopVertex; | ||
import org.opentripplanner.street.search.state.TestStateBuilder; | ||
import org.opentripplanner.transit.model._data.TransitModelForTest; | ||
import org.opentripplanner.transit.model.site.RegularStop; | ||
|
||
class StopFinderTraverseVisitorTest { | ||
|
||
static final RegularStop STOP = TransitModelForTest.stopForTest("a-stop", 1, 1); | ||
|
||
@Test | ||
void deduplicateStops() { | ||
var visitor = new StopFinderTraverseVisitor(1000); | ||
|
||
assertEquals(List.of(), visitor.stopsFound()); | ||
var state1 = TestStateBuilder.ofWalking().streetEdge().stop(STOP).build(); | ||
|
||
visitor.visitVertex(state1); | ||
|
||
var transitStopVertex = (TransitStopVertex) state1.getVertex(); | ||
final NearbyStop nearbyStop = NearbyStop.nearbyStopForState( | ||
state1, | ||
transitStopVertex.getStop() | ||
); | ||
|
||
assertEquals(List.of(nearbyStop), visitor.stopsFound()); | ||
|
||
visitor.visitVertex(state1); | ||
|
||
assertEquals(List.of(nearbyStop), visitor.stopsFound()); | ||
|
||
// taking a different path to the same stop | ||
var state2 = TestStateBuilder.ofWalking().streetEdge().streetEdge().stop(STOP).build(); | ||
|
||
visitor.visitVertex(state2); | ||
|
||
assertEquals(List.of(nearbyStop), visitor.stopsFound()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters