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

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

There is a newer version: 2.6.0
Show newest version
/* This program is free software: you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public License
 as published by the Free Software Foundation, either version 3 of
 the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see . */

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.onebusaway.gtfs.model.AgencyAndId;
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 - 2025 Weber Informatics LLC | Privacy Policy