Skip to content
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 @@ -10,13 +10,13 @@
import graphql.schema.GraphQLOutputType;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Stream;
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.apis.transmodel.model.EnumTypes;
import org.opentripplanner.apis.transmodel.support.GqlUtil;
import org.opentripplanner.transit.api.request.TripOnServiceDateRequest;
import org.opentripplanner.transit.api.request.TripOnServiceDateRequestBuilder;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.TripAlteration;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;

/**
* A GraphQL query for retrieving data on DatedServiceJourneys
Expand Down Expand Up @@ -93,72 +93,34 @@ public static GraphQLFieldDefinition createQuery(
.type(new GraphQLList(new GraphQLNonNull(Scalars.GraphQLString)))
)
.dataFetcher(environment -> {
Stream<TripOnServiceDate> stream = GqlUtil
.getTransitService(environment)
.getAllTripOnServiceDates()
.stream();

// 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"));
var replacementFor = mapIDsToDomainNullSafe(environment.getArgument("replacementFor"));
var privateCodes = environment.<List<String>>getArgument("privateCodes");
var operatingDays = environment.<List<LocalDate>>getArgument("operatingDays");
var alterations = environment.<List<TripAlteration>>getArgument("alterations");
var authorities = mapIDsToDomainNullSafe(environment.getArgument("authorities"));
var replacementFor = mapIDsToDomainNullSafe(environment.getArgument("replacementFor"));

if (!lines.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
lines.contains(tripOnServiceDate.getTrip().getRoute().getId())
);
}
TripOnServiceDateRequestBuilder tripOnServiceDateRequestBuilder = TripOnServiceDateRequest
.of()
.withOperatingDays(operatingDays)
.withAuthorities(authorities)
.withLines(lines)
.withServiceJourneys(serviceJourneys)
.withReplacementFor(replacementFor);

if (!serviceJourneys.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
serviceJourneys.contains(tripOnServiceDate.getTrip().getId())
);
}
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withPrivateCodes(privateCodes);

if (privateCodes != null && !privateCodes.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
privateCodes.contains(tripOnServiceDate.getTrip().getNetexInternalPlanningCode())
);
}
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withAlterations(alterations);

// At least one operationg day is required
var days = operatingDays.stream().toList();

stream =
stream.filter(tripOnServiceDate -> days.contains(tripOnServiceDate.getServiceDate()));

if (alterations != null && !alterations.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
alterations.contains(tripOnServiceDate.getTripAlteration())
);
}

if (!authorities.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
authorities.contains(tripOnServiceDate.getTrip().getRoute().getAgency().getId())
);
}

if (!replacementFor.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
!tripOnServiceDate.getReplacementFor().isEmpty() &&
tripOnServiceDate
.getReplacementFor()
.stream()
.anyMatch(replacement -> replacementFor.contains(replacement.getId()))
);
}

return stream.toList();
return GqlUtil
.getTransitService(environment)
.getTripOnServiceDates(tripOnServiceDateRequestBuilder.build());
})
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static <T> String toString(@Nullable Collection<T> c, String nullText) {
/**
* A null-safe version of isEmpty() for a collection.
* <p>
* The main strategy handling collections in OTP is to avoid nullable collection fields and use empty
* collections instead. So, before using this method check if the variable/field is indeed `@Nullable`.
* <p>
* If the collection is {@code null} then {@code true} is returned.
* <p>
* If the collection is empty then {@code true} is returned.
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
@@ -0,0 +1,77 @@
package org.opentripplanner.transit.api.request;

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;

/*
* A request for trips on a specific service date.
*
* This request is used to retrieve TripsOnServiceDates that match the provided criteria.
* At least one operatingDay must be provided.
*/
public class TripOnServiceDateRequest {

private final List<LocalDate> operatingDays;
private final List<FeedScopedId> authorities;
private final List<FeedScopedId> lines;
private final List<FeedScopedId> serviceJourneys;
private final List<FeedScopedId> replacementFor;
private final List<String> privateCodes;
private final List<TripAlteration> alterations;

protected TripOnServiceDateRequest(
List<LocalDate> operatingDays,
List<FeedScopedId> authorities,
List<FeedScopedId> lines,
List<FeedScopedId> serviceJourneys,
List<FeedScopedId> replacementFor,
List<String> privateCodes,
List<TripAlteration> alterations
) {
if (operatingDays == null || operatingDays.isEmpty()) {
throw new IllegalArgumentException("operatingDays must have at least one date");
}
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() {
return new TripOnServiceDateRequestBuilder();
}

public List<FeedScopedId> authorities() {
return authorities;
}

public List<FeedScopedId> lines() {
return lines;
}

public List<FeedScopedId> serviceJourneys() {
return serviceJourneys;
}

public List<FeedScopedId> replacementFor() {
return replacementFor;
}

public List<String> privateCodes() {
return privateCodes;
}

public List<TripAlteration> alterations() {
return alterations;
}

public List<LocalDate> operatingDays() {
return operatingDays;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.opentripplanner.transit.api.request;

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

public class TripOnServiceDateRequestBuilder {

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() {}

public TripOnServiceDateRequestBuilder withOperatingDays(List<LocalDate> operatingDays) {
this.operatingDays = operatingDays;
return this;
}

public TripOnServiceDateRequestBuilder withAuthorities(List<FeedScopedId> authorities) {
this.authorities = authorities;
return this;
}

public TripOnServiceDateRequestBuilder withLines(List<FeedScopedId> lines) {
this.lines = lines;
return this;
}

public TripOnServiceDateRequestBuilder withServiceJourneys(List<FeedScopedId> serviceJourneys) {
this.serviceJourneys = serviceJourneys;
return this;
}

public TripOnServiceDateRequestBuilder withReplacementFor(List<FeedScopedId> replacementFor) {
this.replacementFor = replacementFor;
return this;
}

public TripOnServiceDateRequestBuilder withPrivateCodes(List<String> privateCodes) {
this.privateCodes = privateCodes;
return this;
}

public TripOnServiceDateRequestBuilder withAlterations(List<TripAlteration> alterations) {
this.alterations = alterations;
return this;
}

public TripOnServiceDateRequest build() {
return new TripOnServiceDateRequest(
operatingDays,
authorities,
lines,
serviceJourneys,
replacementFor,
privateCodes,
alterations
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.opentripplanner.transit.model.filter.expr;

import static org.opentripplanner.transit.model.filter.expr.BinaryOperator.AND;

import java.util.List;

/**
* Takes a list of matchers and provides a single interface. All matchers in the list must match for
* the composite matcher to return a match.
*
* @param <T> The entity type the AndMatcher matches.
*/
public final class AndMatcher<T> implements Matcher<T> {

private final Matcher<T>[] matchers;

private AndMatcher(List<Matcher<T>> matchers) {
this.matchers = matchers.toArray(Matcher[]::new);
}

public static <T> Matcher<T> of(List<Matcher<T>> matchers) {
// simplify a list of one element
if (matchers.size() == 1) {
return matchers.get(0);
}
return new AndMatcher<>(matchers);
}

@Override
public boolean match(T entity) {
for (var m : matchers) {
if (!m.match(entity)) {
return false;
}
}
return true;
}

@Override
public String toString() {
return "(" + AND.arrayToString(matchers) + ')';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.opentripplanner.transit.model.filter.expr;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Used to concatenate matches with either the logical "AND" or "OR" operator.
*/
enum BinaryOperator {
AND("&"),
OR("|");

private final String token;

BinaryOperator(String token) {
this.token = token;
}

@Override
public String toString() {
return token;
}

<T> String arrayToString(T[] values) {
return colToString(Arrays.asList(values));
}

<T> String colToString(Collection<T> values) {
return values.stream().map(Objects::toString).collect(Collectors.joining(" " + token + " "));
}

<T> String toString(T a, T b) {
return a.toString() + " " + token + " " + b.toString();
}
}
Loading