Skip to content

Commit f53b682

Browse files
Merge pull request opentripplanner#7331 from ibi-group/transfer-count
Ensure valid values for `FareLegRule#transferCount`
2 parents 4ed9460 + 06c92f2 commit f53b682

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

application/src/ext/java/org/opentripplanner/ext/fares/model/FareTransferRule.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.annotation.Nullable;
99
import org.opentripplanner.core.model.id.FeedScopedId;
1010
import org.opentripplanner.model.fare.FareProduct;
11+
import org.opentripplanner.utils.lang.IntUtils;
1112
import org.opentripplanner.utils.tostring.ToStringBuilder;
1213

1314
public final class FareTransferRule implements Serializable {
@@ -33,7 +34,11 @@ public final class FareTransferRule implements Serializable {
3334
this.fareProducts = List.copyOf(b.fareProducts());
3435
this.fromLegGroup = b.fromLegGroup();
3536
this.toLegGroup = b.toLegGroup();
36-
this.transferCount = b.transferCount();
37+
this.transferCount = IntUtils.requireInRange(
38+
b.transferCount(),
39+
UNLIMITED_TRANSFERS,
40+
Integer.MAX_VALUE
41+
);
3742
this.timeLimit = b.timeLimit();
3843
}
3944

@@ -49,7 +54,7 @@ public boolean containsWildCard() {
4954
}
5055

5156
/**
52-
* Returns true if there is no limit on the number of transfers.
57+
* Returns true if there is no limit on the number of transfers or if limit unknown.
5358
*/
5459
public boolean unlimitedTransfers() {
5560
return transferCount == UNLIMITED_TRANSFERS;

application/src/main/java/org/opentripplanner/gtfs/mapping/FareTransferRuleMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
class FareTransferRuleMapper {
1414

15-
public final int MISSING_VALUE = -999;
16-
1715
private final IdFactory idFactory;
1816
private final FareProductMapper fareProductMapper;
1917

@@ -36,9 +34,11 @@ private FareTransferRule doMap(org.onebusaway.gtfs.model.FareTransferRule rhs) {
3634
.withId(idFactory.createId(rhs.getId(), "fare transfer rule"))
3735
.withFromLegGroup(idFactory.createNullableId(rhs.getFromLegGroupId()))
3836
.withToLegGroup(idFactory.createNullableId(rhs.getToLegGroupId()))
39-
.withTransferCount(rhs.getTransferCount())
4037
.withFareProducts(products);
41-
if (rhs.getDurationLimit() != MISSING_VALUE) {
38+
if (rhs.isTransferCountSet()) {
39+
builder.withTransferCount(rhs.getTransferCount());
40+
}
41+
if (rhs.isDurationLimitSet()) {
4242
var duration = Duration.ofSeconds(rhs.getDurationLimit());
4343
var type = mapLimitType(rhs.getDurationLimitType());
4444
builder.withTimeLimit(type, duration);

application/src/test/java/org/opentripplanner/gtfs/mapping/FareTransferRuleMapperTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,48 @@ void ruleWithoutProductIsFree() {
9999
assertTrue(transferRule.isFree());
100100
}
101101

102+
@Test
103+
void defaultTransferCount() {
104+
var rule = new FareTransferRule();
105+
rule.setFromLegGroupId(groupId1);
106+
rule.setToLegGroupId(groupId2);
107+
108+
var transferRule = map(fareProduct(), rule);
109+
assertTrue(transferRule.unlimitedTransfers());
110+
}
111+
112+
@Test
113+
void explicitUnlimitedTransfer() {
114+
var rule = new FareTransferRule();
115+
rule.setFromLegGroupId(groupId1);
116+
rule.setToLegGroupId(groupId2);
117+
rule.setTransferCount(-1);
118+
119+
var transferRule = map(fareProduct(), rule);
120+
assertTrue(transferRule.unlimitedTransfers());
121+
}
122+
123+
@Test
124+
void transferCount() {
125+
var rule = new FareTransferRule();
126+
rule.setFromLegGroupId(groupId1);
127+
rule.setToLegGroupId(groupId2);
128+
rule.setTransferCount(2);
129+
130+
var transferRule = map(fareProduct(), rule);
131+
assertFalse(transferRule.unlimitedTransfers());
132+
}
133+
134+
@Test
135+
void invalidTransferCount() {
136+
var rule = new FareTransferRule();
137+
rule.setFromLegGroupId(groupId1);
138+
rule.setToLegGroupId(groupId2);
139+
rule.setTransferCount(-2);
140+
141+
assertThrows(IllegalArgumentException.class, () -> map(fareProduct(), rule));
142+
}
143+
102144
private FareProduct fareProduct() {
103145
var fareProduct = new FareProduct();
104146
fareProduct.setId(id);

0 commit comments

Comments
 (0)