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

org.opentripplanner.transit.raptor.api.request.SearchParamsBuilder Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.transit.raptor.api.request;

import org.opentripplanner.transit.raptor.api.transit.RaptorTripSchedule;
import org.opentripplanner.transit.raptor.api.transit.RaptorTransfer;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Mutable version of {@link SearchParams}.
 *
 * @param  The TripSchedule type defined by the user of the raptor API.
 */
public class SearchParamsBuilder {

    private final RaptorRequestBuilder parent;
    // Search
    private int earliestDepartureTime;
    private int latestArrivalTime;
    private int searchWindowInSeconds;
    private int boardSlackInSeconds;
    private int numberOfAdditionalTransfers;
    private int maxNumberOfTransfers;
    private double relaxCostAtDestination;
    private boolean timetableEnabled;
    private final Collection accessLegs = new ArrayList<>();
    private final Collection egressLegs = new ArrayList<>();

    public SearchParamsBuilder(RaptorRequestBuilder parent, SearchParams defaults) {
        this.parent = parent;
        this.earliestDepartureTime = defaults.earliestDepartureTime();
        this.latestArrivalTime = defaults.latestArrivalTime();
        this.searchWindowInSeconds = defaults.searchWindowInSeconds();
        this.boardSlackInSeconds = defaults.boardSlackInSeconds();
        this.numberOfAdditionalTransfers = defaults.numberOfAdditionalTransfers();
        this.maxNumberOfTransfers = defaults.maxNumberOfTransfers();
        this.relaxCostAtDestination = defaults.relaxCostAtDestination();
        this.timetableEnabled = defaults.timetableEnabled();
        this.accessLegs.addAll(defaults.accessLegs());
        this.egressLegs.addAll(defaults.egressLegs());
    }

    public int earliestDepartureTime() {
        return earliestDepartureTime;
    }

    public SearchParamsBuilder earliestDepartureTime(int earliestDepartureTime) {
        this.earliestDepartureTime = earliestDepartureTime;
        return this;
    }

    public int latestArrivalTime() {
        return latestArrivalTime;
    }

    public SearchParamsBuilder latestArrivalTime(int latestArrivalTime) {
        this.latestArrivalTime = latestArrivalTime;
        return this;
    }

    public int searchWindowInSeconds() {
        return searchWindowInSeconds;
    }

    public SearchParamsBuilder searchOneIterationOnly() {
        return searchWindowInSeconds(0);
    }

    public SearchParamsBuilder searchWindowInSeconds(int searchWindowInSeconds) {
        this.searchWindowInSeconds = searchWindowInSeconds;
        return this;
    }

    public SearchParamsBuilder searchWindow(Duration searchWindow) {
        this.searchWindowInSeconds = searchWindow == null
                ? SearchParams.NOT_SET
                : (int)searchWindow.toSeconds();
        return this;
    }

    public int boardSlackInSeconds() {
        return boardSlackInSeconds;
    }

    public SearchParamsBuilder boardSlackInSeconds(int boardSlackInSeconds) {
        this.boardSlackInSeconds = boardSlackInSeconds;
        return this;
    }

    public int numberOfAdditionalTransfers() {
        return numberOfAdditionalTransfers;
    }

    public SearchParamsBuilder numberOfAdditionalTransfers(int numberOfAdditionalTransfers) {
        this.numberOfAdditionalTransfers = numberOfAdditionalTransfers;
        return this;
    }

    public int maxNumberOfTransfers() {
        return maxNumberOfTransfers;
    }

    public SearchParamsBuilder maxNumberOfTransfers(int maxNumberOfTransfers) {
        this.maxNumberOfTransfers = maxNumberOfTransfers;
        return this;
    }

    public double relaxCostAtDestination() {
        return relaxCostAtDestination;
    }

    public SearchParamsBuilder relaxCostAtDestination(double relaxCostAtDestination) {
        this.relaxCostAtDestination = relaxCostAtDestination;
        return this;
    }

    public boolean timetableEnabled() {
        return timetableEnabled;
    }

    public SearchParamsBuilder timetableEnabled(boolean enable) {
        this.timetableEnabled = enable;
        return this;
    }

    public Collection accessLegs() {
        return accessLegs;
    }

    public SearchParamsBuilder addAccessStop(RaptorTransfer accessLeg) {
        this.accessLegs.add(accessLeg);
        return this;
    }

    public SearchParamsBuilder addAccessStops(Iterable accessLegs) {
        for (RaptorTransfer it : accessLegs) {
            addAccessStop(it);
        }
        return this;
    }

    public Collection egressLegs() {
        return egressLegs;
    }

    public SearchParamsBuilder addEgressStop(RaptorTransfer egressLeg) {
        this.egressLegs.add(egressLeg);
        return this;
    }

    public SearchParamsBuilder addEgressStops(Iterable egressLegs) {
        for (RaptorTransfer it : egressLegs) {
            addEgressStop(it);
        }
        return this;
    }

    public RaptorRequest build() {
        return parent.build();
    }

    /** This is public to allow tests to build just search params */
    public SearchParams buildSearchParam() {
        return new SearchParams(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy