Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.opentripplanner.transit.raptor.util;
import java.util.Calendar;
import javax.annotation.Nullable;
import org.opentripplanner.model.base.OtpNumberFormat;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.transit.raptor.api.transit.CostCalculator;
import org.opentripplanner.transit.raptor.api.transit.RaptorStopNameResolver;
import org.opentripplanner.transit.raptor.api.transit.RaptorTransfer;
import org.opentripplanner.util.time.DurationUtils;
import org.opentripplanner.util.time.TimeUtils;
/**
* Create a path like: {@code Walk 5m - 101 - Transit 10:07 10:35 - 2111 - Walk 4m }
*/
@SuppressWarnings("UnusedReturnValue")
public class PathStringBuilder {
private final RaptorStopNameResolver stopNameResolver;
private final StringBuilder buf = new StringBuilder();
private final boolean padDuration;
private boolean elementAdded = false;
private boolean sepAdded = false;
public PathStringBuilder(@Nullable RaptorStopNameResolver stopNameResolver) {
this(stopNameResolver, false);
}
/**
* @param stopNameResolver Used to translate stopIndexes to stopNames, if {@code null} the
* index is used in the result string.
* @param padDuration This can be set to {@code true} for padding the duration output.
* This would be used in cases were several similar paths are listed.
* If the legs are similar, the path elements is more likely to be
* aligned.
*/
public PathStringBuilder(@Nullable RaptorStopNameResolver stopNameResolver, boolean padDuration) {
this.stopNameResolver = RaptorStopNameResolver.nullSafe(stopNameResolver);
this.padDuration = padDuration;
}
public PathStringBuilder sep() {
sepAdded = true;
return this;
}
/**
* The given {@code stopIndex} is translated to stop name using the {@code stopNameTranslator}
* set in the constructor. If not translator is set the stopIndex is used.
*/
public PathStringBuilder stop(int stopIndex) {
return stop(stopNameResolver.apply(stopIndex));
}
public PathStringBuilder stop(String stop) {
return start().append(stop).end();
}
public PathStringBuilder walk(int duration) {
return start().append("Walk").duration(duration).end();
}
public PathStringBuilder flex(int duration, int nRides) {
// The 'tx' is short for eXtra Transfers added by the flex access/egress.
return start().append("Flex").duration(duration).space().append(nRides).append("x").end();
}
public PathStringBuilder accessEgress(RaptorTransfer leg) {
if(leg.hasRides()) {
return flex(leg.durationInSeconds(), leg.numberOfRides());
}
return leg.durationInSeconds() == 0 ? this : walk(leg.durationInSeconds());
}
public PathStringBuilder transit(String description, int fromTime, int toTime) {
return start().append(description).space().time(fromTime, toTime).end();
}
public PathStringBuilder transit(
TraverseMode mode, String trip, Calendar fromTime, Calendar toTime
) {
return start()
.append(mode.name()).space()
.append(trip).space()
.time(fromTime, toTime)
.end();
}
public PathStringBuilder other(TraverseMode mode, Calendar fromTime, Calendar toTime) {
return start().append(mode.name()).space().time(fromTime, toTime).end();
}
public PathStringBuilder timeAndCostCentiSec(int fromTime, int toTime, int generalizedCost) {
if(buf.length() != 0) { space(); }
return time(fromTime, toTime).generalizedCostSentiSec(generalizedCost);
}
/** Add generalizedCostCentiSec {@link #costCentiSec(int, int, String)} */
public PathStringBuilder generalizedCostSentiSec(int cost) {
return costCentiSec(cost, CostCalculator.ZERO_COST, null);
}
/**
* Add a cost to the string with an optional unit. Try to be consistent with unit naming,
* use lower-case:
*