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

org.opentripplanner.routing.fares.impl.MultipleFareServiceFactory Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.routing.fares.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.opentripplanner.model.OtpTransitService;
import org.opentripplanner.routing.fares.FareService;
import org.opentripplanner.routing.fares.FareServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

public abstract class MultipleFareServiceFactory implements FareServiceFactory {

    private static Logger log = LoggerFactory
            .getLogger(MultipleFareServiceFactory.class);

    private List subFactories;

    @Override
    public FareService makeFareService() {
        List subServices = new ArrayList<>();
        for (FareServiceFactory subFactory : subFactories)
            subServices.add(subFactory.makeFareService());
        return makeMultipleFareService(subServices);
    }

    protected abstract FareService makeMultipleFareService(List subServices);

    @Override
    public void processGtfs(OtpTransitService transitService) {
        for (FareServiceFactory subFactory : subFactories)
            subFactory.processGtfs(transitService);
    }

    /**
     * Accept several ways to define fares to compose. Examples:
     * 
     * 
     * { combinationStrategy : "additive",
     *   // An array of 'fares'
     *   fares : [ "seattle", { ... } ]
     * }
     * --------------------------
     * { combinationStrategy : "additive",
     *   // All properties starting with 'fare'
     *   fare1 : "seattle",
     *   fare2 : { type: "vehicle-rental-time-based",
     *             prices : { ... }
     * } }
     * 
*/ @Override public void configure(JsonNode config) { subFactories = new ArrayList<>(); for (JsonNode pConfig : config.path("fares")) { subFactories.add(DefaultFareServiceFactory.fromConfig(pConfig)); } for (Iterator> i = config.fields(); i.hasNext();) { Map.Entry kv = i.next(); String key = kv.getKey(); if (key.startsWith("fare") && !key.equals("fares")) { JsonNode node = kv.getValue(); FareServiceFactory fareFactory = DefaultFareServiceFactory.fromConfig(node); if (fareFactory != null) subFactories.add(fareFactory); } } if (subFactories.isEmpty()) throw new IllegalArgumentException( "Empty fare composite. Please specify either a 'fares' array or a list of 'fareXxx' properties"); if (subFactories.size() == 1) { // Legal, but suspicious. log.warn("Fare composite has only ONE fare to combine. This is allowed, but useless. Did you forgot to define a second fare to combine?"); } } public static class AddingMultipleFareServiceFactory extends MultipleFareServiceFactory { @Override protected FareService makeMultipleFareService(List subServices) { return new AddingMultipleFareService(subServices); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy