Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort GTFS files alphabetically inside GtfsModule #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class GtfsBundle {

private static final Logger LOG = LoggerFactory.getLogger(GtfsBundle.class);


private File path;

private URL url;
Expand Down Expand Up @@ -64,6 +65,8 @@ public GtfsBundle(File gtfsFile) {
this.setPath(gtfsFile);
}

public File getPath() { return path; }

public void setPath(File path) {
this.path = path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import org.onebusaway.csv_entities.EntityHandler;
import org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl;
Expand Down Expand Up @@ -64,9 +59,21 @@ public class GtfsModule implements GraphBuilderModule {

int nextAgencyId = 1; // used for generating agency IDs to resolve ID conflicts

public List<GtfsBundle> gtfsBundles;

public GtfsModule(List<GtfsBundle> bundles) { this.gtfsBundles = bundles; }

private List<GtfsBundle> gtfsBundles;

public Boolean getUseCached() {
return useCached;
}

private Comparator<GtfsBundle> compareByFileName = Comparator.comparing(bundle -> bundle.getPath().getName());

public GtfsModule(List<GtfsBundle> bundles) {
List<GtfsBundle> defensiveCopy = new ArrayList<>(bundles);
defensiveCopy.sort(compareByFileName);
this.gtfsBundles = defensiveCopy;
}

public List<String> provides() {
List<String> result = new ArrayList<String>();
Expand Down Expand Up @@ -95,6 +102,8 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
GtfsStopContext stopContext = new GtfsStopContext();

try {
String fileNames = gtfsBundles.stream().map(b -> b.getPath().getName()).collect(Collectors.joining(", "));
LOG.info("Processing GTFS files in the following order: {}", fileNames);
for (GtfsBundle gtfsBundle : gtfsBundles) {
// apply global defaults to individual GTFSBundles (if globals have been set)
if (cacheDirectory != null && gtfsBundle.cacheDirectory == null) {
Expand Down Expand Up @@ -392,4 +401,7 @@ public void checkInputs() {
}
}

public List<GtfsBundle> getGtfsBundles() {
return Collections.unmodifiableList(gtfsBundles);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.opentripplanner.graph_builder.module;

import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.opentripplanner.graph_builder.model.GtfsBundle;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class GtfsModuleTest {

@Test
public void shouldSortByFileNameAlphabetically() {
List<GtfsBundle> bundles = ImmutableList.of("C.gtfs", "c.gtfs", "/tmp/z.gtfs", "/x-files/001-a.gtfs", "/some/other/folder/b.gtfs")
.stream()
.map(name -> new GtfsBundle(new File(name))).collect(Collectors.toList());

GtfsModule module = new GtfsModule(bundles);

List<String> names = module.getGtfsBundles().stream().map(m -> m.getPath().getName()).collect(Collectors.toList());

assertThat(names, is(ImmutableList.of("001-a.gtfs", "C.gtfs", "b.gtfs", "c.gtfs", "z.gtfs")));

}
}