Skip to content

Commit

Permalink
Unify Edge toString methods
Browse files Browse the repository at this point in the history
  • Loading branch information
habrahamsson-skanetrafiken committed Sep 13, 2024
1 parent 7ac66ba commit 345b2a9
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ public static StreetVehicleRentalLink createStreetVehicleRentalLink(
return connectToGraph(new StreetVehicleRentalLink(fromv, tov));
}

public String toString() {
return "StreetVehicleRentalLink(" + fromv + " -> " + tov + ")";
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/org/opentripplanner/street/model/edge/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Objects;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.locationtech.jts.geom.LineString;
import org.opentripplanner.astar.spi.AStarEdge;
import org.opentripplanner.framework.i18n.I18NString;
Expand Down Expand Up @@ -99,8 +101,28 @@ public int hashCode() {
return Objects.hash(fromv, tov);
}

@Override
public String toString() {
return String.format("%s (%s -> %s)", getClass().getName(), fromv, tov);
return buildToString(null, b -> {});
}

/**
* Use this to construct a {@code toString()} in derived classes. The result will be:
* {@code "%simple-class-name%(%name%, %fromv% -> %tov%" + body + ")"}. Note! There is no space
* between {@code tov} and {@code body}. Both name and body are optional.
*
* @param name Will add name to the string if not null.
* @param body Use this callback to add local content.
*/
protected String buildToString(@Nullable String name, Consumer<StringBuilder> body) {
var buf = new StringBuilder();
buf.append(getClass().getSimpleName()).append("(");
if (name != null) {
buf.append(name).append(", ");
}
buf.append(fromv).append(" -> ").append(tov);
body.accept(buf);
return buf.append(')').toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ public static ElevatorAlightEdge createElevatorAlightEdge(
return connectToGraph(new ElevatorAlightEdge(from, to, level));
}

public String toString() {
return "ElevatorAlightEdge(" + fromv + " -> " + tov + ")";
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public static ElevatorBoardEdge createElevatorBoardEdge(
return connectToGraph(new ElevatorBoardEdge(from, to));
}

@Override
public String toString() {
return ToStringBuilder.of(this.getClass()).addObj("from", fromv).addObj("to", tov).toString();
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ public StreetTraversalPermission getPermission() {
return permission;
}

@Override
public String toString() {
return ToStringBuilder.of(this.getClass()).addObj("from", fromv).addObj("to", tov).toString();
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public static FreeEdge createFreeEdge(Vertex from, Vertex to) {
return connectToGraph(new FreeEdge(from, to));
}

public String toString() {
return "FreeEdge(" + fromv + " -> " + tov + ")";
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/org/opentripplanner/street/model/edge/StreetEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,17 @@ public double getEffectiveWalkSafetyDistance() {
}

public String toString() {
return (
"StreetEdge(" +
name +
", " +
fromv +
" -> " +
tov +
" length=" +
this.getDistanceMeters() +
" carSpeed=" +
this.getCarSpeed() +
" permission=" +
this.getPermission() +
")"
var nameString = name != null ? name.toString() : null;
return buildToString(
nameString,
b ->
b
.append(", length=")
.append(this.getDistanceMeters())
.append(", carSpeed=")
.append(this.getCarSpeed())
.append(", permission=")
.append(this.getPermission())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,4 @@ public static StreetStationCentroidLink createStreetStationLink(
) {
return connectToGraph(new StreetStationCentroidLink(fromv, tov));
}

public String toString() {
return "StreetStationCentroidLink(" + fromv + " -> " + tov + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ protected StreetTransitEntityLink(
this.wheelchairAccessibility = wheelchairAccessibility;
}

public String toString() {
return ToStringBuilder.of(this.getClass()).addObj("from", fromv).addObj("to", tov).toString();
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public boolean isExit() {
return !isEntrance;
}

public String toString() {
return "StreetTransitEntranceLink(" + fromv + " -> " + tov + ")";
}

protected int getStreetToStopTime() {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,4 @@ protected int getStreetToStopTime() {
? 0
: getTransitEntityVertex().getStreetToStopTime();
}

public String toString() {
return "StreetTransitStopLink(" + fromv + " -> " + tov + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ public static StreetVehicleParkingLink createStreetVehicleParkingLink(
return connectToGraph(new StreetVehicleParkingLink(fromv, tov));
}

@Override
public String toString() {
return ToStringBuilder.of(this.getClass()).addObj("fromv", fromv).addObj("tov", tov).toString();
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ public static TemporaryFreeEdge createTemporaryFreeEdge(Vertex from, TemporaryVe
return connectToGraph(new TemporaryFreeEdge(from, to));
}

@Override
public String toString() {
return "Temporary" + super.toString();
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,10 @@ public StreetEdge getParentEdge() {

@Override
public String toString() {
return (
"TemporaryPartialStreetEdge(" +
this.getDefaultName() +
", " +
this.getFromVertex() +
" -> " +
this.getToVertex() +
" length=" +
this.getDistanceMeters() +
" carSpeed=" +
this.getCarSpeed() +
" parentEdge=" +
parentEdge +
")"
return buildToString(
this.getDefaultName(),
b ->
b.append(", length=").append(this.getCarSpeed()).append(", parentEdge=").append(parentEdge)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ public boolean equals(Object o) {
return false;
}

public String toString() {
return "VehicleParkingEdge(" + fromv + " -> " + tov + ")";
}

@Override
@Nonnull
public State[] traverse(State s0) {
Expand Down

0 comments on commit 345b2a9

Please sign in to comment.