Skip to content

Commit

Permalink
Make TimetableSnapshot state final
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Jul 12, 2024
1 parent 6d9ff33 commit b508c79
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/main/java/org/opentripplanner/model/TimetableSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ public class TimetableSnapshot {
* The compound key approach better reflects the fact that there should be only one Timetable per
* TripPattern and date.
*/
private Map<TripPattern, SortedSet<Timetable>> timetables = new HashMap<>();
private final Map<TripPattern, SortedSet<Timetable>> timetables;

/**
* For cases where the trip pattern (sequence of stops visited) has been changed by a realtime
* update, a Map associating the updated trip pattern with a compound key of the feed-scoped
* trip ID and the service date.
* TODO RT_AB: clarify if this is an index or the original source of truth.
*/
private Map<TripIdAndServiceDate, TripPattern> realtimeAddedTripPattern = new HashMap<>();
private final Map<TripIdAndServiceDate, TripPattern> realtimeAddedTripPattern;

/**
* This is an index of TripPatterns, not the primary collection. It tracks which TripPatterns
Expand All @@ -111,20 +111,36 @@ public class TimetableSnapshot {
* more than once.
* TODO RT_AB: More general handling of all realtime indexes outside primary data structures.
*/
private SetMultimap<StopLocation, TripPattern> patternsForStop = HashMultimap.create();
private final SetMultimap<StopLocation, TripPattern> 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
* the last commit if true.
*/
private boolean dirty = false;

public TimetableSnapshot() {
this(new HashMap<>(), new HashMap<>(), HashMultimap.create(), false);
}

private TimetableSnapshot(
Map<TripPattern, SortedSet<Timetable>> timetables,
Map<TripIdAndServiceDate, TripPattern> realtimeAddedTripPattern,
SetMultimap<StopLocation, TripPattern> 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.
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit b508c79

Please sign in to comment.