From b508c79f6c8679b3c40a9bb555a22752b4e7ee12 Mon Sep 17 00:00:00 2001 From: Vincent Paturet Date: Fri, 12 Jul 2024 09:53:08 +0200 Subject: [PATCH] Make TimetableSnapshot state final --- .../model/TimetableSnapshot.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/opentripplanner/model/TimetableSnapshot.java b/src/main/java/org/opentripplanner/model/TimetableSnapshot.java index d7d9385d9b8..7d3f6181d44 100644 --- a/src/main/java/org/opentripplanner/model/TimetableSnapshot.java +++ b/src/main/java/org/opentripplanner/model/TimetableSnapshot.java @@ -93,7 +93,7 @@ public class TimetableSnapshot { * The compound key approach better reflects the fact that there should be only one Timetable per * TripPattern and date. */ - private Map> timetables = new HashMap<>(); + private final Map> timetables; /** * For cases where the trip pattern (sequence of stops visited) has been changed by a realtime @@ -101,7 +101,7 @@ public class TimetableSnapshot { * trip ID and the service date. * TODO RT_AB: clarify if this is an index or the original source of truth. */ - private Map realtimeAddedTripPattern = new HashMap<>(); + private final Map realtimeAddedTripPattern; /** * This is an index of TripPatterns, not the primary collection. It tracks which TripPatterns @@ -111,13 +111,13 @@ public class TimetableSnapshot { * more than once. * TODO RT_AB: More general handling of all realtime indexes outside primary data structures. */ - private SetMultimap patternsForStop = HashMultimap.create(); + private final SetMultimap patternsForStop; /** * Boolean value indicating that timetable snapshot is read only if true. Once it is true, it * shouldn't be possible to change it to false anymore. */ - private boolean readOnly = false; + private final boolean readOnly; /** * Boolean value indicating that this timetable snapshot contains changes compared to the state of @@ -125,6 +125,22 @@ public class TimetableSnapshot { */ private boolean dirty = false; + public TimetableSnapshot() { + this(new HashMap<>(), new HashMap<>(), HashMultimap.create(), false); + } + + private TimetableSnapshot( + Map> timetables, + Map realtimeAddedTripPattern, + SetMultimap patternsForStop, + boolean readOnly + ) { + this.timetables = timetables; + this.realtimeAddedTripPattern = realtimeAddedTripPattern; + this.patternsForStop = patternsForStop; + this.readOnly = readOnly; + } + /** * Returns an updated timetable for the specified pattern if one is available in this snapshot, or * the originally scheduled timetable if there are no updates in this snapshot. @@ -235,12 +251,15 @@ public TimetableSnapshot commit(TransitLayerUpdater transitLayerUpdater, boolean throw new ConcurrentModificationException("This TimetableSnapshot is read-only."); } - TimetableSnapshot ret = new TimetableSnapshot(); if (!force && !this.isDirty()) { return null; } - ret.timetables = Map.copyOf(timetables); - ret.realtimeAddedTripPattern = Map.copyOf(realtimeAddedTripPattern); + TimetableSnapshot ret = new TimetableSnapshot( + Map.copyOf(timetables), + Map.copyOf(realtimeAddedTripPattern), + ImmutableSetMultimap.copyOf(patternsForStop), + true + ); if (transitLayerUpdater != null) { transitLayerUpdater.update(dirtyTimetables, timetables); @@ -249,9 +268,6 @@ public TimetableSnapshot commit(TransitLayerUpdater transitLayerUpdater, boolean this.dirtyTimetables.clear(); this.dirty = false; - ret.patternsForStop = ImmutableSetMultimap.copyOf(patternsForStop); - - ret.readOnly = true; // mark the snapshot as henceforth immutable return ret; }