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

org.opentripplanner.ext.fares.FaresConfiguration Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
package org.opentripplanner.ext.fares;

import com.fasterxml.jackson.databind.JsonNode;
import org.opentripplanner.ext.fares.impl.AtlantaFareServiceFactory;
import org.opentripplanner.ext.fares.impl.CombineInterlinedLegsFactory;
import org.opentripplanner.ext.fares.impl.DefaultFareServiceFactory;
import org.opentripplanner.ext.fares.impl.HSLFareServiceFactory;
import org.opentripplanner.ext.fares.impl.HighestFareInFreeTransferWindowFareServiceFactory;
import org.opentripplanner.ext.fares.impl.MultipleFareServiceFactory;
import org.opentripplanner.ext.fares.impl.NoopFareServiceFactory;
import org.opentripplanner.ext.fares.impl.NycFareServiceFactory;
import org.opentripplanner.ext.fares.impl.SFBayFareServiceFactory;
import org.opentripplanner.ext.fares.impl.TimeBasedVehicleRentalFareServiceFactory;
import org.opentripplanner.routing.fares.FareServiceFactory;
import org.opentripplanner.standalone.config.framework.json.NodeAdapter;
import org.opentripplanner.standalone.config.framework.json.OtpVersion;

public class FaresConfiguration {

  public static FareServiceFactory fromConfig(NodeAdapter root, String parameterName) {
    // Fares uses the raw node, not the types-safe adapter, but defining the fares root here
    // will cause fares to be added to the build-config configuration document with a link to the
    // Fares.md.
    var fares = root
      .of(parameterName)
      .summary("Fare configuration.")
      .since(OtpVersion.V2_0)
      .asObject();

    return fromConfig(fares.rawNode());
  }

  /**
   * Build a specific FareServiceFactory given the config node, or fallback to the default if none
   * specified.
   * 

* Accept different formats. Examples: * *

   * { fares : "seattle" }
   * --------------------------
   * { fares : {} } // Fallback to default
   * --------------------------
   * { fares : {
   *       type : "foobar",
   *       param1 : 42
   * } }
   * --------------------------
   * { fares : {
   *       combinationStrategy : "additive",
   *       fares : [
   *           "seattle",
   *           { type : "foobar", ... }
   *       ]
   * } }
   * 
*/ public static FareServiceFactory fromConfig(JsonNode config) { String type = null; if (config == null) { /* Empty block, fallback to default */ type = null; } else if (config.isTextual()) { /* Simplest form: { fares : "seattle" } */ type = config.asText(); } else if (config.has("combinationStrategy")) { /* Composite */ String combinationStrategy = config.path("combinationStrategy").asText(); switch (combinationStrategy) { case "additive": break; default: throw new IllegalArgumentException( "Unknown fare combinationStrategy: " + combinationStrategy ); } type = "composite:" + combinationStrategy; } else if (config.has("type")) { /* Fare with a type: { fares : { type : "foobar", param1 : 42 } } */ type = config.path("type").asText(null); } if (type == null) { type = "default"; } FareServiceFactory factory = createFactory(type); factory.configure(config); return factory; } private static FareServiceFactory createFactory(String type) { return switch (type) { case "default" -> new DefaultFareServiceFactory(); case "off" -> new NoopFareServiceFactory(); case "composite:additive" -> new MultipleFareServiceFactory.AddingMultipleFareServiceFactory(); case "vehicle-rental-time-based", "bike-rental-time-based" -> new TimeBasedVehicleRentalFareServiceFactory(); //TODO: deprecated, remove in next major version case "san-francisco" -> new SFBayFareServiceFactory(); case "new-york" -> new NycFareServiceFactory(); case "highest-fare-in-free-transfer-window", "highestFareInFreeTransferWindow" -> new HighestFareInFreeTransferWindowFareServiceFactory(); case "hsl" -> new HSLFareServiceFactory(); case "atlanta" -> new AtlantaFareServiceFactory(); case "combine-interlined-legs" -> new CombineInterlinedLegsFactory(); default -> throw new IllegalArgumentException(String.format("Unknown fare type: '%s'", type)); }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy