-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add a matcher API for filters in the transit service used for datedServiceJourneyQuery #5713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
98d53d8
Adds a matcher API for the transit service and makes use of it in the…
eibakke df0b4b2
Adds an expression builder for building up a list of matchers simply …
eibakke b782f55
Adds convenience function for null and empty check for collections an…
eibakke 0a0f475
Addresses minor comments in code review.
eibakke 6e2f083
Makes ContainsMatcher way more configurable and performant using sugg…
eibakke 153fe56
Addresses comments in PR.
eibakke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequestBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
eibakke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 | ||
| ); | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
src/main/java/org/opentripplanner/transit/model/filter/expr/AndMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) + ')'; | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/org/opentripplanner/transit/model/filter/expr/BinaryOperator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.