Skip to content

Commit

Permalink
use an enum instead of boolean to switch next / previous alternative …
Browse files Browse the repository at this point in the history
…legs
  • Loading branch information
miklcct committed Oct 29, 2024
1 parent ccd001d commit c715329
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ private Leg getSource(DataFetchingEnvironment environment) {

@Override
public DataFetcher<Iterable<Leg>> previousLegs() {
return nextOrPreviousLegs(true);
return nextOrPreviousLegs(AlternativeLegs.SearchMode.PREVIOUS);
}

@Override
public DataFetcher<Iterable<Leg>> nextLegs() {
return nextOrPreviousLegs(false);
return nextOrPreviousLegs(AlternativeLegs.SearchMode.NEXT);
}

private DataFetcher<Iterable<Leg>> nextOrPreviousLegs(boolean includeDepartBefore) {
private DataFetcher<Iterable<Leg>> nextOrPreviousLegs(AlternativeLegs.SearchMode searchMode) {
return environment -> {
if (environment.getSource() instanceof ScheduledTransitLeg originalLeg) {
var args = new GraphQLTypes.GraphQLLegNextLegsArgs(environment.getArguments());
Expand Down Expand Up @@ -320,7 +320,7 @@ private DataFetcher<Iterable<Leg>> nextOrPreviousLegs(boolean includeDepartBefor
environment.getSource(),
numberOfLegs,
environment.<GraphQLRequestContext>getContext().transitService(),
includeDepartBefore,
searchMode,
AlternativeLegsFilter.NO_FILTER,
limitToExactOriginStop,
limitToExactDestinationStop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
package org.opentripplanner.apis.gtfs.generated;

import graphql.relay.Connection;
import graphql.relay.Connection;
import graphql.relay.DefaultEdge;
import graphql.relay.Edge;
import graphql.relay.Edge;
import graphql.schema.DataFetcher;
import graphql.schema.TypeResolver;
import java.util.Currency;
Expand All @@ -25,8 +27,12 @@
import org.opentripplanner.apis.gtfs.model.FeedPublisher;
import org.opentripplanner.apis.gtfs.model.PlanPageInfo;
import org.opentripplanner.apis.gtfs.model.RideHailingProvider;
import org.opentripplanner.apis.gtfs.model.RouteTypeModel;
import org.opentripplanner.apis.gtfs.model.StopOnRouteModel;
import org.opentripplanner.apis.gtfs.model.StopOnTripModel;
import org.opentripplanner.apis.gtfs.model.StopPosition;
import org.opentripplanner.apis.gtfs.model.TripOccupancy;
import org.opentripplanner.apis.gtfs.model.UnknownModel;
import org.opentripplanner.ext.fares.model.FareRuleSet;
import org.opentripplanner.ext.ridehailing.model.RideEstimate;
import org.opentripplanner.model.StopTimesInPattern;
Expand All @@ -49,6 +55,8 @@
import org.opentripplanner.routing.graphfinder.PatternAtStop;
import org.opentripplanner.routing.graphfinder.PlaceAtDistance;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParking;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingSpaces;
import org.opentripplanner.routing.vehicle_parking.VehicleParkingState;
import org.opentripplanner.service.realtimevehicles.model.RealtimeVehicle;
Expand All @@ -59,6 +67,7 @@
import org.opentripplanner.service.vehiclerental.model.VehicleRentalPlace;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStation;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStationUris;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalStationUris;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalSystem;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalVehicle;
import org.opentripplanner.transit.model.basic.Money;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
package org.opentripplanner.apis.gtfs.generated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +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.SearchMode;

public class LegType {

Expand Down Expand Up @@ -485,7 +486,7 @@ public static GraphQLObjectType create(
leg,
env.getArgument("previous"),
GqlUtil.getTransitService(env),
true,
SearchMode.PREVIOUS,
env.getArgument("filter")
);
})
Expand Down Expand Up @@ -525,7 +526,7 @@ public static GraphQLObjectType create(
leg,
env.getArgument("next"),
GqlUtil.getTransitService(env),
false,
SearchMode.NEXT,
env.getArgument("filter")
);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,21 @@
*/
public class AlternativeLegs {

public enum SearchMode {
NEXT,
PREVIOUS,
}

public static final int ZERO_COST = 0;

public static List<ScheduledTransitLeg> getAlternativeLegs(
Leg leg,
Integer numberLegs,
TransitService transitService,
boolean searchBackward,
SearchMode searchMode,
AlternativeLegsFilter filter
) {
return getAlternativeLegs(
leg,
numberLegs,
transitService,
searchBackward,
filter,
false,
false
);
return getAlternativeLegs(leg, numberLegs, transitService, searchMode, filter, false, false);
}

/**
Expand All @@ -66,9 +63,8 @@ public static List<ScheduledTransitLeg> getAlternativeLegs(
* @param numberLegs The number of alternative legs requested. If fewer legs are found,
* only the found legs are returned.
* @param transitService The transit service used for the search
* @param includeDepartBefore Boolean indicating whether the alternative legs should depart
* earlier or later than the original leg True if earlier, false if
* later.
* @param searchMode Indicating whether the alternative legs should depart
* earlier (PREVIOUS) or later (NEXT) than the original.
* @param filter AlternativeLegsFilter indicating which properties of the original
* leg should not change in the alternative legs
* @param exactOriginStop Boolean indicating whether the exact departure stop of the original
Expand All @@ -82,7 +78,7 @@ public static List<ScheduledTransitLeg> getAlternativeLegs(
Leg leg,
Integer numberLegs,
TransitService transitService,
boolean includeDepartBefore,
SearchMode searchMode,
AlternativeLegsFilter filter,
boolean exactOriginStop,
boolean exactDestinationStop
Expand All @@ -105,7 +101,7 @@ public static List<ScheduledTransitLeg> getAlternativeLegs(
ScheduledTransitLeg::getStartTime
);

if (includeDepartBefore) {
if (searchMode == SearchMode.PREVIOUS) {
legComparator = legComparator.reversed();
}

Expand All @@ -119,13 +115,7 @@ public static List<ScheduledTransitLeg> getAlternativeLegs(
.distinct()
.flatMap(tripPattern -> withBoardingAlightingPositions(origins, destinations, tripPattern))
.flatMap(t ->
generateLegs(
transitService,
t,
leg.getStartTime(),
leg.getServiceDate(),
includeDepartBefore
)
generateLegs(transitService, t, leg.getStartTime(), leg.getServiceDate(), searchMode)
)
.filter(Predicate.not(leg::isPartiallySameTransitLeg))
.sorted(legComparator)
Expand All @@ -142,7 +132,7 @@ private static Stream<ScheduledTransitLeg> generateLegs(
TripPatternBetweenStops tripPatternBetweenStops,
ZonedDateTime departureTime,
LocalDate originalDate,
boolean includeDepartBefore
SearchMode searchMode
) {
TripPattern pattern = tripPatternBetweenStops.tripPattern;
int boardingPosition = tripPatternBetweenStops.positions.boardingPosition;
Expand All @@ -155,7 +145,7 @@ private static Stream<ScheduledTransitLeg> generateLegs(
tts.getServiceDayMidnight() + tts.getRealtimeDeparture()
);

if (includeDepartBefore) {
if (searchMode == SearchMode.PREVIOUS) {
comparator = comparator.reversed();
}

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

boolean departureTimeInRange = includeDepartBefore
boolean departureTimeInRange = searchMode == SearchMode.PREVIOUS
? tripTimes.getDepartureTime(boardingPosition) <= secondsSinceMidnight
: tripTimes.getDepartureTime(boardingPosition) >= secondsSinceMidnight;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void testPreviousLegs() {
originalLeg,
3,
transitService,
true,
AlternativeLegs.SearchMode.PREVIOUS,
AlternativeLegsFilter.NO_FILTER
);

Expand Down Expand Up @@ -85,7 +85,7 @@ void testNextLegs() {
originalLeg,
3,
transitService,
false,
AlternativeLegs.SearchMode.NEXT,
AlternativeLegsFilter.NO_FILTER
);

Expand Down Expand Up @@ -119,7 +119,7 @@ void testCircularRoutes() {
originalLeg,
2,
transitService,
false,
AlternativeLegs.SearchMode.NEXT,
AlternativeLegsFilter.NO_FILTER
);

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

0 comments on commit c715329

Please sign in to comment.