From 134c5bd4d798f6d89bce8d59bcb5271f80d749d3 Mon Sep 17 00:00:00 2001 From: Thomas Gran Date: Mon, 20 Nov 2023 21:32:14 +0100 Subject: [PATCH] refactor:Improve SearchParams#toString() --- .../framework/tostring/ToStringBuilder.java | 18 ++++++++++++------ .../raptor/api/model/RaptorAccessEgress.java | 7 ++++++- .../raptor/api/request/SearchParams.java | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/opentripplanner/framework/tostring/ToStringBuilder.java b/src/main/java/org/opentripplanner/framework/tostring/ToStringBuilder.java index dda7904002f..d2cec512420 100644 --- a/src/main/java/org/opentripplanner/framework/tostring/ToStringBuilder.java +++ b/src/main/java/org/opentripplanner/framework/tostring/ToStringBuilder.java @@ -186,21 +186,27 @@ public ToStringBuilder addCol(String name, Collection c, Function c, int maxLimit) { + public ToStringBuilder addCollection( + String name, + Collection c, + int maxLimit, + Function toString + ) { if (c == null) { return this; } if (c.size() > maxLimit + 1) { - String value = c - .stream() - .limit(maxLimit) - .map(Object::toString) - .collect(Collectors.joining(", ")); + String value = c.stream().limit(maxLimit).map(toString).collect(Collectors.joining(", ")); return addIt(name + "(" + maxLimit + "/" + c.size() + ")", "[" + value + ", ..]"); } return addIfNotNull(name, c); } + /** Add the collection, truncate the number of elements at given maxLimit. */ + public ToStringBuilder addCollection(String name, Collection c, int maxLimit) { + return addCollection(name, c, maxLimit, Object::toString); + } + public ToStringBuilder addColSize(String name, Collection c) { return addIfNotNull(name, c, x -> String.format("%d items", x.size())); } diff --git a/src/main/java/org/opentripplanner/raptor/api/model/RaptorAccessEgress.java b/src/main/java/org/opentripplanner/raptor/api/model/RaptorAccessEgress.java index 59033de3d7e..1953c60d5d6 100644 --- a/src/main/java/org/opentripplanner/raptor/api/model/RaptorAccessEgress.java +++ b/src/main/java/org/opentripplanner/raptor/api/model/RaptorAccessEgress.java @@ -183,7 +183,12 @@ default boolean isFree() { return durationInSeconds() == 0; } - /** Call this from toString */ + /** Call this from toString or {@link #asString(boolean, boolean, String)}*/ + default String defaultToString() { + return asString(true, true, null); + } + + /** Call this from toString or {@link #defaultToString()} */ default String asString(boolean includeStop, boolean includeCost, @Nullable String summary) { StringBuilder buf = new StringBuilder(); if (isFree()) { diff --git a/src/main/java/org/opentripplanner/raptor/api/request/SearchParams.java b/src/main/java/org/opentripplanner/raptor/api/request/SearchParams.java index 5f2ae4a7e3b..28651104f9d 100644 --- a/src/main/java/org/opentripplanner/raptor/api/request/SearchParams.java +++ b/src/main/java/org/opentripplanner/raptor/api/request/SearchParams.java @@ -288,8 +288,8 @@ public String toString() { .addDurationSec("searchWindow", searchWindowInSeconds, RaptorConstants.NOT_SET) .addBoolIfTrue("departAsLateAsPossible", preferLateArrival) .addNum("numberOfAdditionalTransfers", numberOfAdditionalTransfers, RaptorConstants.NOT_SET) - .addCollection("accessPaths", accessPaths, 5) - .addCollection("egressPaths", egressPaths, 5) + .addCollection("accessPaths", accessPaths, 5, RaptorAccessEgress::defaultToString) + .addCollection("egressPaths", egressPaths, 5, RaptorAccessEgress::defaultToString) .toString(); }