Skip to content

Commit

Permalink
Make factors serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Apr 4, 2024
1 parent 19ff53f commit f52d18f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class FlexTripsMapper {
for (Trip trip : stopTimesByTrip.keys()) {
/* Fetch the stop times for this trip. Copy the list since it's immutable. */
List<StopTime> stopTimes = new ArrayList<>(stopTimesByTrip.get(trip));
var factors = builder.getFlexDurationFactors().getOrDefault(trip, FlexDurationFactors.ZERO);

if (UnscheduledTrip.isUnscheduledTrip(stopTimes)) {
var factors = builder.getFlexDurationFactors().getOrDefault(trip, FlexDurationFactors.ZERO);
result.add(
UnscheduledTrip
.of(trip.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.opentripplanner.ext.flex.trip;

import java.io.Serializable;
import java.time.Duration;
import org.opentripplanner.framework.time.DurationUtils;
import org.opentripplanner.framework.tostring.ToStringBuilder;

public class FlexDurationFactors {
public class FlexDurationFactors implements Serializable {

public static FlexDurationFactors ZERO = new FlexDurationFactors(Duration.ZERO, 1);
private final int offset;
private final float factor;

public FlexDurationFactors(Duration offset, float factor) {
this.offset = (int) offset.toSeconds();
if (factor < 0.1) {
throw new IllegalArgumentException("Flex duration factor must not be less than 0.1");
}
this.offset = (int) DurationUtils.requireNonNegative(offset).toSeconds();
this.factor = factor;
}

Expand All @@ -24,4 +30,13 @@ public int offsetInSeconds() {
boolean nonZero() {
return offset != 0 && factor != 1.0;
}

@Override
public String toString() {
return ToStringBuilder
.of(FlexDurationFactors.class)
.addNum("factor", factor)
.addDurationSec("offset", offset)
.toString();
}
}

0 comments on commit f52d18f

Please sign in to comment.