From 8c16310474c2e769de68b57df4f3ae872e454089 Mon Sep 17 00:00:00 2001 From: Thomas Butz Date: Thu, 11 Apr 2024 10:20:52 +0200 Subject: [PATCH] Avoid String concatenation in loops --- .../routing/RoutingAlgorithmWithOSMTest.java | 2 +- .../main/java/com/graphhopper/GHResponse.java | 24 +++++++++---------- .../java/com/graphhopper/ResponsePath.java | 8 +++---- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMTest.java b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMTest.java index 5e62a603eed..0cbce97c208 100644 --- a/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMTest.java +++ b/core/src/test/java/com/graphhopper/routing/RoutingAlgorithmWithOSMTest.java @@ -733,7 +733,7 @@ private void checkQueries(GraphHopper hopper, List 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() + "'"); } diff --git a/web-api/src/main/java/com/graphhopper/GHResponse.java b/web-api/src/main/java/com/graphhopper/GHResponse.java index ffd99461226..2ec22ef3c9f 100644 --- a/web-api/src/main/java/com/graphhopper/GHResponse.java +++ b/web-api/src/main/java/com/graphhopper/GHResponse.java @@ -32,7 +32,7 @@ public class GHResponse { private final List errors = new ArrayList<>(4); private PMap hintsMap = new PMap(); private final List responsePaths = new ArrayList<>(5); - private String debugInfo = ""; + private final StringBuilder debugInfo = new StringBuilder(); public GHResponse() { } @@ -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(); } /** @@ -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) { diff --git a/web-api/src/main/java/com/graphhopper/ResponsePath.java b/web-api/src/main/java/com/graphhopper/ResponsePath.java index 8638efc73c7..bb79e06cff3 100644 --- a/web-api/src/main/java/com/graphhopper/ResponsePath.java +++ b/web-api/src/main/java/com/graphhopper/ResponsePath.java @@ -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 waypointIndices = new ArrayList<>(); @@ -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; }