All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.routing.algorithm.filterchain.filters.OtpDefaultSortOrder Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.routing.algorithm.filterchain.filters;

import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.algorithm.filterchain.ItineraryFilter;
import org.opentripplanner.util.CompositeComparator;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

 /**
 * This filter will sort the itineraries in OTP default order according to the request
  * {@code arriveBy} flag.
 * 

* The SORT ORDER for a "depart-after-search" is: *

    *
  1. ON-STREET-ONLY before TRANSIT
  2. *
  3. Earliest arrival time first
  4. *
  5. Generalized cost, lowest first
  6. *
  7. Number of transfers, lowest first
  8. *
  9. Latest departure time first
  10. *
*

* The SORT ORDER for a "arrive-by-search" is: *

    *
  1. ON-STREET-ONLY before TRANSIT
  2. *
  3. Latest departure time first
  4. *
  5. Generalized cost, lowest cost first
  6. *
  7. Number of transfers, lowest first
  8. *
  9. Earliest arrival time first
  10. *
*

* The filter do only sort the itineraries, no other modifications are done. */ public class OtpDefaultSortOrder implements ItineraryFilter { /** * This comparator will sort all itineraries with STREET ONLY first. So, if there is an itinerary * with walking/bicycle/car from origin all the way to the destination, than it will be sorted * before any itineraries with one or more transit legs. */ static final Comparator STREET_ONLY_FIRST = (a, b) -> Boolean.compare(b.isOnStreetAllTheWay(), a.isOnStreetAllTheWay()); /** Sort latest arrival-time first */ static final Comparator ARRIVAL_TIME = Comparator.comparing(Itinerary::endTime); static final Comparator DEPARTURE_TIME = (a, b) -> b.startTime().compareTo(a.startTime()); static final Comparator GENERALIZED_COST = Comparator.comparingInt(a -> a.generalizedCost); static final Comparator NUM_OF_TRANSFERS = Comparator.comparingInt(a -> a.nTransfers); private final Comparator sortComparator; public OtpDefaultSortOrder(boolean arriveBy) { // Put walking first - encourage healthy lifestyle sortComparator = new CompositeComparator<>( STREET_ONLY_FIRST, arriveBy ? DEPARTURE_TIME : ARRIVAL_TIME, GENERALIZED_COST, NUM_OF_TRANSFERS, arriveBy ? ARRIVAL_TIME : DEPARTURE_TIME ); } @Override public String name() { return "otp-default-sort-order"; } @Override public List filter(List itineraries) { return itineraries.stream().sorted(sortComparator).collect(Collectors.toList()); } @Override public boolean removeItineraries() { return false; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy