org.opentripplanner.routing.algorithm.filterchain.deletionflagger.TransitGeneralizedCostFilter Maven / Gradle / Ivy
Show all versions of otp Show documentation
package org.opentripplanner.routing.algorithm.filterchain.deletionflagger;
import java.util.stream.Collectors;
import org.opentripplanner.model.plan.Itinerary;
import java.util.List;
import java.util.OptionalDouble;
import java.util.function.DoubleFunction;
/**
* This filter remove all transit results which have a generalized-cost higher than
* the max-limit computed by the {@link #costLimitFunction}.
*
* @see org.opentripplanner.routing.api.request.ItineraryFilterParameters#transitGeneralizedCostLimit
*/
public class TransitGeneralizedCostFilter implements ItineraryDeletionFlagger {
private final DoubleFunction costLimitFunction;
public TransitGeneralizedCostFilter(DoubleFunction costLimitFunction) {
this.costLimitFunction = costLimitFunction;
}
@Override
public String name() {
return "transit-cost-filter";
}
@Override
public List getFlaggedItineraries(List itineraries) {
OptionalDouble minGeneralizedCost = itineraries
.stream()
.filter(Itinerary::hasTransit)
.mapToDouble(it -> it.generalizedCost)
.min();
if(minGeneralizedCost.isEmpty()) { return List.of(); }
final double maxLimit = costLimitFunction.apply(minGeneralizedCost.getAsDouble());
return itineraries.stream()
.filter( it -> it.hasTransit() && it.generalizedCost > maxLimit)
.collect(Collectors.toList());
}
}