diff --git a/src/main/java/org/opentripplanner/apis/transmodel/model/timetable/DatedServiceJourneyQuery.java b/src/main/java/org/opentripplanner/apis/transmodel/model/timetable/DatedServiceJourneyQuery.java index c0e1657d689..e3fbf90a35d 100644 --- a/src/main/java/org/opentripplanner/apis/transmodel/model/timetable/DatedServiceJourneyQuery.java +++ b/src/main/java/org/opentripplanner/apis/transmodel/model/timetable/DatedServiceJourneyQuery.java @@ -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; @@ -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")); @@ -102,10 +104,6 @@ public static GraphQLFieldDefinition createQuery( var operatingDays = environment.>getArgument("operatingDays"); var alterations = environment.>getArgument("alterations"); - if (CollectionUtils.isEmpty(operatingDays)) { - throw new IllegalArgumentException("At least one operatingDay must be provided."); - } - TripOnServiceDateRequestBuilder tripOnServiceDateRequestBuilder = TripOnServiceDateRequest .of() .withOperatingDays(operatingDays) @@ -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) diff --git a/src/main/java/org/opentripplanner/framework/collection/ListUtils.java b/src/main/java/org/opentripplanner/framework/collection/ListUtils.java index 5964a1674e3..35b7e083695 100644 --- a/src/main/java/org/opentripplanner/framework/collection/ListUtils.java +++ b/src/main/java/org/opentripplanner/framework/collection/ListUtils.java @@ -69,4 +69,13 @@ public static List 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 List nullSafeImmutableList(Collection c) { + return (c == null) ? List.of() : List.copyOf(c); + } } diff --git a/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequest.java b/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequest.java index 551ee656ced..6735dc1db29 100644 --- a/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequest.java +++ b/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequest.java @@ -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; @@ -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() { diff --git a/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequestBuilder.java b/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequestBuilder.java index a1e2707b099..7aa2644fdc9 100644 --- a/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequestBuilder.java +++ b/src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequestBuilder.java @@ -7,12 +7,12 @@ public class TripOnServiceDateRequestBuilder { - private List authorities = List.of(); - private List lines = List.of(); - private List serviceJourneys = List.of(); - private List replacementFor = List.of(); - private List privateCodes = List.of(); - private List alterations = List.of(); + private List authorities; + private List lines; + private List serviceJourneys; + private List replacementFor; + private List privateCodes; + private List alterations; private List operatingDays; protected TripOnServiceDateRequestBuilder() {}