@@ -80,12 +80,11 @@ private List<GraphPath<State, Edge, Vertex>> findArriveByPaths(
8080 lastLocations .add (baseRequest .to ());
8181 var minimumWaitTimes = getMinimumWaitTimes (baseRequest );
8282
83- var totalTravelDuration = 0 ;
8483 var paths = new ArrayList <GraphPath <State , Edge , Vertex >>();
8584 var newStartTime = request .dateTime ();
86- var maxDuration = getMaximumDirectDuration (request , mode );
87- var maxDurationLeft = maxDuration ;
88- for ( int i = lastLocations . size () - 2 ; i >= 0 ; i -- ) {
85+ var maxDurationLeft = getMaximumDirectDuration (request , mode );
86+ int i = lastLocations . size () - 2 ;
87+ while ( i >= 0 && maxDurationLeft . isPositive () ) {
8988 var from = lastLocations .get (i );
9089 var to = lastLocations .get (i + 1 );
9190 var patchedRequest = getRequest (
@@ -103,8 +102,8 @@ private List<GraphPath<State, Edge, Vertex>> findArriveByPaths(
103102 newStartTime = Instant .ofEpochSecond (path .getStartTime ()).minus (minimumWaitTime );
104103 // Wait time is not counted here as it doesn't slow down routing or inconvenience travelers
105104 // like travel time does
106- totalTravelDuration += path .getDuration ();
107- maxDurationLeft = maxDuration . minus ( Duration . ofSeconds ( totalTravelDuration )) ;
105+ maxDurationLeft = maxDurationLeft . minus ( Duration . ofSeconds ( path .getDuration ()) );
106+ i -- ;
108107 }
109108
110109 var firstRequest = getRequest (
@@ -113,7 +112,7 @@ private List<GraphPath<State, Edge, Vertex>> findArriveByPaths(
113112 baseRequest .listViaLocationsWithCoordinates ().getFirst (),
114113 newStartTime ,
115114 mode ,
116- maxDuration
115+ maxDurationLeft
117116 );
118117 paths .add (graphPathFinder .graphPathFinderEntryPoint (firstRequest , linkingContext ));
119118 return paths .reversed ();
@@ -137,13 +136,13 @@ private List<GraphPath<State, Edge, Vertex>> findDepartAfterPaths(
137136 var lastLocations = new ArrayList <>(vias );
138137 lastLocations .add (baseRequest .to ());
139138 var minimumWaitTimes = getMinimumWaitTimes (baseRequest );
140-
141- var totalTravelDuration = paths .getFirst ().getDuration ();
142- var maxDuration = getMaximumDirectDuration (request , mode );
143- for (int i = 0 ; i < lastLocations .size () - 1 ; i ++) {
139+ var maxDurationLeft = getMaximumDirectDuration (request , mode ).minus (
140+ Duration .ofSeconds (paths .getFirst ().getDuration ())
141+ );
142+ int i = 0 ;
143+ while (i < lastLocations .size () - 1 && maxDurationLeft .isPositive ()) {
144144 var from = lastLocations .get (i );
145145 var to = lastLocations .get (i + 1 );
146- var maxDurationLeft = maxDuration .minus (Duration .ofSeconds (totalTravelDuration ));
147146 var minimumWaitTime = minimumWaitTimes .get (i );
148147 var newStartTime = Instant .ofEpochSecond (paths .getLast ().getEndTime ()).plus (minimumWaitTime );
149148 var patchedRequest = getRequest (
@@ -158,7 +157,8 @@ private List<GraphPath<State, Edge, Vertex>> findDepartAfterPaths(
158157 paths .add (path );
159158 // Wait time is not counted here as it doesn't slow down routing or inconvenience travelers
160159 // like travel time does
161- totalTravelDuration += path .getDuration ();
160+ maxDurationLeft = maxDurationLeft .minus (Duration .ofSeconds (path .getDuration ()));
161+ i ++;
162162 }
163163 return paths ;
164164 }
0 commit comments