diff --git a/src/main/java/org/opentripplanner/model/TimetableSnapshot.java b/src/main/java/org/opentripplanner/model/TimetableSnapshot.java index 5c018412572..63bef21cfac 100644 --- a/src/main/java/org/opentripplanner/model/TimetableSnapshot.java +++ b/src/main/java/org/opentripplanner/model/TimetableSnapshot.java @@ -330,10 +330,23 @@ public boolean revertTripToScheduledTripPattern(FeedScopedId tripId, LocalDate s } if (tripTimesToRemove != null) { - for (Timetable sortedTimetable : sortedTimetables) { - boolean isDirty = sortedTimetable.getTripTimes().remove(tripTimesToRemove); + for (Timetable originalTimetable : sortedTimetables) { + boolean isDirty = originalTimetable.getTripTimes().contains(tripTimesToRemove); if (isDirty) { - dirtyTimetables.add(sortedTimetable); + Timetable updatedTimetable; + if (dirtyTimetables.contains(originalTimetable)) { + // the timetable has already been copied-on-write in this snapshot + // no need to copy it again + updatedTimetable = originalTimetable; + } else { + // create a copy of this timetable and swap the copy and the original + updatedTimetable = new Timetable(originalTimetable, serviceDate); + sortedTimetables.remove(originalTimetable); + sortedTimetables.add(updatedTimetable); + dirtyTimetables.add(updatedTimetable); + dirty = true; + } + updatedTimetable.getTripTimes().remove(tripTimesToRemove); } } }