forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a matcher API for filters in the transit service used for service…
…Journey lookup (opentripplanner#6207) * Removes some unused functions from TransitService and TripOnServiceDate * Adds filtering logic to the serviceJourneys Transmodel GraphQL query. * Fixes a typo that carried over from a documentation copy. * Resolving merge conflict. * Addresses comments in review - updates naming to reflect clear division between netex and OTP internals. * Applies formatting. * Fixes formatting and some residual merge issues. * Addresses comments in PR. Importantly this creates the CriteriaCollection type which allows for checking features about collections of criteria independently of the collection type. * Applies results from prettier to CriteriaCollectionTest. * Addresses comments from review. Adds formatting to API and md documentation, and adds an exception thrown for invalid calls to values in CriteriaCollection. * Renames methods in CriteriaCollection as discussed in code review. * Addresses comments from code review. * Changes name of CriteriaCollection to FilterValueCollection. * Adds documentation to clarify usage and intent of the FilterValueCollection. * Fixes potential null values in request builders and exception throwing in get() method. * Addresses comments from code review. * Changes name of FilterValueCollection to FilterValues. * Creates the RequiredFilterValues and FilterValuesEmptyIsEverything subclasses of the now abstract FilterValues class. * Apply suggestions from code review Co-authored-by: Thomas Gran <[email protected]> * Addresses comments from code review. * Makes a method name change. * asserts that exception strings in the RequiredFilterValuesTest are as expected. * Fixes some issues left over from resolving merge conflicts. --------- Co-authored-by: Thomas Gran <[email protected]>
- Loading branch information
Showing
22 changed files
with
783 additions
and
184 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
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
86 changes: 86 additions & 0 deletions
86
application/src/main/java/org/opentripplanner/transit/api/model/FilterValues.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,86 @@ | ||
package org.opentripplanner.transit.api.model; | ||
|
||
import com.beust.jcommander.internal.Nullable; | ||
import java.util.Collection; | ||
import java.util.NoSuchElementException; | ||
import org.opentripplanner.transit.model.framework.FeedScopedId; | ||
import org.opentripplanner.transit.service.TransitService; | ||
|
||
/** | ||
* {@link FilterValues} is meant to be used when filtering results from {@link TransitService}. | ||
* </p> | ||
* This abstraction over the Collection type lets us keep filter specific functionality separate | ||
* from interpretation of various states of a collection. For instance in which case the filter values | ||
* should match all entities they are meant to filter. | ||
* </p> | ||
* @param <E> - The type of the filter values. Typically, String or {@link FeedScopedId}. | ||
*/ | ||
public abstract class FilterValues<E> { | ||
|
||
@Nullable | ||
protected final Collection<E> values; | ||
|
||
private final String name; | ||
|
||
FilterValues(String name, @Nullable Collection<E> values) { | ||
this.name = name; | ||
this.values = values; | ||
} | ||
|
||
/** | ||
* Returns a {@link FilterValues} that matches everything if there are no filter values. | ||
* </p> | ||
* @param name - The name of the filter. | ||
* @param <E> - The type of the filter values. Typically, String or {@link FeedScopedId}. | ||
* @param values - The {@link Collection} of filter values. | ||
* @return FilterValues | ||
*/ | ||
public static <E> FilterValues<E> ofEmptyIsEverything( | ||
String name, | ||
@Nullable Collection<E> values | ||
) { | ||
return new FilterValuesEmptyIsEverything<>(name, values); | ||
} | ||
|
||
/** | ||
* Returns a {@link RequiredFilterValues} that throws an exception at creation time if the filter | ||
* values is null or empty. | ||
* </p> | ||
* @param name - The name of the filter. | ||
* @param <E> - The type of the filter values. Typically, String or {@link FeedScopedId}. | ||
* @param values - The {@link Collection} of filter values. | ||
* @return RequiredFilterValues | ||
*/ | ||
public static <E> RequiredFilterValues<E> ofRequired( | ||
String name, | ||
@Nullable Collection<E> values | ||
) { | ||
return new RequiredFilterValues<>(name, values); | ||
} | ||
|
||
/** | ||
* Returns True if the collection of filter values matches everything that it could filter. If this | ||
* is the case, then the filter values should not be used to filter anything and filtering logic can | ||
* safely ignore it. | ||
* </p> | ||
* @return boolean | ||
*/ | ||
public abstract boolean includeEverything(); | ||
|
||
/** | ||
* Returns the collection of filter values. If the filter values effectively don't filter anything, | ||
* an exception is thrown. | ||
* </p> | ||
* @return Collection<E> - The values of the filter. | ||
*/ | ||
public Collection<E> get() { | ||
if (includeEverything()) { | ||
throw new NoSuchElementException( | ||
"Filter values for filter %s effectively don't filter, use includeEverything() before calling this method.".formatted( | ||
name | ||
) | ||
); | ||
} | ||
return values; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...on/src/main/java/org/opentripplanner/transit/api/model/FilterValuesEmptyIsEverything.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,19 @@ | ||
package org.opentripplanner.transit.api.model; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* {@link FilterValuesEmptyIsEverything} is a subclass of {@link FilterValues} that includes | ||
* everything if the values are null or empty. | ||
*/ | ||
public class FilterValuesEmptyIsEverything<E> extends FilterValues<E> { | ||
|
||
FilterValuesEmptyIsEverything(String name, Collection<E> values) { | ||
super(name, values); | ||
} | ||
|
||
@Override | ||
public boolean includeEverything() { | ||
return values == null || values.isEmpty(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
application/src/main/java/org/opentripplanner/transit/api/model/RequiredFilterValues.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,24 @@ | ||
package org.opentripplanner.transit.api.model; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* {@link RequiredFilterValues} is a subclass of {@link FilterValues} that requires at least one | ||
* value to be included. | ||
*/ | ||
public class RequiredFilterValues<E> extends FilterValues<E> { | ||
|
||
RequiredFilterValues(String name, Collection<E> values) { | ||
super(name, values); | ||
if (values == null || values.isEmpty()) { | ||
throw new IllegalArgumentException("Filter %s values must not be empty.".formatted(name)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean includeEverything() { | ||
// RequiredFilterValues should never include everything. In theory the filter values could be | ||
// exhaustive, but there is no check for that currently. | ||
return false; | ||
} | ||
} |
Oops, something went wrong.