This repository was archived by the owner on Mar 3, 2025. It is now read-only.
forked from HSLdevcom/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a matcher API for the transit service and makes use of it in the…
… DatedServiceJourneyQuery. This is the first simple implementation of a filter using the unified matcher API.
- Loading branch information
Showing
17 changed files
with
826 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
73 changes: 73 additions & 0 deletions
73
src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequest.java
This file contains 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,73 @@ | ||
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; | ||
|
||
/* | ||
* 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<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; | ||
private final List<LocalDate> operatingDays; | ||
|
||
protected TripOnServiceDateRequest( | ||
List<FeedScopedId> authorities, | ||
List<FeedScopedId> lines, | ||
List<FeedScopedId> serviceJourneys, | ||
List<FeedScopedId> replacementFor, | ||
List<String> privateCodes, | ||
List<LocalDate> operatingDays, | ||
List<TripAlteration> alterations | ||
) { | ||
this.authorities = authorities; | ||
this.lines = lines; | ||
this.serviceJourneys = serviceJourneys; | ||
this.replacementFor = replacementFor; | ||
this.privateCodes = privateCodes; | ||
this.alterations = alterations; | ||
this.operatingDays = operatingDays; | ||
} | ||
|
||
public static TripOnServiceDateRequestBuilder of(List<LocalDate> operatingDays) { | ||
return new TripOnServiceDateRequestBuilder(operatingDays); | ||
} | ||
|
||
public List<FeedScopedId> getAuthorities() { | ||
return authorities; | ||
} | ||
|
||
public List<FeedScopedId> getLines() { | ||
return lines; | ||
} | ||
|
||
public List<FeedScopedId> getServiceJourneys() { | ||
return serviceJourneys; | ||
} | ||
|
||
public List<FeedScopedId> getReplacementFor() { | ||
return replacementFor; | ||
} | ||
|
||
public List<String> getPrivateCodes() { | ||
return privateCodes; | ||
} | ||
|
||
public List<TripAlteration> getAlterations() { | ||
return alterations; | ||
} | ||
|
||
public List<LocalDate> getOperatingDays() { | ||
return operatingDays; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/org/opentripplanner/transit/api/request/TripOnServiceDateRequestBuilder.java
This file contains 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 = 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 final List<LocalDate> operatingDays; | ||
|
||
protected TripOnServiceDateRequestBuilder(List<LocalDate> operatingDays) { | ||
if (operatingDays == null || operatingDays.isEmpty()) { | ||
throw new IllegalArgumentException("operatingDays must have at least one date"); | ||
} | ||
this.operatingDays = operatingDays; | ||
} | ||
|
||
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( | ||
authorities, | ||
lines, | ||
serviceJourneys, | ||
replacementFor, | ||
privateCodes, | ||
operatingDays, | ||
alterations | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/opentripplanner/transit/model/filter/expr/AndMatcher.java
This file contains 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,41 @@ | ||
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. | ||
*/ | ||
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 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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/opentripplanner/transit/model/filter/expr/ContainsMatcher.java
This file contains 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,36 @@ | ||
package org.opentripplanner.transit.model.filter.expr; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A matcher that checks if a collection contains a value. | ||
* <p> | ||
* The collection is provided by a function that takes the entity being matched as an argument. | ||
*/ | ||
public class ContainsMatcher<T, V> implements Matcher<T> { | ||
|
||
private final String typeName; | ||
private final V value; | ||
private final Function<T, Collection<V>> valueProvider; | ||
|
||
public ContainsMatcher(String typeName, V value, Function<T, Collection<V>> valueProvider) { | ||
this.typeName = typeName; | ||
this.value = value; | ||
this.valueProvider = valueProvider; | ||
} | ||
|
||
@Override | ||
public boolean match(T entity) { | ||
Collection<V> values = valueProvider.apply(entity); | ||
if (values == null) { | ||
return false; | ||
} | ||
return values.contains(value); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return typeName + " contains " + value; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/opentripplanner/transit/model/filter/expr/EqualityMatcher.java
This file contains 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,31 @@ | ||
package org.opentripplanner.transit.model.filter.expr; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* A matcher that checks if a value is equal to another value. | ||
* <p> | ||
* The value is provided by a function that takes the entity being matched as an argument. | ||
*/ | ||
public class EqualityMatcher<T, V> implements Matcher<T> { | ||
|
||
private final String typeName; | ||
private final V value; | ||
private final Function<T, V> valueProvider; | ||
|
||
public EqualityMatcher(String typeName, V value, Function<T, V> valueProvider) { | ||
this.typeName = typeName; | ||
this.value = value; | ||
this.valueProvider = valueProvider; | ||
} | ||
|
||
@Override | ||
public boolean match(T entity) { | ||
return value.equals(valueProvider.apply(entity)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return typeName + "==" + value; | ||
} | ||
} |
Oops, something went wrong.