private TripSchedule rerouteTripSchedule(TripSchedule originalSchedule){
  TripSchedule schedule=originalSchedule.clone();
  schedule.arrivals=new int[newPatternLength];
  schedule.departures=new int[newPatternLength];
  int prevOutputDeparture=0;
  for (int ss=0, ts=0; ts < newPatternLength; ss++, ts++) {
    if ((ss == insertBeginIndex - 1) || (ss == 0 && insertBeginIndex == 0)) {
      int hopTime=originalSchedule.arrivals[ss];
      if (ss > 0)       hopTime-=originalSchedule.departures[ss - 1];
      schedule.arrivals[ts]=prevOutputDeparture + hopTime;
      schedule.departures[ts]=schedule.arrivals[ts] + dwellTimes[0];
      prevOutputDeparture=schedule.departures[ts];
      ts++;
      for (int hop=0, dwell=1; dwell < dwellTimes.length; hop++, dwell++) {
        schedule.arrivals[ts]=prevOutputDeparture + hopTimes[hop];
        schedule.departures[ts]=schedule.arrivals[ts] + dwellTimes[dwell];
        prevOutputDeparture=schedule.departures[ts];
        ts++;
      }
      ss=insertEndIndex + 1;
      if (ts == newPatternLength)       break;
    }
    int hopTime=originalSchedule.arrivals[ss];
    if (ss > 0)     hopTime-=originalSchedule.departures[ss - 1];
    schedule.arrivals[ts]=prevOutputDeparture + hopTime;
    int dwellTime=originalSchedule.departures[ss] - originalSchedule.arrivals[ss];
    schedule.departures[ts]=schedule.arrivals[ts] + dwellTime;
    prevOutputDeparture=schedule.departures[ts];
  }
  int timeShift=originalSchedule.arrivals[originalFixedPointStopIndex] - schedule.arrivals[newFixedPointStopIndex];
  for (int i=0; i < newPatternLength; i++) {
    schedule.arrivals[i]+=timeShift;
    schedule.departures[i]+=timeShift;
  }
  if (LOG.isDebugEnabled()) {
    LOG.debug("Original arrivals:   {}",originalSchedule.arrivals);
    LOG.debug("Original departures: {}",originalSchedule.departures);
    LOG.debug("Modified arrivals:   {}",schedule.arrivals);
    LOG.debug("Modified departures: {}",schedule.departures);
  }
  return schedule;
}

private TripSchedule rerouteTripSchedule ( TripSchedule originalSchedule ) { TripSchedule schedule = originalSchedule . clone ( ) ; schedule . arrivals = new int [ newPatternLength ] ; schedule . departures = new int [ newPatternLength ] ; int prevOutputDeparture = 0 ; for ( int ss = 0 , ts = 0 ; ts < newPatternLength ; ss ++ , ts ++ ) { if ( ( ss == insertBeginIndex - 1 ) || ( ss == 0 && insertBeginIndex == 0 ) ) { int hopTime = originalSchedule . arrivals [ ss ] ; if ( ss > 0 ) hopTime -= originalSchedule . departures [ ss - 1 ] ; schedule . arrivals [ ts ] = prevOutputDeparture + hopTime ; schedule . departures [ ts ] = schedule . arrivals [ ts ] + dwellTimes [ 0 ] ; prevOutputDeparture = schedule . departures [ ts ] ; ts ++ ; for ( int hop = 0 , dwell = 1 ; dwell < dwellTimes . length ; hop ++ , dwell ++ ) { schedule . arrivals [ ts ] = prevOutputDeparture + hopTimes [ hop ] ; schedule . departures [ ts ] = schedule . arrivals [ ts ] + dwellTimes [ dwell ] ; prevOutputDeparture = schedule . departures [ ts ] ; ts ++ ; } ss = insertEndIndex + 1 ; if ( ts == newPatternLength ) break ; } int hopTime = originalSchedule . arrivals [ ss ] ; if ( ss > 0 ) hopTime -= originalSchedule . departures [ ss - 1 ] ; schedule . arrivals [ ts ] = prevOutputDeparture + hopTime ; int dwellTime = originalSchedule . departures [ ss ] - originalSchedule . arrivals [ ss ] ; schedule . departures [ ts ] = schedule . arrivals [ ts ] + dwellTime ; prevOutputDeparture = schedule . departures [ ts ] ; } int timeShift = originalSchedule . arrivals [ originalFixedPointStopIndex ] - schedule . arrivals [ newFixedPointStopIndex ] ; for ( int i = 0 ; i < newPatternLength ; i ++ ) { schedule . arrivals [ i ] += timeShift ; schedule . departures [ i ] += timeShift ; } if ( LOG . isDebugEnabled ( ) ) { LOG . debug ( "Original arrivals:   {}" , originalSchedule . arrivals ) ; LOG . debug ( "Original departures: {}" , originalSchedule . departures ) ; LOG . debug ( "Modified arrivals:   {}" , schedule . arrivals ) ; LOG . debug ( "Modified departures: {}" , schedule . departures ) ; } return schedule ; }
todo calculate all arrivals and departures zero - based , and then shift them at the end to maintain fixed - point stop . the fixed - point stop should be different depending on whether we are splicing onto the beginning or end of the route .
