Program: 35420
Project: 35420
Buggy source:
public RouteResult createRouteResult(List<GHResponse> routes,RoutingRequest request,List<RouteExtraInfo> extras) throws Exception {
  RouteResult result=new RouteResult(request.getExtraInfo());
  if (routes.isEmpty())   return result;
  if (!LocalizationManager.getInstance().isLanguageSupported(request.getLanguage()))   throw new Exception("String_Node_Str" + request.getLanguage() + "String_Node_Str");
  InstructionTranslator instrTranslator=InstructionTranslatorsCache.getInstance().getTranslator(request.getLanguage());
  boolean formatInstructions=request.getInstructionsFormat() == RouteInstructionsFormat.HTML;
  int nRoutes=routes.size();
  double distance=0.0;
  double duration=0.0;
  double ascent=0.0;
  double descent=0.0;
  double distanceActual=0.0;
  double durationTraffic=0.0;
  double lon0=0, lat0=0, lat1=0, lon1=0;
  boolean includeDetourFactor=request.hasAttribute("String_Node_Str");
  boolean includeElev=request.getIncludeElevation();
  DistanceUnit units=request.getUnits();
  int unitDecimals=FormatUtility.getUnitDecimals(units);
  PointList prevSegPoints=null, segPoints, nextSegPoints;
  BBox bbox=null;
  int[] routeWayPoints=null;
  if (request.getIncludeGeometry()) {
    routeWayPoints=new int[nRoutes + 1];
    routeWayPoints[0]=0;
  }
  if (extras != null)   result.addExtraInfo(extras);
  for (int ri=0; ri < nRoutes; ++ri) {
    GHResponse resp=routes.get(ri);
    if (resp.hasErrors())     throw new InternalServerException(RoutingErrorCodes.UNKNOWN,String.format("String_Node_Str",ri,FormatUtility.formatCoordinate(request.getCoordinates()[ri]),ri + 1,FormatUtility.formatCoordinate(request.getCoordinates()[ri + 1])));
    PathWrapper path=resp.getBest();
    PointList routePoints=path.getPoints();
    if (bbox == null)     bbox=new BBox(routePoints.getLon(0),routePoints.getLon(0),routePoints.getLat(0),routePoints.getLat(0));
    bbox=path.calcRouteBBox(bbox);
    if (request.getIncludeGeometry()) {
      result.addPoints(routePoints,ri > 0,includeElev);
      routeWayPoints[ri + 1]=result.getGeometry().length - 1;
      if (request.getIncludeInstructions()) {
        InstructionList instructions=path.getInstructions();
        int startWayPointIndex=routeWayPoints[ri];
        int nInstructions=instructions.size();
        Instruction instr, prevInstr=null;
        InstructionType instrType, prevInstrType=InstructionType.UNKNOWN;
        RouteSegment seg=new RouteSegment(path,units);
        if (includeDetourFactor) {
          lat0=routePoints.getLat(0);
          lon0=routePoints.getLon(0);
          lat1=routePoints.getLat(routePoints.getSize() - 1);
          lon1=routePoints.getLon(routePoints.getSize() - 1);
          double dist=_distCalc.calcDist(lat0,lon0,lat1,lon1);
          seg.setDetourFactor((dist == 0) ? 0 : FormatUtility.roundToDecimals(path.getDistance() / dist,2));
        }
        RouteStep prevStep=null;
        String instrText="String_Node_Str";
        double stepDistance, stepDuration;
        for (int ii=0; ii < nInstructions; ++ii) {
          instr=instructions.get(ii);
          InstructionAnnotation instrAnnotation=instr.getAnnotation();
          instrType=getInstructionType(ii == 0,instr);
          segPoints=instr.getPoints();
          nextSegPoints=(ii + 1 < nInstructions) ? instructions.get(ii + 1).getPoints() : getNextSegPoints(routes,ri + 1,0);
          String roadName=formatInstructions && !Helper.isEmpty(instr.getName()) ? "String_Node_Str" + instr.getName() + "String_Node_Str" : instr.getName();
          instrText="String_Node_Str";
          stepDistance=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(instr.getDistance(),DistanceUnit.Meters,units),unitDecimals);
          stepDuration=FormatUtility.roundToDecimals(instr.getTime() / 1000.0,1);
          RouteStep step=new RouteStep();
          if (ii == 0) {
            if (segPoints.size() == 1) {
              if (ii + 1 < nInstructions) {
                lat1=nextSegPoints.getLat(0);
                lon1=nextSegPoints.getLon(0);
              }
 else {
                lat1=segPoints.getLat(ii);
                lon1=segPoints.getLon(ii);
              }
            }
 else {
              lat1=segPoints.getLat(ii + 1);
              lon1=segPoints.getLon(ii + 1);
            }
            CardinalDirection dir=calcDirection(segPoints.getLat(ii),segPoints.getLon(ii),lat1,lon1);
            instrText=instrTranslator.getDepart(dir,roadName);
          }
 else {
            if (instr instanceof RoundaboutInstruction) {
              RoundaboutInstruction raInstr=(RoundaboutInstruction)instr;
              step.setExitNumber(raInstr.getExitNumber());
              instrText=instrTranslator.getRoundabout(raInstr.getExitNumber(),roadName);
            }
 else {
              if (isTurnInstruction(instrType))               instrText=instrTranslator.getTurn(instrType,roadName);
 else               if (instrType == InstructionType.CONTINUE)               instrText=instrTranslator.getContinue(instrType,roadName);
 else               if (instrType == InstructionType.FINISH) {
                instrText=instrTranslator.getArrive(getArrivalDirection(routePoints,request.getDestination()),prevInstr.getName());
              }
 else               instrText="String_Node_Str";
            }
          }
          if (prevStep != null && instrType == InstructionType.CONTINUE && instrType == prevInstrType && canMergeInstructions(instr.getName(),prevInstr.getName())) {
            String mergedRoadName=mergeInstructions(instr.getName(),prevInstr.getName());
            if (_nameAppendix != null)             mergedRoadName+="String_Node_Str" + _nameAppendix + "String_Node_Str";
            if (formatInstructions)             mergedRoadName="String_Node_Str" + mergedRoadName + "String_Node_Str";
            int[] wayPoints=prevStep.getWayPoints();
            wayPoints[1]=wayPoints[1] + instr.getPoints().size();
            stepDuration=FormatUtility.roundToDecimals(instr.getTime() / 1000.0,1);
            prevStep.setDistance(FormatUtility.roundToDecimals(DistanceUnitUtil.convert(prevStep.getDistance() + stepDistance,DistanceUnit.Meters,units),unitDecimals));
            prevStep.setDuration(FormatUtility.roundToDecimals(prevStep.getDuration() + stepDuration,1));
            prevStep.setInstruction(instrTranslator.getContinue(instrType,mergedRoadName));
            prevStep.setName(mergedRoadName);
          }
 else {
            _nameAppendix=null;
            step.setDistance(stepDistance);
            step.setDuration(stepDuration);
            step.setInstruction(instrText);
            step.setName(instr.getName());
            step.setType(instrType.ordinal());
            step.setWayPoints(new int[]{startWayPointIndex,getWayPointEndIndex(startWayPointIndex,instrType,instr)});
            if (request.getIncludeManeuvers())             step.setManeuver(calcManeuver(instrType,prevSegPoints,segPoints,nextSegPoints));
            seg.addStep(step);
            prevStep=step;
          }
          startWayPointIndex+=instr.getPoints().size();
          if (instrAnnotation != null && instrAnnotation.getWayType() != 1)           distanceActual+=stepDistance;
          prevInstr=instr;
          prevInstrType=instrType;
          prevSegPoints=segPoints;
        }
        result.addSegment(seg);
        distance+=seg.getDistance();
        duration+=seg.getDuration();
      }
 else {
        distance+=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(path.getDistance(),DistanceUnit.Meters,units),FormatUtility.getUnitDecimals(units));
        duration+=FormatUtility.roundToDecimals(path.getTime() / 1000.0,1);
      }
    }
 else {
      InstructionList instructions=path.getInstructions();
      int nInstructions=instructions.size();
      if (nInstructions > 1)       nInstructions-=1;
      for (int j=0; j < nInstructions; ++j) {
        Instruction instr=instructions.get(j);
        InstructionAnnotation instrAnnotation=instr.getAnnotation();
        if (instrAnnotation != null && instrAnnotation.getWayType() != 1)         distanceActual+=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(instr.getDistance(),DistanceUnit.Meters,units),unitDecimals);
      }
      distance+=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(path.getDistance(),DistanceUnit.Meters,units),unitDecimals);
      duration+=FormatUtility.roundToDecimals(path.getTime() / 1000.0,1);
    }
    if (includeElev) {
      ascent+=path.getAscend();
      descent+=path.getDescend();
    }
    durationTraffic+=path.getRouteWeight();
  }
  RouteSummary routeSummary=result.getSummary();
  routeSummary.setDuration(request.getSearchParameters().getConsiderTraffic() ? durationTraffic : duration);
  routeSummary.setDistance(FormatUtility.roundToDecimals(distance,unitDecimals));
  routeSummary.setDistanceActual(FormatUtility.roundToDecimals(distanceActual,unitDecimals));
  routeSummary.setAverageSpeed(FormatUtility.roundToDecimals(distance / (units == DistanceUnit.Meters ? 1000 : 1) / (routeSummary.getDuration() / 3600),1));
  routeSummary.setAscent(FormatUtility.roundToDecimals(ascent,1));
  routeSummary.setDescent(FormatUtility.roundToDecimals(descent,1));
  if (routeWayPoints != null)   result.setWayPointsIndices(routeWayPoints);
  if (bbox != null)   routeSummary.setBBox(bbox);
  return result;
}

