Skip to content

Commit

Permalink
Adapt to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 26, 2024
1 parent dfe1f12 commit 47e49c1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.opentripplanner.routing.core.VehicleRoutingOptimizeType;
import org.opentripplanner.transit.model.basic.MainAndSubMode;
import org.opentripplanner.transit.model.basic.TransitMode;
import org.opentripplanner.transit.service.TransitService;

public class LegacyRouteRequestMapper {

Expand All @@ -57,7 +56,7 @@ public static RouteRequest toRouteRequest(
callWith.argument("from", (Map<String, Object> v) -> request.setFrom(toGenericLocation(v)));
callWith.argument("to", (Map<String, Object> v) -> request.setTo(toGenericLocation(v)));

mapViaPoints(request, callWith, context.transitService());
mapViaPoints(request, callWith);

request.setDateTime(
environment.getArgument("date"),
Expand Down Expand Up @@ -260,15 +259,11 @@ public static RouteRequest toRouteRequest(
return request;
}

static void mapViaPoints(
RouteRequest request,
CallerWithEnvironment callWith,
TransitService transitService
) {
static void mapViaPoints(RouteRequest request, CallerWithEnvironment callWith) {
callWith.argument(
"via",
(List<Map<String, Object>> v) ->
request.setPassThroughPoints(PassThroughLocationMapper.toLocations(transitService, v))
request.setViaLocations(ViaLocationMapper.mapToViaLocations(v))
);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.opentripplanner.apis.gtfs.mapping.routerequest;

import static java.util.stream.Collectors.toList;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.apis.transmodel.model.plan.TripQuery;
import org.opentripplanner.apis.transmodel.model.plan.ViaLocationInputType;
import org.opentripplanner.apis.transmodel.support.OneOfInputValidator;
import org.opentripplanner.routing.api.request.via.PassThroughViaLocation;
import org.opentripplanner.routing.api.request.via.ViaLocation;
import org.opentripplanner.routing.api.request.via.VisitViaLocation;
import org.opentripplanner.transit.model.framework.FeedScopedId;

class ViaLocationMapper {

static List<ViaLocation> mapToViaLocations(final List<Map<String, Object>> via) {
return via.stream().map(ViaLocationMapper::mapViaLocation).collect(toList());
}

private static ViaLocation mapViaLocation(Map<String, Object> inputMap) {
var fieldName = OneOfInputValidator.validateOneOf(
inputMap,
TripQuery.FIELD_VIA,
ViaLocationInputType.FIELD_VISIT,
ViaLocationInputType.FIELD_PASS_THROUGH
);

Map<String, Object> value = (Map<String, Object>) inputMap.get(fieldName);

return switch (fieldName) {
case ViaLocationInputType.FIELD_VISIT -> mapVisitViaLocation(value);
case ViaLocationInputType.FIELD_PASS_THROUGH -> mapPassThroughViaLocation(value);
default -> throw new IllegalArgumentException("Unknown field: " + fieldName);
};
}

private static VisitViaLocation mapVisitViaLocation(Map<String, Object> inputMap) {
var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL);
var minimumWaitTime = (Duration) inputMap.get(ViaLocationInputType.FIELD_MINIMUM_WAIT_TIME);
var stopLocationIds = mapStopLocationIds(inputMap);
return new VisitViaLocation(label, minimumWaitTime, stopLocationIds, List.of());
}

private static PassThroughViaLocation mapPassThroughViaLocation(Map<String, Object> inputMap) {
var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL);
var stopLocationIds = mapStopLocationIds(inputMap);
return new PassThroughViaLocation(label, stopLocationIds);
}

private static List<FeedScopedId> mapStopLocationIds(Map<String, Object> map) {
var c = (Collection<String>) map.get(ViaLocationInputType.FIELD_STOP_LOCATION_IDS);
return c.stream().map(TransitIdMapper::mapIDToDomain).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,22 @@ void transferSlack() {
void passThroughPoints() {
Map<String, Object> arguments = Map.of(
"via",
List.of(Map.of("passThrough", Map.of("stopLocationIds", List.of("F:stop1"))))
List.of(
Map.of("passThrough", Map.of("stopLocationIds", List.of("F:stop1"), "label", "a label"))
)
);

var routeRequest = LegacyRouteRequestMapper.toRouteRequest(
executionContext(arguments),
context
);
assertEquals(
"[PassThroughPoint[stopLocations=[RegularStop{F:stop1 stop1}], name=null]]",
routeRequest.getPassThroughPoints().toString()
"[PassThroughViaLocation{label: a label, stopLocationIds: [F:stop1]}]",
routeRequest.getViaLocations().toString()
);

var noParamsReq = LegacyRouteRequestMapper.toRouteRequest(executionContext(Map.of()), context);
assertEquals(List.of(), noParamsReq.getPassThroughPoints());
assertEquals(List.of(), noParamsReq.getViaLocations());
}

private DataFetchingEnvironment executionContext(Map<String, Object> arguments) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CollectionUtilsTest {

@Test
void testIsEmpty() {
assertTrue(CollectionUtils.isEmpty(null));
assertTrue(CollectionUtils.isEmpty((List) null));
assertTrue(CollectionUtils.isEmpty(List.of()));
assertFalse(CollectionUtils.isEmpty(List.of(1)));
}
Expand Down

0 comments on commit 47e49c1

Please sign in to comment.