Skip to content

Commit

Permalink
Addresses comments in PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
eibakke committed Sep 5, 2024
1 parent 6e2f083 commit 153fe56
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.apis.transmodel.model.EnumTypes;
import org.opentripplanner.apis.transmodel.support.GqlUtil;
import org.opentripplanner.framework.collection.CollectionUtils;
import org.opentripplanner.transit.api.request.TripOnServiceDateRequest;
import org.opentripplanner.transit.api.request.TripOnServiceDateRequestBuilder;
import org.opentripplanner.transit.model.framework.FeedScopedId;
Expand Down Expand Up @@ -94,6 +93,9 @@ public static GraphQLFieldDefinition createQuery(
.type(new GraphQLList(new GraphQLNonNull(Scalars.GraphQLString)))
)
.dataFetcher(environment -> {
// The null safety checks are not needed here - they are taken care of by the request
// object, but reuse let's use the mapping method and leave this improvement until all APIs
// are pushing this check into the domain request.
var authorities = mapIDsToDomainNullSafe(environment.getArgument("authorities"));
var lines = mapIDsToDomainNullSafe(environment.getArgument("lines"));
var serviceJourneys = mapIDsToDomainNullSafe(environment.getArgument("serviceJourneys"));
Expand All @@ -102,10 +104,6 @@ public static GraphQLFieldDefinition createQuery(
var operatingDays = environment.<List<LocalDate>>getArgument("operatingDays");
var alterations = environment.<List<TripAlteration>>getArgument("alterations");

if (CollectionUtils.isEmpty(operatingDays)) {
throw new IllegalArgumentException("At least one operatingDay must be provided.");
}

TripOnServiceDateRequestBuilder tripOnServiceDateRequestBuilder = TripOnServiceDateRequest
.of()
.withOperatingDays(operatingDays)
Expand All @@ -114,15 +112,11 @@ public static GraphQLFieldDefinition createQuery(
.withServiceJourneys(serviceJourneys)
.withReplacementFor(replacementFor);

if (!CollectionUtils.isEmpty(privateCodes)) {
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withPrivateCodes(privateCodes);
}
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withPrivateCodes(privateCodes);

if (!CollectionUtils.isEmpty(alterations)) {
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withAlterations(alterations);
}
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withAlterations(alterations);

return GqlUtil
.getTransitService(environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ public static <T> List<T> ofNullable(T input) {
return List.of(input);
}
}

/**
* This method converts the given collection to an instance of a List. If the input is
* {@code null} an empty collection is returned. If not the {@link List#copyOf(Collection)} is
* called.
*/
public static <T> List<T> nullSafeImmutableList(Collection<T> c) {
return (c == null) ? List.of() : List.copyOf(c);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDate;
import java.util.List;
import org.opentripplanner.framework.collection.ListUtils;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.TripAlteration;

Expand Down Expand Up @@ -33,13 +34,13 @@ protected TripOnServiceDateRequest(
if (operatingDays == null || operatingDays.isEmpty()) {
throw new IllegalArgumentException("operatingDays must have at least one date");
}
this.operatingDays = List.copyOf(operatingDays);
this.authorities = List.copyOf(authorities);
this.lines = List.copyOf(lines);
this.serviceJourneys = List.copyOf(serviceJourneys);
this.replacementFor = List.copyOf(replacementFor);
this.privateCodes = List.copyOf(privateCodes);
this.alterations = List.copyOf(alterations);
this.operatingDays = ListUtils.nullSafeImmutableList(operatingDays);
this.authorities = ListUtils.nullSafeImmutableList(authorities);
this.lines = ListUtils.nullSafeImmutableList(lines);
this.serviceJourneys = ListUtils.nullSafeImmutableList(serviceJourneys);
this.replacementFor = ListUtils.nullSafeImmutableList(replacementFor);
this.privateCodes = ListUtils.nullSafeImmutableList(privateCodes);
this.alterations = ListUtils.nullSafeImmutableList(alterations);
}

public static TripOnServiceDateRequestBuilder of() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

public class TripOnServiceDateRequestBuilder {

private List<FeedScopedId> authorities = List.of();
private List<FeedScopedId> lines = List.of();
private List<FeedScopedId> serviceJourneys = List.of();
private List<FeedScopedId> replacementFor = List.of();
private List<String> privateCodes = List.of();
private List<TripAlteration> alterations = List.of();
private List<FeedScopedId> authorities;
private List<FeedScopedId> lines;
private List<FeedScopedId> serviceJourneys;
private List<FeedScopedId> replacementFor;
private List<String> privateCodes;
private List<TripAlteration> alterations;
private List<LocalDate> operatingDays;

protected TripOnServiceDateRequestBuilder() {}
Expand Down

0 comments on commit 153fe56

Please sign in to comment.