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

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

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.opentripplanner.raptor.api.debug.DebugEvent;
import org.opentripplanner.raptor.api.debug.DebugLogger;
import org.opentripplanner.raptor.api.path.RaptorPath;
import org.opentripplanner.raptor.api.view.ArrivalView;
import org.opentripplanner.raptor.api.view.PatternRideView;

/**
 * Mutable version of {@link DebugRequest}.
 */
public class DebugRequestBuilder {

  private final Set stops = new HashSet<>();
  private List path = new ArrayList<>();
  private int debugPathFromStopIndex;
  private Consumer>> stopArrivalListener;
  private Consumer>> patternRideDebugListener;
  private Consumer>> pathFilteringListener;
  private DebugLogger logger;

  DebugRequestBuilder(DebugRequest debug) {
    this.stops.addAll(debug.stops());
    this.path.addAll(debug.path());
    this.debugPathFromStopIndex = debug.debugPathFromStopIndex();
    this.stopArrivalListener = debug.stopArrivalListener();
    this.patternRideDebugListener = debug.patternRideDebugListener();
    this.pathFilteringListener = debug.pathFilteringListener();
    this.logger = debug.logger();
  }

  /** Read-only view to stops added sorted in ascending order. */
  public List stops() {
    return stops.stream().sorted().collect(Collectors.toList());
  }

  public DebugRequestBuilder addStops(Collection stops) {
    this.stops.addAll(stops);
    return this;
  }

  public DebugRequestBuilder addStops(int... stops) {
    return addStops(Arrays.stream(stops).boxed().collect(Collectors.toList()));
  }

  /**
   * The list of stops for a given path to debug.
   */
  public List path() {
    return path;
  }

  public DebugRequestBuilder setPath(List stopsInPath) {
    if (!path.isEmpty()) {
      throw new IllegalStateException(
        "The API support only one debug path. " + "Existing: " + path + ", new: " + stopsInPath
      );
    }
    this.path = new ArrayList<>(stopsInPath);
    return this;
  }

  public int debugPathFromStopIndex() {
    return debugPathFromStopIndex;
  }

  /**
   * Select the stop index where the debugging should start. It is the index of the stops in the
   * path list.
   */
  public DebugRequestBuilder debugPathFromStopIndex(int stopIndex) {
    this.debugPathFromStopIndex = stopIndex;
    return this;
  }

  public Consumer>> stopArrivalListener() {
    return stopArrivalListener;
  }

  public DebugRequestBuilder stopArrivalListener(Consumer>> listener) {
    this.stopArrivalListener = listener;
    return this;
  }

  public Consumer>> patternRideDebugListener() {
    return patternRideDebugListener;
  }

  public DebugRequestBuilder patternRideDebugListener(
    Consumer>> listener
  ) {
    this.patternRideDebugListener = listener;
    return this;
  }

  public Consumer>> pathFilteringListener() {
    return pathFilteringListener;
  }

  public DebugRequestBuilder pathFilteringListener(Consumer>> listener) {
    this.pathFilteringListener = listener;
    return this;
  }

  public DebugLogger logger() {
    return logger;
  }

  public DebugRequestBuilder logger(DebugLogger logger) {
    this.logger = logger;
    return this;
  }

  public DebugRequestBuilder reverseDebugRequest() {
    Collections.reverse(this.path);
    return this;
  }

  public DebugRequest build() {
    return new DebugRequest(
      List.copyOf(stops),
      List.copyOf(path),
      debugPathFromStopIndex,
      stopArrivalListener,
      patternRideDebugListener,
      pathFilteringListener,
      logger
    );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy