Skip to content

Commit

Permalink
Merge branch 'realtime-vehicle-pattern' into test-during-holiday
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Aug 9, 2024
2 parents 1f6ba7c + e81aa53 commit 46b1f4a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository;
Expand All @@ -33,8 +32,16 @@ public DefaultRealtimeVehicleService(TransitService transitService) {
this.transitService = transitService;
}

/**
* Stores the relationship between a list of realtime vehicles with a pattern. If the pattern is
* a realtime-added one, then the original (scheduled) one is used as the key for the map storing
* the information.
*/
@Override
public void setRealtimeVehicles(TripPattern pattern, List<RealtimeVehicle> updates) {
if (pattern.getOriginalTripPattern() != null) {
pattern = pattern.getOriginalTripPattern();
}
vehicles.put(pattern, List.copyOf(updates));
}

Expand All @@ -43,8 +50,18 @@ public void clearRealtimeVehicles(TripPattern pattern) {
vehicles.remove(pattern);
}

/**
* Gets the realtime vehicles for a given pattern. If the pattern is a realtime-added one
* then the original (scheduled) one is used for the lookup instead, so you receive the correct
* result no matter if you use the realtime or static information.
*
* @see DefaultRealtimeVehicleService#setRealtimeVehicles(TripPattern, List)
*/
@Override
public List<RealtimeVehicle> getRealtimeVehicles(@Nonnull TripPattern pattern) {
if (pattern.getOriginalTripPattern() != null) {
pattern = pattern.getOriginalTripPattern();
}
// the list is made immutable during insertion, so we can safely return them
return vehicles.getOrDefault(pattern, List.of());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.opentripplanner.service.realtimevehicles.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.framework.geometry.WgsCoordinate.GREENWICH;
import static org.opentripplanner.transit.model._data.TransitModelForTest.route;
import static org.opentripplanner.transit.model._data.TransitModelForTest.tripPattern;

import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.service.realtimevehicles.model.RealtimeVehicle;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;

class DefaultRealtimeVehicleServiceTest {

private static final Route ROUTE = route("r1").build();
private static final TransitModelForTest MODEL = TransitModelForTest.of();
private static final StopPattern STOP_PATTERN = TransitModelForTest.stopPattern(
MODEL.stop("1").build(),
MODEL.stop("2").build()
);
private static final TripPattern ORIGINAL = tripPattern("original", ROUTE)
.withStopPattern(STOP_PATTERN)
.build();
private static final Instant TIME = Instant.ofEpochSecond(1000);
private static final List<RealtimeVehicle> VEHICLES = List.of(
RealtimeVehicle.builder().withTime(TIME).withCoordinates(GREENWICH).build()
);

@Test
void originalPattern() {
var service = new DefaultRealtimeVehicleService(new DefaultTransitService(new TransitModel()));
service.setRealtimeVehicles(ORIGINAL, VEHICLES);
var updates = service.getRealtimeVehicles(ORIGINAL);
assertEquals(VEHICLES, updates);
}

@Test
void realtimeAddedPattern() {
var service = new DefaultRealtimeVehicleService(new DefaultTransitService(new TransitModel()));
var realtimePattern = tripPattern("realtime-added", ROUTE)
.withStopPattern(STOP_PATTERN)
.withOriginalTripPattern(ORIGINAL)
.withCreatedByRealtimeUpdater(true)
.build();
service.setRealtimeVehicles(realtimePattern, VEHICLES);
var updates = service.getRealtimeVehicles(ORIGINAL);
assertEquals(VEHICLES, updates);
}
}

0 comments on commit 46b1f4a

Please sign in to comment.