short accumulatedStopTimeIndex = 0;
int accumulatedSlackTime = 0;
double distanceAlongBlock = 0;
BlockTripEntryImpl prevTrip = null;
StopTimeEntry prevTripStopTime = null;
double prevTripAvgVelocity = 0;
for (short i = 0; i < trips.size(); i++) {
TripEntry tripEntry = trips.get(i);
List<StopTimeEntry> stopTimes = tripEntry.getStopTimes();
/**
* See if there is any slack time in the schedule in the transition
* between the two trips. We take the distance between the last stop of
* the previous trip and the first stop of the next trip, along with the
* average travel velocity from the previous trip, and compute the
* estimated travel time. Any time that's left over is slack.
*/
if (prevTripStopTime != null) {
StopTimeEntry nextStopTime = stopTimes.get(0);
int slackTime = nextStopTime.getArrivalTime()
- prevTripStopTime.getDepartureTime();
double distance = (distanceAlongBlock - (prevTrip.getDistanceAlongBlock() + prevTripStopTime.getShapeDistTraveled()))
+ nextStopTime.getShapeDistTraveled();
if (prevTripAvgVelocity > 0) {
int timeToTravel = (int) (distance / prevTripAvgVelocity);
slackTime -= Math.min(timeToTravel, slackTime);
}