Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve transfer times #3007

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
class MinimalTransferTimesImpl implements MinimalTransferTimes {

private Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> minimalTransferTimes = new ConcurrentHashMap<>();
private final Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> minimalTransferTimes = new ConcurrentHashMap<>();

@Override
public double set(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double seconds) {
Expand All @@ -59,15 +59,21 @@ public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toSt
public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double defaultSeconds) {
Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(fromStop);
if (innerMap == null) {
return defaultSeconds;
return getInnerStopTransferTime(toStop,defaultSeconds);
}
Double value = innerMap.get(toStop);
if (value == null) {
return defaultSeconds;
return Math.max(getInnerStopTransferTime(toStop,defaultSeconds),getInnerStopTransferTime(fromStop,defaultSeconds));
}
return value;
}

private double getInnerStopTransferTime(Id<TransitStopFacility> stopId, double defaultSeconds){
Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(stopId);
return innerMap!=null?innerMap.getOrDefault(stopId,defaultSeconds):defaultSeconds;

}

@Override
public double remove(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop) {
Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(fromStop);
Expand All @@ -94,7 +100,7 @@ private static class MinimalTransferTimesIteratorImpl implements MinimalTransfe
private double seconds = Double.NaN;
private boolean hasElement = false;

private Iterator<Map.Entry<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>>> outerIterator;
private final Iterator<Map.Entry<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>>> outerIterator;
private Iterator<Map.Entry<Id<TransitStopFacility>, Double>> innerIterator;

MinimalTransferTimesIteratorImpl(Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
*/
public class MinimalTransferTimesImplTest {

private Id<TransitStopFacility> stopId1 = Id.create(1, TransitStopFacility.class);
private Id<TransitStopFacility> stopId2 = Id.create(2, TransitStopFacility.class);
private Id<TransitStopFacility> stopId3 = Id.create(3, TransitStopFacility.class);
private Id<TransitStopFacility> stopId4 = Id.create(4, TransitStopFacility.class);
private Id<TransitStopFacility> stopId5 = Id.create(5, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId1 = Id.create(1, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId2 = Id.create(2, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId3 = Id.create(3, TransitStopFacility.class);
private final Id<TransitStopFacility> stopId4 = Id.create(4, TransitStopFacility.class);

@Test
void testSetGet() {
Expand All @@ -55,6 +54,19 @@ void testSetGet() {
Assertions.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId4), 0.0);
}

@Test
void testGetNonSetConnection() {
MinimalTransferTimes mtt = new MinimalTransferTimesImpl();
mtt.set(this.stopId1, this.stopId1, 300.0);
mtt.set(this.stopId3, this.stopId3, 240.0);

Assertions.assertEquals(300.0, mtt.get(this.stopId1, this.stopId1), 0.0);
Assertions.assertEquals(240.0, mtt.get(this.stopId3, this.stopId3), 0.0);

Assertions.assertEquals(300.0, mtt.get(this.stopId1, this.stopId3), 0.0);

}

@Test
void testGetWithDefault() {
MinimalTransferTimes mtt = new MinimalTransferTimesImpl();
Expand Down Expand Up @@ -259,4 +271,4 @@ void testIterator_withRemove() {
iter = mtt.iterator();
Assertions.assertFalse(iter.hasNext());
}
}
}
Loading