org.opentripplanner.routing.algorithm.transferoptimization.services.TransferServiceAdaptor Maven / Gradle / Ivy
Show all versions of otp Show documentation
package org.opentripplanner.routing.algorithm.transferoptimization.services;
import java.util.function.IntFunction;
import javax.annotation.Nullable;
import org.opentripplanner.model.StopLocation;
import org.opentripplanner.model.Trip;
import org.opentripplanner.model.transfer.ConstrainedTransfer;
import org.opentripplanner.model.transfer.TransferService;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TripSchedule;
import org.opentripplanner.routing.algorithm.transferoptimization.model.TripStopTime;
import org.opentripplanner.transit.raptor.api.transit.RaptorTripSchedule;
/**
* This a just an adaptor to look up transfers constraints. The adaptor hides the
* {@link TransferService} specific API and functions as a bridge to the
* {@code transferoptimization} model. The best solution would be to use the same
* mechanism in Raptor and here, but that would require the main transit model to be refactored.
*
* The adaptor makes it easy to test the {@link TransferGenerator} by mocking.
*
* @param The TripSchedule type defined by the user of the raptor API.
*/
public class TransferServiceAdaptor {
private final IntFunction stopLookup;
private final TransferService transferService;
protected TransferServiceAdaptor(
IntFunction stopLookup,
TransferService transferService
) {
this.stopLookup = stopLookup;
this.transferService = transferService;
}
public static TransferServiceAdaptor create(
IntFunction stopLookup,
TransferService transferService
) {
return new TransferServiceAdaptor<>(stopLookup, transferService);
}
public static TransferServiceAdaptor noop() {
return new TransferServiceAdaptor<>(null, null) {
@Override
protected ConstrainedTransfer findTransfer(TripStopTime from, T toTrip, int toStop) { return null; }
};
}
/**
* Find transfer in the same stop for the given from location and to trip/stop.
*/
@Nullable
protected ConstrainedTransfer findTransfer(TripStopTime from, T toTrip, int toStop) {
return transferService.findTransfer(
trip(from.trip()),
from.stopPosition(),
stop(from.stop()),
trip(toTrip),
toTrip.findDepartureStopPosition(from.time(), toStop),
stop(toStop)
);
}
private StopLocation stop(int index) {
return stopLookup.apply(index);
}
private Trip trip(T raptorTripSchedule) {
return ((TripSchedule)raptorTripSchedule).getOriginalTripTimes().getTrip();
}
}