Fixed version (ground truth):
public RouteResult createRouteResult(List<GHResponse> routes,RoutingRequest request,List<RouteExtraInfo> extras) throws Exception {
  RouteResult result=new RouteResult(request.getExtraInfo());
  if (routes.isEmpty())   return result;
  if (!LocalizationManager.getInstance().isLanguageSupported(request.getLanguage()))   throw new Exception("String_Node_Str" + request.getLanguage() + "String_Node_Str");
  InstructionTranslator instrTranslator=InstructionTranslatorsCache.getInstance().getTranslator(request.getLanguage());
  boolean formatInstructions=request.getInstructionsFormat() == RouteInstructionsFormat.HTML;
  int nRoutes=routes.size();
  double distance=0.0;
  double duration=0.0;
  double ascent=0.0;
  double descent=0.0;
  double distanceActual=0.0;
  double durationTraffic=0.0;
  double lon0=0, lat0=0, lat1=0, lon1=0;
  boolean includeDetourFactor=request.hasAttribute("String_Node_Str");
  boolean includeElev=request.getIncludeElevation();
  DistanceUnit units=request.getUnits();
  int unitDecimals=FormatUtility.getUnitDecimals(units);
  PointList prevSegPoints=null, segPoints, nextSegPoints;
  BBox bbox=null;
  int[] routeWayPoints=null;
  if (request.getIncludeGeometry()) {
    routeWayPoints=new int[nRoutes + 1];
    routeWayPoints[0]=0;
  }
  if (extras != null)   result.addExtraInfo(extras);
  for (int ri=0; ri < nRoutes; ++ri) {
    GHResponse resp=routes.get(ri);
    if (resp.hasErrors())     throw new InternalServerException(RoutingErrorCodes.UNKNOWN,String.format("String_Node_Str",ri,FormatUtility.formatCoordinate(request.getCoordinates()[ri]),ri + 1,FormatUtility.formatCoordinate(request.getCoordinates()[ri + 1])));
    PathWrapper path=resp.getBest();
    PointList routePoints=path.getPoints();
    if (bbox == null)     bbox=new BBox(routePoints.getLon(0),routePoints.getLon(0),routePoints.getLat(0),routePoints.getLat(0));
    bbox=path.calcRouteBBox(bbox);
    if (request.getIncludeGeometry()) {
      result.addPoints(routePoints,ri > 0,includeElev);
      routeWayPoints[ri + 1]=result.getGeometry().length - 1;
      if (request.getIncludeInstructions()) {
        InstructionList instructions=path.getInstructions();
        int startWayPointIndex=routeWayPoints[ri];
        int nInstructions=instructions.size();
        Instruction instr, prevInstr=null;
        InstructionType instrType, prevInstrType=InstructionType.UNKNOWN;
        RouteSegment seg=new RouteSegment(path,units);
        if (includeDetourFactor) {
          lat0=routePoints.getLat(0);
          lon0=routePoints.getLon(0);
          lat1=routePoints.getLat(routePoints.getSize() - 1);
          lon1=routePoints.getLon(routePoints.getSize() - 1);
          double dist=_distCalc.calcDist(lat0,lon0,lat1,lon1);
          seg.setDetourFactor((dist == 0) ? 0 : FormatUtility.roundToDecimals(path.getDistance() / dist,2));
        }
        RouteStep prevStep=null;
        String instrText="String_Node_Str";
        double stepDistance, stepDuration;
        for (int ii=0; ii < nInstructions; ++ii) {
          instr=instructions.get(ii);
          InstructionAnnotation instrAnnotation=instr.getAnnotation();
          instrType=getInstructionType(ii == 0,instr);
          segPoints=instr.getPoints();
          nextSegPoints=(ii + 1 < nInstructions) ? instructions.get(ii + 1).getPoints() : getNextSegPoints(routes,ri + 1,0);
          String roadName=formatInstructions && !Helper.isEmpty(instr.getName()) ? "String_Node_Str" + instr.getName() + "String_Node_Str" : instr.getName();
          instrText="String_Node_Str";
          stepDistance=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(instr.getDistance(),DistanceUnit.Meters,units),unitDecimals);
          stepDuration=FormatUtility.roundToDecimals(instr.getTime() / 1000.0,1);
          RouteStep step=new RouteStep();
          if (ii == 0) {
            if (segPoints.size() == 1) {
              if (ii + 1 < nInstructions) {
                lat1=nextSegPoints.getLat(0);
                lon1=nextSegPoints.getLon(0);
              }
 else {
                lat1=segPoints.getLat(ii);
                lon1=segPoints.getLon(ii);
              }
            }
 else {
              lat1=segPoints.getLat(ii + 1);
              lon1=segPoints.getLon(ii + 1);
            }
            CardinalDirection dir=calcDirection(segPoints.getLat(ii),segPoints.getLon(ii),lat1,lon1);
            instrText=instrTranslator.getDepart(dir,roadName);
          }
 else {
            if (instr instanceof RoundaboutInstruction) {
              RoundaboutInstruction raInstr=(RoundaboutInstruction)instr;
              step.setExitNumber(raInstr.getExitNumber());
              instrText=instrTranslator.getRoundabout(raInstr.getExitNumber(),roadName);
            }
 else {
              if (isTurnInstruction(instrType))               instrText=instrTranslator.getTurn(instrType,roadName);
 else               if (instrType == InstructionType.CONTINUE)               instrText=instrTranslator.getContinue(instrType,roadName);
 else               if (instrType == InstructionType.FINISH) {
                instrText=instrTranslator.getArrive(getArrivalDirection(routePoints,request.getDestination()),prevInstr.getName());
              }
 else               instrText="String_Node_Str";
            }
          }
          if (prevStep != null && instrType == prevInstrType && canMergeInstructions(instr.getName(),prevInstr.getName())) {
            String mergedRoadName=mergeInstructions(instr.getName(),prevInstr.getName());
            int[] wayPoints=prevStep.getWayPoints();
            wayPoints[1]=wayPoints[1] + instr.getPoints().size();
            stepDuration=FormatUtility.roundToDecimals(instr.getTime() / 1000.0,1);
            prevStep.setDistance(FormatUtility.roundToDecimals(DistanceUnitUtil.convert(prevStep.getDistance() + stepDistance,DistanceUnit.Meters,units),unitDecimals));
            prevStep.setDuration(FormatUtility.roundToDecimals(prevStep.getDuration() + stepDuration,1));
            prevStep.setName(mergedRoadName);
            if (_nameAppendix != null)             mergedRoadName+="String_Node_Str" + _nameAppendix + "String_Node_Str";
            if (formatInstructions)             mergedRoadName="String_Node_Str" + mergedRoadName + "String_Node_Str";
            prevStep.setInstruction(instrTranslator.getContinue(instrType,mergedRoadName));
          }
 else {
            _nameAppendix=null;
            step.setDistance(stepDistance);
            step.setDuration(stepDuration);
            step.setInstruction(instrText);
            step.setName(instr.getName());
            step.setType(instrType.ordinal());
            step.setWayPoints(new int[]{startWayPointIndex,getWayPointEndIndex(startWayPointIndex,instrType,instr)});
            if (request.getIncludeManeuvers())             step.setManeuver(calcManeuver(instrType,prevSegPoints,segPoints,nextSegPoints));
            seg.addStep(step);
            prevStep=step;
          }
          startWayPointIndex+=instr.getPoints().size();
          if (instrAnnotation != null && instrAnnotation.getWayType() != 1)           distanceActual+=stepDistance;
          prevInstr=instr;
          prevInstrType=instrType;
          prevSegPoints=segPoints;
        }
        result.addSegment(seg);
        distance+=seg.getDistance();
        duration+=seg.getDuration();
      }
 else {
        distance+=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(path.getDistance(),DistanceUnit.Meters,units),FormatUtility.getUnitDecimals(units));
        duration+=FormatUtility.roundToDecimals(path.getTime() / 1000.0,1);
      }
    }
 else {
      InstructionList instructions=path.getInstructions();
      int nInstructions=instructions.size();
      if (nInstructions > 1)       nInstructions-=1;
      for (int j=0; j < nInstructions; ++j) {
        Instruction instr=instructions.get(j);
        InstructionAnnotation instrAnnotation=instr.getAnnotation();
        if (instrAnnotation != null && instrAnnotation.getWayType() != 1)         distanceActual+=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(instr.getDistance(),DistanceUnit.Meters,units),unitDecimals);
      }
      distance+=FormatUtility.roundToDecimals(DistanceUnitUtil.convert(path.getDistance(),DistanceUnit.Meters,units),unitDecimals);
      duration+=FormatUtility.roundToDecimals(path.getTime() / 1000.0,1);
    }
    if (includeElev) {
      ascent+=path.getAscend();
      descent+=path.getDescend();
    }
    durationTraffic+=path.getRouteWeight();
  }
  RouteSummary routeSummary=result.getSummary();
  routeSummary.setDuration(request.getSearchParameters().getConsiderTraffic() ? durationTraffic : duration);
  routeSummary.setDistance(FormatUtility.roundToDecimals(distance,unitDecimals));
  routeSummary.setDistanceActual(FormatUtility.roundToDecimals(distanceActual,unitDecimals));
  routeSummary.setAverageSpeed(FormatUtility.roundToDecimals(distance / (units == DistanceUnit.Meters ? 1000 : 1) / (routeSummary.getDuration() / 3600),1));
  routeSummary.setAscent(FormatUtility.roundToDecimals(ascent,1));
  routeSummary.setDescent(FormatUtility.roundToDecimals(descent,1));
  if (routeWayPoints != null)   result.setWayPointsIndices(routeWayPoints);
  if (bbox != null)   routeSummary.setBBox(bbox);
  return result;
}