diff --git a/application/src/main/java/org/opentripplanner/updater/trip/siri/AddedTripBuilder.java b/application/src/main/java/org/opentripplanner/updater/trip/siri/AddedTripBuilder.java index 7255c7adff2..297a4b7ce28 100644 --- a/application/src/main/java/org/opentripplanner/updater/trip/siri/AddedTripBuilder.java +++ b/application/src/main/java/org/opentripplanner/updater/trip/siri/AddedTripBuilder.java @@ -163,7 +163,7 @@ class AddedTripBuilder { this.dataSource = dataSource; } - Result build() { + Result build() { if (calls.size() < 2) { return UpdateError.result(tripId, TOO_FEW_STOPS, dataSource); } @@ -228,7 +228,7 @@ Result build() { tripTimes.validateNonIncreasingTimes(); tripTimes.setServiceCode(transitService.getServiceCode(trip.getServiceId())); - TripPattern pattern = TripPattern + TripPattern addedTripPattern = TripPattern .of(getTripPatternId.apply(trip)) .withRoute(trip.getRoute()) .withMode(trip.getMode()) @@ -273,12 +273,12 @@ Result build() { .build(); return Result.success( - new TripUpdate( + new SiriTripUpdate.SiriAddTrip( stopPattern, updatedTripTimes, serviceDate, tripOnServiceDate, - pattern, + addedTripPattern, isAddedRoute, dataSource ) diff --git a/application/src/main/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilder.java b/application/src/main/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilder.java index 85278ea22f7..173af4706a3 100644 --- a/application/src/main/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilder.java +++ b/application/src/main/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilder.java @@ -99,7 +99,7 @@ public ModifiedTripBuilder( * Create a new StopPattern and TripTimes for the trip based on the calls, and other fields read * in form the SIRI-ET update. */ - public Result build() { + public Result build() { RealTimeTripTimes newTimes = existingTripTimes.copyScheduledTimes(); var stopPattern = createStopPattern(pattern, calls, entityResolver); @@ -108,7 +108,12 @@ public Result build() { LOG.debug("Trip is cancelled"); newTimes.cancelTrip(); return Result.success( - new TripUpdate(pattern.getStopPattern(), newTimes, serviceDate, dataSource) + new SiriTripUpdate.SiriModifyTrip( + pattern.getStopPattern(), + newTimes, + serviceDate, + dataSource + ) ); } @@ -147,7 +152,9 @@ public Result build() { } LOG.debug("A valid TripUpdate object was applied using the Timetable class update method."); - return Result.success(new TripUpdate(stopPattern, newTimes, serviceDate, dataSource)); + return Result.success( + new SiriTripUpdate.SiriModifyTrip(stopPattern, newTimes, serviceDate, dataSource) + ); } /** diff --git a/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriRealTimeTripUpdateAdapter.java b/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriRealTimeTripUpdateAdapter.java index 8ee723567c0..8369140d3ad 100644 --- a/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriRealTimeTripUpdateAdapter.java +++ b/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriRealTimeTripUpdateAdapter.java @@ -130,7 +130,7 @@ private Result apply( boolean shouldAddNewTrip = false; try { shouldAddNewTrip = shouldAddNewTrip(journey, entityResolver); - Result result; + Result result; if (shouldAddNewTrip) { result = new AddedTripBuilder( @@ -188,7 +188,7 @@ private Timetable getCurrentTimetable(TripPattern tripPattern, LocalDate service return snapshotManager.getTimetableSnapshotBuffer().resolve(tripPattern, serviceDate); } - private Result handleModifiedTrip( + private Result handleModifiedTrip( @Nullable SiriFuzzyTripMatcher fuzzyTripMatcher, EntityResolver entityResolver, EstimatedVehicleJourney estimatedVehicleJourney @@ -274,31 +274,46 @@ private Result handleModifiedTrip( /** * Add a (new) trip to the timetableRepository and the buffer */ - private Result addTripToGraphAndBuffer(TripUpdate tripUpdate) { + private Result addTripToGraphAndBuffer(SiriTripUpdate tripUpdate) { Trip trip = tripUpdate.tripTimes().getTrip(); - LocalDate serviceDate = tripUpdate.serviceDate(); - final TripPattern pattern; - if (tripUpdate.tripPatternCreation()) { - pattern = tripUpdate.addedTripPattern(); - } else { - // Get cached trip pattern or create one if it doesn't exist yet - pattern = - tripPatternCache.getOrCreateTripPattern(tripUpdate.stopPattern(), trip, serviceDate); - } + final RealTimeTripUpdate realTimeTripUpdate; + switch (tripUpdate) { + case SiriTripUpdate.SiriAddTrip addTrip -> { + // Add new trip times to buffer, making protective copies as needed. Bubble success/error up. + realTimeTripUpdate = + new RealTimeTripUpdate( + addTrip.addedTripPattern(), + addTrip.tripTimes(), + addTrip.serviceDate(), + addTrip.addedTripOnServiceDate(), + true, + addTrip.routeCreation(), + addTrip.dataSource() + ); + } + case SiriTripUpdate.SiriModifyTrip modifyTrip -> { + // Get cached trip pattern or create one if it doesn't exist yet + var tripPattern = tripPatternCache.getOrCreateTripPattern( + modifyTrip.stopPattern(), + trip, + modifyTrip.serviceDate() + ); - // Add new trip times to buffer, making protective copies as needed. Bubble success/error up. - RealTimeTripUpdate realTimeTripUpdate = new RealTimeTripUpdate( - pattern, - tripUpdate.tripTimes(), - serviceDate, - tripUpdate.addedTripOnServiceDate(), - tripUpdate.tripCreation(), - tripUpdate.routeCreation(), - tripUpdate.dataSource() - ); + realTimeTripUpdate = + new RealTimeTripUpdate( + tripPattern, + modifyTrip.tripTimes(), + modifyTrip.serviceDate(), + null, + false, + false, + modifyTrip.dataSource() + ); + } + } var result = snapshotManager.updateBuffer(realTimeTripUpdate); - LOG.debug("Applied real-time data for trip {} on {}", trip, serviceDate); + LOG.debug("Applied real-time data for trip {} on {}", trip, tripUpdate.serviceDate()); return result; } diff --git a/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriTripUpdate.java b/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriTripUpdate.java new file mode 100644 index 00000000000..4c73d6a56c0 --- /dev/null +++ b/application/src/main/java/org/opentripplanner/updater/trip/siri/SiriTripUpdate.java @@ -0,0 +1,59 @@ +package org.opentripplanner.updater.trip.siri; + +import java.time.LocalDate; +import javax.annotation.Nullable; +import org.opentripplanner.transit.model.network.StopPattern; +import org.opentripplanner.transit.model.network.TripPattern; +import org.opentripplanner.transit.model.timetable.RealTimeTripTimes; +import org.opentripplanner.transit.model.timetable.TripOnServiceDate; +import org.opentripplanner.transit.model.timetable.TripTimes; + +/** + * This is a DTO used internally in the SIRI code for holding on to realtime trip update information + */ +public sealed interface SiriTripUpdate + permits SiriTripUpdate.SiriAddTrip, SiriTripUpdate.SiriModifyTrip { + TripTimes tripTimes(); + LocalDate serviceDate(); + + /** + * A message containing information for modifying an existing trip. + * + * @param stopPattern The stop pattern for the modified trip. + * @param tripTimes The updated trip times for the modified trip. + * @param serviceDate The service date for which this update applies (updates are valid + * only for one service date) + * @param dataSource The dataSource of the real-time update. + */ + record SiriModifyTrip( + StopPattern stopPattern, + RealTimeTripTimes tripTimes, + LocalDate serviceDate, + @Nullable String dataSource + ) + implements SiriTripUpdate {} + + /** + * A message with information for adding a new trip + * + * @param stopPattern The stop pattern to which belongs the created trip. + * @param tripTimes The trip times for the created trip. + * @param serviceDate The service date for which this update applies (updates are valid + * only for one service date) + * @param addedTripOnServiceDate TripOnServiceDate corresponding to the new trip. + * @param addedTripPattern The new trip pattern for the new trip. + * @param routeCreation true if an added trip cannot be registered under an existing route + * and a new route must be created. + * @param dataSource The dataSource of the real-time update. + */ + record SiriAddTrip( + StopPattern stopPattern, + TripTimes tripTimes, + LocalDate serviceDate, + TripOnServiceDate addedTripOnServiceDate, + TripPattern addedTripPattern, + boolean routeCreation, + @Nullable String dataSource + ) + implements SiriTripUpdate {} +} diff --git a/application/src/main/java/org/opentripplanner/updater/trip/siri/TripUpdate.java b/application/src/main/java/org/opentripplanner/updater/trip/siri/TripUpdate.java deleted file mode 100644 index 7bd5b0bc211..00000000000 --- a/application/src/main/java/org/opentripplanner/updater/trip/siri/TripUpdate.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.opentripplanner.updater.trip.siri; - -import java.time.LocalDate; -import java.util.Objects; -import javax.annotation.Nullable; -import org.opentripplanner.transit.model.network.StopPattern; -import org.opentripplanner.transit.model.network.TripPattern; -import org.opentripplanner.transit.model.timetable.RealTimeTripTimes; -import org.opentripplanner.transit.model.timetable.TripOnServiceDate; -import org.opentripplanner.transit.model.timetable.TripTimes; - -/** - * Represents the SIRI real-time update of a single trip. - * - * @param stopPattern the stop pattern to which belongs the updated trip. - * @param tripTimes the new trip times for the updated trip. - * @param serviceDate the service date for which this update applies (updates are valid - * only for one service date) - * @param addedTripOnServiceDate optionally if this trip update adds a new trip, the - * TripOnServiceDate corresponding to this new trip. - * @param addedTripPattern optionally if this trip update adds a new trip pattern , the new - * trip pattern to this new trip. - * @param routeCreation true if an added trip cannot be registered under an existing route - * and a new route must be created. - * @param dataSource the dataSource of the real-time update. - */ -record TripUpdate( - StopPattern stopPattern, - TripTimes tripTimes, - LocalDate serviceDate, - @Nullable TripOnServiceDate addedTripOnServiceDate, - @Nullable TripPattern addedTripPattern, - boolean routeCreation, - @Nullable String dataSource -) { - public TripUpdate { - Objects.requireNonNull(stopPattern); - Objects.requireNonNull(tripTimes); - Objects.requireNonNull(serviceDate); - } - - /** - * Create a trip update for an existing trip. - */ - public TripUpdate( - StopPattern stopPattern, - RealTimeTripTimes updatedTripTimes, - LocalDate serviceDate, - String dataSource - ) { - this(stopPattern, updatedTripTimes, serviceDate, null, null, false, dataSource); - } - - /** - * Return true if this trip update creates a new trip pattern. - */ - public boolean tripPatternCreation() { - return addedTripPattern != null; - } - - /** - * Return true if this trip update creates a new trip. - */ - public boolean tripCreation() { - return addedTripOnServiceDate != null; - } -} diff --git a/application/src/test/java/org/opentripplanner/updater/trip/siri/AddedTripBuilderTest.java b/application/src/test/java/org/opentripplanner/updater/trip/siri/AddedTripBuilderTest.java index 6b57b306e45..6c6928b98ef 100644 --- a/application/src/test/java/org/opentripplanner/updater/trip/siri/AddedTripBuilderTest.java +++ b/application/src/test/java/org/opentripplanner/updater/trip/siri/AddedTripBuilderTest.java @@ -142,7 +142,7 @@ void testAddedTrip() { assertTrue(addedTrip.isSuccess(), "Trip creation should succeed"); - TripUpdate tripUpdate = addedTrip.successValue(); + var tripUpdate = addedTrip.successValue(); // Assert trip Trip trip = tripUpdate.tripTimes().getTrip(); assertEquals(TRIP_ID, trip.getId(), "Trip should be mapped"); @@ -164,8 +164,6 @@ void testAddedTrip() { assertTrue(tripUpdate.routeCreation(), "The route is marked as created by real time updater"); - assertTrue(tripUpdate.tripCreation(), "The trip is marked as created by real time updater"); - TripPattern pattern = tripUpdate.addedTripPattern(); assertNotNull(pattern); assertEquals(route, pattern.getRoute()); diff --git a/application/src/test/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilderTest.java b/application/src/test/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilderTest.java index bca6b39b9a8..d1a105899b7 100644 --- a/application/src/test/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilderTest.java +++ b/application/src/test/java/org/opentripplanner/updater/trip/siri/ModifiedTripBuilderTest.java @@ -181,7 +181,7 @@ void testUpdateNoCalls() { assertTrue(result.isSuccess(), "Update should succeed"); - TripUpdate tripUpdate = result.successValue(); + var tripUpdate = result.successValue(); assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern()); TripTimes updatedTimes = tripUpdate.tripTimes(); assertEquals(STOP_TIME_A_1.getArrivalTime(), updatedTimes.getArrivalTime(0)); @@ -211,7 +211,7 @@ void testUpdateCancellation() { assertTrue(result.isSuccess(), "Update should succeed"); - TripUpdate tripUpdate = result.successValue(); + var tripUpdate = result.successValue(); assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern()); TripTimes updatedTimes = tripUpdate.tripTimes(); assertEquals(RealTimeState.CANCELED, updatedTimes.getRealTimeState()); @@ -256,7 +256,7 @@ void testUpdateSameStops() { assertTrue(result.isSuccess(), "Update should succeed"); - TripUpdate tripUpdate = result.successValue(); + var tripUpdate = result.successValue(); assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern()); TripTimes updatedTimes = tripUpdate.tripTimes(); assertEquals(secondsInDay(10, 1), updatedTimes.getArrivalTime(0)); @@ -356,7 +356,7 @@ void testUpdateSameStopsDepartEarly() { assertTrue(result.isSuccess(), "Update should succeed"); - TripUpdate tripUpdate = result.successValue(); + var tripUpdate = result.successValue(); assertEquals(PATTERN.getStopPattern(), tripUpdate.stopPattern()); TripTimes updatedTimes = tripUpdate.tripTimes(); assertEquals(secondsInDay(9, 58), updatedTimes.getArrivalTime(0)); @@ -407,7 +407,7 @@ void testUpdateUpdatedStop() { assertTrue(result.isSuccess(), "Update should succeed"); - TripUpdate tripUpdate = result.successValue(); + var tripUpdate = result.successValue(); StopPattern stopPattern = tripUpdate.stopPattern(); assertNotEquals(PATTERN.getStopPattern(), stopPattern); assertEquals(STOP_A_2, stopPattern.getStop(0)); @@ -508,7 +508,7 @@ void testUpdateCascading() { ) .build(); - TripUpdate tripUpdate = secondResult.successValue(); + var tripUpdate = secondResult.successValue(); StopPattern stopPattern = tripUpdate.stopPattern(); assertEquals(PATTERN.getStopPattern(), stopPattern);