Skip to content

Commit

Permalink
move SearchDirection to a separate class and rename to SearchTime
Browse files Browse the repository at this point in the history
  • Loading branch information
miklcct committed Nov 5, 2024
1 parent c780d5b commit 0482fb7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.alternativelegs.AlternativeLegs;
import org.opentripplanner.routing.alternativelegs.AlternativeLegsFilter;
import org.opentripplanner.routing.alternativelegs.SearchTime;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.organization.Agency;
import org.opentripplanner.transit.model.timetable.Trip;
Expand Down Expand Up @@ -277,16 +278,16 @@ private Leg getSource(DataFetchingEnvironment environment) {

@Override
public DataFetcher<Iterable<Leg>> previousLegs() {
return alternativeLegs(AlternativeLegs.SearchDirection.PREVIOUS);
return alternativeLegs(SearchTime.BEFORE);
}

@Override
public DataFetcher<Iterable<Leg>> nextLegs() {
return alternativeLegs(AlternativeLegs.SearchDirection.NEXT);
return alternativeLegs(SearchTime.AFTER);
}

private DataFetcher<Iterable<Leg>> alternativeLegs(
AlternativeLegs.SearchDirection searchDirection
SearchTime searchDirection
) {
return environment -> {
if (environment.getSource() instanceof ScheduledTransitLeg originalLeg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.model.plan.legreference.LegReferenceSerializer;
import org.opentripplanner.routing.alternativelegs.AlternativeLegs;
import org.opentripplanner.routing.alternativelegs.AlternativeLegs.SearchDirection;
import org.opentripplanner.routing.alternativelegs.SearchTime;

public class LegType {

Expand Down Expand Up @@ -486,7 +486,7 @@ public static GraphQLObjectType create(
leg,
env.getArgument("previous"),
GqlUtil.getTransitService(env),
SearchDirection.PREVIOUS,
SearchTime.BEFORE,
env.getArgument("filter")
);
})
Expand Down Expand Up @@ -526,7 +526,7 @@ public static GraphQLObjectType create(
leg,
env.getArgument("next"),
GqlUtil.getTransitService(env),
SearchDirection.NEXT,
SearchTime.AFTER,
env.getArgument("filter")
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@
*/
public class AlternativeLegs {

public enum SearchDirection {
NEXT,
PREVIOUS,
}

public static final int ZERO_COST = 0;

public static List<ScheduledTransitLeg> getAlternativeLegs(
Leg leg,
Integer numberLegs,
TransitService transitService,
SearchDirection searchDirection,
SearchTime searchDirection,
AlternativeLegsFilter filter
) {
return getAlternativeLegs(
Expand Down Expand Up @@ -86,7 +81,7 @@ public static List<ScheduledTransitLeg> getAlternativeLegs(
Leg leg,
Integer numberLegs,
TransitService transitService,
SearchDirection searchDirection,
SearchTime searchDirection,
AlternativeLegsFilter filter,
boolean exactOriginStop,
boolean exactDestinationStop
Expand All @@ -109,7 +104,7 @@ public static List<ScheduledTransitLeg> getAlternativeLegs(
ScheduledTransitLeg::getStartTime
);

if (searchDirection == SearchDirection.PREVIOUS) {
if (searchDirection == SearchTime.BEFORE) {
legComparator = legComparator.reversed();
}

Expand Down Expand Up @@ -140,7 +135,7 @@ private static Stream<ScheduledTransitLeg> generateLegs(
TripPatternBetweenStops tripPatternBetweenStops,
ZonedDateTime departureTime,
LocalDate originalDate,
SearchDirection searchDirection
SearchTime searchDirection
) {
TripPattern pattern = tripPatternBetweenStops.tripPattern;
int boardingPosition = tripPatternBetweenStops.positions.boardingPosition;
Expand All @@ -153,7 +148,7 @@ private static Stream<ScheduledTransitLeg> generateLegs(
tts.getServiceDayMidnight() + tts.getRealtimeDeparture()
);

if (searchDirection == SearchDirection.PREVIOUS) {
if (searchDirection == SearchTime.BEFORE) {
comparator = comparator.reversed();
}

Expand Down Expand Up @@ -183,7 +178,7 @@ private static Stream<ScheduledTransitLeg> generateLegs(
continue;
}

boolean departureTimeInRange = searchDirection == SearchDirection.PREVIOUS
boolean departureTimeInRange = searchDirection == SearchTime.BEFORE
? tripTimes.getDepartureTime(boardingPosition) <= secondsSinceMidnight
: tripTimes.getDepartureTime(boardingPosition) >= secondsSinceMidnight;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opentripplanner.routing.alternativelegs;

public enum SearchTime {
AFTER,
BEFORE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opentripplanner.model.plan.legreference.ScheduledTransitLegReference;
import org.opentripplanner.routing.alternativelegs.AlternativeLegs;
import org.opentripplanner.routing.alternativelegs.AlternativeLegsFilter;
import org.opentripplanner.routing.alternativelegs.SearchTime;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.service.DefaultTransitService;

Expand Down Expand Up @@ -51,7 +52,7 @@ void testPreviousLegs() {
originalLeg,
3,
transitService,
AlternativeLegs.SearchDirection.PREVIOUS,
SearchTime.BEFORE,
AlternativeLegsFilter.NO_FILTER
);

Expand Down Expand Up @@ -85,7 +86,7 @@ void testNextLegs() {
originalLeg,
3,
transitService,
AlternativeLegs.SearchDirection.NEXT,
SearchTime.AFTER,
AlternativeLegsFilter.NO_FILTER
);

Expand Down Expand Up @@ -119,7 +120,7 @@ void testCircularRoutes() {
originalLeg,
2,
transitService,
AlternativeLegs.SearchDirection.NEXT,
SearchTime.AFTER,
AlternativeLegsFilter.NO_FILTER
);

Expand Down Expand Up @@ -147,7 +148,7 @@ void testComplexCircularRoutes() {
originalLeg,
2,
transitService,
AlternativeLegs.SearchDirection.NEXT,
SearchTime.AFTER,
AlternativeLegsFilter.NO_FILTER
);
var legs = toString(alternativeLegs);
Expand Down

0 comments on commit 0482fb7

Please sign in to comment.