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

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

The newest version!
package org.opentripplanner.raptor.api.request;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.opentripplanner.framework.tostring.ToStringBuilder;
import org.opentripplanner.raptor.api.model.RaptorAccessEgress;
import org.opentripplanner.raptor.api.model.RaptorConstants;
import org.opentripplanner.raptor.api.model.RaptorTripSchedule;

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

  private final RaptorRequestBuilder parent;
  private final Collection accessPaths = new ArrayList<>();
  private final Collection egressPaths = new ArrayList<>();
  // Search
  private int earliestDepartureTime;
  private int latestArrivalTime;
  private int searchWindowInSeconds;
  private boolean preferLateArrival;
  private int numberOfAdditionalTransfers;
  private int maxNumberOfTransfers;
  private boolean timetable;
  private boolean constrainedTransfers;

  public SearchParamsBuilder(RaptorRequestBuilder parent, SearchParams defaults) {
    this.parent = parent;
    this.earliestDepartureTime = defaults.earliestDepartureTime();
    this.latestArrivalTime = defaults.latestArrivalTime();
    this.searchWindowInSeconds = defaults.searchWindowInSeconds();
    this.preferLateArrival = defaults.preferLateArrival();
    this.numberOfAdditionalTransfers = defaults.numberOfAdditionalTransfers();
    this.maxNumberOfTransfers = defaults.maxNumberOfTransfers();
    this.timetable = defaults.timetable();
    this.constrainedTransfers = defaults.constrainedTransfers();
    this.accessPaths.addAll(defaults.accessPaths());
    this.egressPaths.addAll(defaults.egressPaths());
  }

  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 searchWindowInSeconds(int searchWindowInSeconds) {
    this.searchWindowInSeconds = searchWindowInSeconds;
    return this;
  }

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

  /**
   * Do one RangeRaptor iteration. This disable the dynamic resolved search-window Alias for calling
   * {@code searchWindow(Duration.ZERO)}.
   */
  public SearchParamsBuilder searchOneIterationOnly() {
    return searchWindowInSeconds(0);
  }

  public boolean preferLateArrival() {
    return preferLateArrival;
  }

  public SearchParamsBuilder preferLateArrival(boolean enable) {
    this.preferLateArrival = enable;
    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 boolean timetable() {
    return timetable;
  }

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

  public boolean constrainedTransfers() {
    return constrainedTransfers;
  }

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

  public Collection accessPaths() {
    return accessPaths;
  }

  public SearchParamsBuilder addAccessPaths(
    Collection accessPaths
  ) {
    this.accessPaths.addAll(accessPaths);
    return this;
  }

  public SearchParamsBuilder addAccessPaths(RaptorAccessEgress... accessPaths) {
    return addAccessPaths(Arrays.asList(accessPaths));
  }

  public Collection egressPaths() {
    return egressPaths;
  }

  public SearchParamsBuilder addEgressPaths(
    Collection egressPaths
  ) {
    this.egressPaths.addAll(egressPaths);
    return this;
  }

  public SearchParamsBuilder addEgressPaths(RaptorAccessEgress... egressPaths) {
    return addEgressPaths(Arrays.asList(egressPaths));
  }

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

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

  @Override
  public String toString() {
    return ToStringBuilder
      .of(SearchParams.class)
      .addServiceTime("earliestDepartureTime", earliestDepartureTime, RaptorConstants.TIME_NOT_SET)
      .addServiceTime("latestArrivalTime", latestArrivalTime, RaptorConstants.TIME_NOT_SET)
      .addDurationSec("searchWindow", searchWindowInSeconds)
      .addBoolIfTrue("departAsLateAsPossible", preferLateArrival)
      .addNum("numberOfAdditionalTransfers", numberOfAdditionalTransfers)
      .addCollection("accessPaths", accessPaths, 5)
      .addCollection("egressPaths", egressPaths, 5)
      .toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy