Skip to content

Commit

Permalink
Avoid String concatenation in loops
Browse files Browse the repository at this point in the history
  • Loading branch information
otbutz committed Apr 11, 2024
1 parent 3f9b3bd commit 8c16310
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ private void checkQueries(GraphHopper hopper, List<Query> queries) {
// for edge-based routing we expect a slightly different algo name for CH
if (profile.hasTurnCosts())
expectedAlgo = expectedAlgo.replaceAll("\\|ch-routing", "|ch|edge_based|no_sod-routing");
assertTrue(res.getBest().getDebugInfo().contains(expectedAlgo),
assertTrue(res.getBest().getDebugInfo().toString().contains(expectedAlgo),
"Response does not contain expected algo string. Expected: '" + expectedAlgo +
"', got: '" + res.getBest().getDebugInfo() + "'");
}
Expand Down
24 changes: 12 additions & 12 deletions web-api/src/main/java/com/graphhopper/GHResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class GHResponse {
private final List<Throwable> errors = new ArrayList<>(4);
private PMap hintsMap = new PMap();
private final List<ResponsePath> responsePaths = new ArrayList<>(5);
private String debugInfo = "";
private final StringBuilder debugInfo = new StringBuilder();

public GHResponse() {
}
Expand Down Expand Up @@ -70,20 +70,20 @@ public void addDebugInfo(String debugInfo) {
throw new IllegalStateException("Debug information has to be none null");

if (!this.debugInfo.isEmpty())
this.debugInfo += "; ";
this.debugInfo.append("; ");

this.debugInfo += debugInfo;
this.debugInfo.append(debugInfo);
}

public String getDebugInfo() {
String str = debugInfo;
StringBuilder str = new StringBuilder(debugInfo);
for (ResponsePath p : responsePaths) {
if (!str.isEmpty())
str += "; ";
str.append("; ");

str += p.getDebugInfo();
str.append(p.getDebugInfo());
}
return str;
return str.toString();
}

/**
Expand Down Expand Up @@ -126,18 +126,18 @@ public GHResponse addError(Throwable error) {

@Override
public String toString() {
String str = "";
StringBuilder str = new StringBuilder();
for (ResponsePath a : responsePaths) {
str += "; " + a.toString();
str.append("; ").append(a.toString());
}

if (responsePaths.isEmpty())
str = "no paths";
str.append("no paths");

if (!errors.isEmpty())
str += ", main errors: " + errors.toString();
str.append(", main errors: ").append(errors);

return str;
return str.toString();
}

public void setHints(PMap hints) {
Expand Down
8 changes: 4 additions & 4 deletions web-api/src/main/java/com/graphhopper/ResponsePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ResponsePath {
private double descend;
private double routeWeight;
private long time;
private String debugInfo = "";
private final StringBuilder debugInfo = new StringBuilder();
private InstructionList instructions;
private PointList waypointList = PointList.EMPTY;
private List<Integer> waypointIndices = new ArrayList<>();
Expand Down Expand Up @@ -71,13 +71,13 @@ public ResponsePath addDebugInfo(String debugInfo) {
throw new IllegalStateException("Debug information has to be none null");

if (!this.debugInfo.isEmpty())
this.debugInfo += ";";
this.debugInfo.append(";");

this.debugInfo += debugInfo;
this.debugInfo.append(debugInfo);
return this;
}

public String getDebugInfo() {
public StringBuilder getDebugInfo() {
return debugInfo;
}

Expand Down

0 comments on commit 8c16310

Please sign in to comment.