Skip to content

Commit

Permalink
refactor:Improve SearchParams#toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
t2gran committed Nov 20, 2023
1 parent 8611d57 commit 134c5bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,27 @@ public <T> ToStringBuilder addCol(String name, Collection<T> c, Function<T, Stri
}

/** Add the collection, truncate the number of elements at given maxLimit. */
public ToStringBuilder addCollection(String name, Collection<?> c, int maxLimit) {
public <T> ToStringBuilder addCollection(
String name,
Collection<T> c,
int maxLimit,
Function<T, String> 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 <T> ToStringBuilder addCollection(String name, Collection<T> 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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 134c5bd

Please sign in to comment.