diff --git a/src/main/java/org/opentripplanner/model/plan/SortOrder.java b/src/main/java/org/opentripplanner/model/plan/SortOrder.java index 342f963ba00..543460ac72d 100644 --- a/src/main/java/org/opentripplanner/model/plan/SortOrder.java +++ b/src/main/java/org/opentripplanner/model/plan/SortOrder.java @@ -42,4 +42,16 @@ public enum SortOrder { public boolean isSortedByAscendingArrivalTime() { return this == STREET_AND_ARRIVAL_TIME; } + + /** + * The itineraries are sorted with by departure time with the latest departure time first. When + * paging we need to know which end of the list of itineraries we should crop. This method is used + * to decide that together with the current page type (next/previous). + *
+ * This returns {@code false} for the default depart-after search, and {@code true} for an
+ * arrive-by search.
+ */
+ public boolean isSortedByDescendingDepartureTime() {
+ return this == STREET_AND_DEPARTURE_TIME;
+ }
}
diff --git a/src/main/java/org/opentripplanner/routing/algorithm/filterchain/ItineraryListFilterChainBuilder.java b/src/main/java/org/opentripplanner/routing/algorithm/filterchain/ItineraryListFilterChainBuilder.java
index 764ad69b59e..6d3d17d8299 100644
--- a/src/main/java/org/opentripplanner/routing/algorithm/filterchain/ItineraryListFilterChainBuilder.java
+++ b/src/main/java/org/opentripplanner/routing/algorithm/filterchain/ItineraryListFilterChainBuilder.java
@@ -473,7 +473,10 @@ public ItineraryListFilterChain build() {
}
if (earliestDepartureTime != null && filterDirectFlexByEarliestDeparture) {
- addRemoveFilter(filters, new FlexSearchWindowFilter(earliestDepartureTime));
+ addRemoveFilter(
+ filters,
+ new FlexSearchWindowFilter(earliestDepartureTime, searchWindow, sortOrder)
+ );
}
// Remove itineraries present in the page retrieved before this page/search.
diff --git a/src/main/java/org/opentripplanner/routing/algorithm/filterchain/filters/system/FlexSearchWindowFilter.java b/src/main/java/org/opentripplanner/routing/algorithm/filterchain/filters/system/FlexSearchWindowFilter.java
index 47ca0e730e7..030bdfa7b78 100644
--- a/src/main/java/org/opentripplanner/routing/algorithm/filterchain/filters/system/FlexSearchWindowFilter.java
+++ b/src/main/java/org/opentripplanner/routing/algorithm/filterchain/filters/system/FlexSearchWindowFilter.java
@@ -1,8 +1,10 @@
package org.opentripplanner.routing.algorithm.filterchain.filters.system;
+import java.time.Duration;
import java.time.Instant;
import java.util.function.Predicate;
import org.opentripplanner.model.plan.Itinerary;
+import org.opentripplanner.model.plan.SortOrder;
import org.opentripplanner.routing.algorithm.filterchain.framework.spi.RemoveItineraryFlagger;
/**
@@ -18,9 +20,17 @@ public class FlexSearchWindowFilter implements RemoveItineraryFlagger {
public static final String TAG = "outside-flex-window";
private final Instant earliestDepartureTime;
-
- public FlexSearchWindowFilter(Instant earliestDepartureTime) {
+ private final Instant latestArrivalTime;
+ private final SortOrder sortOrder;
+
+ public FlexSearchWindowFilter(
+ Instant earliestDepartureTime,
+ Duration searchWindow,
+ SortOrder sortOrder
+ ) {
this.earliestDepartureTime = earliestDepartureTime;
+ this.latestArrivalTime = earliestDepartureTime.plus(searchWindow);
+ this.sortOrder = sortOrder;
}
@Override
@@ -31,9 +41,12 @@ public String name() {
@Override
public Predicate