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

org.opentripplanner.graph_builder.module.EntityReplacementStrategyFactory Maven / Gradle / Ivy

package org.opentripplanner.graph_builder.module;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.opentripplanner.model.FeedScopedId;
import org.opentripplanner.graph_builder.services.EntityReplacementStrategy;
import org.opentripplanner.gtfs.GtfsLibrary;

public class EntityReplacementStrategyFactory {

  private Map, File> _mappings = new HashMap, File>();

  public void setEntityMappings(Map, File> mappings) {
    _mappings.putAll(mappings);
  }

  public EntityReplacementStrategy create() throws IOException {
    EntityReplacementStrategyImpl impl = new EntityReplacementStrategyImpl();
    for (Map.Entry, File> entry : _mappings.entrySet()) {

      Class entityClass = entry.getKey();
      File file = entry.getValue();

      BufferedReader reader = new BufferedReader(new FileReader(file));
      String line = null;

      while ((line = reader.readLine()) != null) {
        String[] tokens = line.split("\\s+");
        List ids = new ArrayList();
        for (String token : tokens)
          ids.add(GtfsLibrary.convertIdFromString(token));
        for (int i = 1; i < ids.size(); i++)
          impl.addEntityReplacement(entityClass, ids.get(i), ids.get(0));
      }
      reader.close();
    }
    return impl;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy