Skip to content

Commit

Permalink
Updates the RegularStopRequest to latest concepts.
Browse files Browse the repository at this point in the history
  • Loading branch information
eibakke committed Dec 3, 2024
1 parent 1e8b0f5 commit b104b5d
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ private GraphQLSchema create() {
RegularStopRequest regularStopRequest = RegularStopRequest
.of()
.withEnvelope(envelope)
.withFeeds(List.of(authority))
.withFeed(authority)
.filterByInUse(filterInUse)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

/**
* A request for {@link RegularStop}s.
*
* <p/>
* This request is used to retrieve {@link RegularStop}s that match the provided criteria.
*/
public class RegularStopRequest {

private final Envelope envelope;
private final List<String> feedIds;
private final String feedId;
private final boolean filterByInUse;

protected RegularStopRequest(Envelope envelope, List<String> feedIds, boolean filterByInUse) {
protected RegularStopRequest(Envelope envelope, String feedId, boolean filterByInUse) {
this.envelope = envelope;
this.feedIds = feedIds;
this.feedId = feedId;
this.filterByInUse = filterByInUse;
}

Expand All @@ -29,8 +29,8 @@ public Envelope envelope() {
return envelope;
}

public List<String> feedIds() {
return feedIds;
public String feedId() {
return feedId;
}

public boolean filterByInUse() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.opentripplanner.transit.api.request;

import java.util.List;
import org.locationtech.jts.geom.Envelope;

public class RegularStopRequestBuilder {

private Envelope envelope;
private List<String> feedIds;
private String feedId;
private boolean filterByInUse;

protected RegularStopRequestBuilder() {}
Expand All @@ -16,8 +15,8 @@ public RegularStopRequestBuilder withEnvelope(Envelope envelope) {
return this;
}

public RegularStopRequestBuilder withFeeds(List<String> feedIds) {
this.feedIds = feedIds;
public RegularStopRequestBuilder withFeed(String feedId) {
this.feedId = feedId;
return this;
}

Expand All @@ -27,6 +26,6 @@ public RegularStopRequestBuilder filterByInUse(boolean filterByInUse) {
}

public RegularStopRequest build() {
return new RegularStopRequest(envelope, feedIds, filterByInUse);
return new RegularStopRequest(envelope, feedId, filterByInUse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public static <T> ExpressionBuilder<T> of() {
return new ExpressionBuilder<>();
}

public ExpressionBuilder<T> matches(Matcher<T> matcher) {
matchers.add(matcher);
return this;
}

public <V> ExpressionBuilder<T> atLeastOneMatch(
FilterValues<V> filterValues,
Function<V, Matcher<T>> matcherProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,32 @@
*/
public class RegularStopMatcherFactory {

/**
* Creates a matcher that filters {@link RegularStop} objects with the provided {@link RegularStopRequest}
* and {@link Predicate}. The {@link Predicate} is used to determine if a {@link RegularStop} is
* in use. Typically the inUseProvider is a function that checks if the {@link RegularStop} is in
* a set of used stops.
* @param request - {@link RegularStopRequest} to filter {@link RegularStop} objects.
* @param inUseProvider - {@link Predicate} to determine if a {@link RegularStop} is in use.
* @return - {@link Matcher} for {@link RegularStop} objects.
*/
public static Matcher<RegularStop> of(
RegularStopRequest request,
Predicate<RegularStop> inUseProvider
) {
ExpressionBuilder<RegularStop> expr = ExpressionBuilder.of();

expr.atLeastOneMatch(request.feedIds(), RegularStopMatcherFactory::feedIds);
if (request.feedId() != null) {
expr.matches(feedId(request.feedId()));
}
if (request.filterByInUse()) {
expr.matches(inUseMatcher(inUseProvider));
}
return expr.build();
}

static Matcher<RegularStop> feedIds(String feedIds) {
return new EqualityMatcher<>("feedId", feedIds, stop -> stop.getId().getFeedId());
static Matcher<RegularStop> feedId(String feedId) {
return new EqualityMatcher<>("feedId", feedId, stop -> stop.getId().getFeedId());
}

static Matcher<RegularStop> inUseMatcher(Predicate<RegularStop> inUseProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public Collection<RegularStop> findRegularStops(RegularStopRequest request) {

Matcher<RegularStop> matcher = RegularStopMatcherFactory.of(
request,
stop -> !getPatternsForStop(stop, true).isEmpty()
stop -> !findPatterns(stop, true).isEmpty()
);
return stops.stream().filter(matcher::match).toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,4 @@ List<TripTimeOnDate> findTripTimeOnDate(
* @return - A list of {@link RegularStop}s
*/
Collection<RegularStop> findRegularStops(RegularStopRequest request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void setup() {

@Test
void testFeedIds() {
var matcher = RegularStopMatcherFactory.feedIds("feedId");
var matcher = RegularStopMatcherFactory.feedId("feedId");
assertTrue(matcher.match(stop1));
assertFalse(matcher.match(stop2));
}
Expand Down

0 comments on commit b104b5d

Please sign in to comment.