From 6df7daccdbba99961ffc55d424334bf39939b4e9 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Fri, 8 Nov 2024 13:22:27 +0000 Subject: [PATCH] refactor arrival and departure stop time --- .../apis/gtfs/datafetchers/TripImpl.java | 93 +++++++++---------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java b/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java index 956b35309e2..2266da23f63 100644 --- a/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java +++ b/application/src/main/java/org/opentripplanner/apis/gtfs/datafetchers/TripImpl.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import javax.annotation.Nullable; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.LineString; import org.opentripplanner.apis.gtfs.GraphQLRequestContext; @@ -36,7 +37,6 @@ import org.opentripplanner.transit.model.site.StopLocation; import org.opentripplanner.transit.model.timetable.Direction; import org.opentripplanner.transit.model.timetable.Trip; -import org.opentripplanner.transit.model.timetable.TripTimes; import org.opentripplanner.transit.service.TransitService; import org.opentripplanner.utils.time.ServiceDateUtils; @@ -132,38 +132,23 @@ public DataFetcher> alerts() { @Override public DataFetcher arrivalStoptime() { return environment -> { - try { - TransitService transitService = getTransitService(environment); - TripPattern tripPattern = getTripPattern(environment); - if (tripPattern == null) { - return null; - } - Timetable timetable = tripPattern.getScheduledTimetable(); - - TripTimes triptimes = timetable.getTripTimes(getSource(environment)); - LocalDate serviceDate = null; - Instant midnight = null; - - var args = new GraphQLTypes.GraphQLTripArrivalStoptimeArgs(environment.getArguments()); - if (args.getGraphQLServiceDate() != null) { + LocalDate serviceDate = null; + var args = new GraphQLTypes.GraphQLTripArrivalStoptimeArgs(environment.getArguments()); + if (args.getGraphQLServiceDate() != null) { + try { serviceDate = ServiceDateUtils.parseString(args.getGraphQLServiceDate()); - midnight = - ServiceDateUtils - .asStartOfService(serviceDate, transitService.getTimeZone()) - .toInstant(); + } catch (ParseException e) { + //Invalid date format + return null; } + } - return new TripTimeOnDate( - triptimes, - triptimes.getNumStops() - 1, - tripPattern, - serviceDate, - midnight - ); - } catch (ParseException e) { - //Invalid date format + TripPattern tripPattern = getTripPattern(environment); + if (tripPattern == null) { return null; } + + return getStoptimeAtIndex(environment, serviceDate, tripPattern.numberOfStops() - 1); }; } @@ -180,32 +165,23 @@ public DataFetcher blockId() { @Override public DataFetcher departureStoptime() { return environment -> { - try { - TransitService transitService = getTransitService(environment); - TripPattern tripPattern = getTripPattern(environment); - if (tripPattern == null) { - return null; - } - Timetable timetable = tripPattern.getScheduledTimetable(); - - TripTimes triptimes = timetable.getTripTimes(getSource(environment)); - LocalDate serviceDate = null; - Instant midnight = null; - - var args = new GraphQLTypes.GraphQLTripDepartureStoptimeArgs(environment.getArguments()); - if (args.getGraphQLServiceDate() != null) { + LocalDate serviceDate = null; + var args = new GraphQLTypes.GraphQLTripDepartureStoptimeArgs(environment.getArguments()); + if (args.getGraphQLServiceDate() != null) { + try { serviceDate = ServiceDateUtils.parseString(args.getGraphQLServiceDate()); - midnight = - ServiceDateUtils - .asStartOfService(serviceDate, transitService.getTimeZone()) - .toInstant(); + } catch (ParseException e) { + //Invalid date format + return null; } + } - return new TripTimeOnDate(triptimes, 0, tripPattern, serviceDate, midnight); - } catch (ParseException e) { - //Invalid date format + TripPattern tripPattern = getTripPattern(environment); + if (tripPattern == null) { return null; } + + return getStoptimeAtIndex(environment, serviceDate, 0); }; } @@ -408,6 +384,25 @@ private RealtimeVehicleService getRealtimeVehiclesService(DataFetchingEnvironmen return environment.getContext().realTimeVehicleService(); } + private TripTimeOnDate getStoptimeAtIndex( + DataFetchingEnvironment environment, + @Nullable LocalDate serviceDate, + int stopIndex + ) { + var tripPattern = getTripPattern(environment); + return new TripTimeOnDate( + tripPattern.getScheduledTimetable().getTripTimes(getSource(environment)), + stopIndex, + tripPattern, + serviceDate, + serviceDate == null + ? null + : ServiceDateUtils + .asStartOfService(serviceDate, getTransitService(environment).getTimeZone()) + .toInstant() + ); + } + private Trip getSource(DataFetchingEnvironment environment) { return environment.getSource(); }