From 9fb309cfb9c4309c181828ae94ab26fa711aa6f8 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Mon, 16 Mar 2020 16:23:37 +0100 Subject: [PATCH] Sort GTFS files alphabetically inside GtfsModule --- .../graph_builder/model/GtfsBundle.java | 3 ++ .../graph_builder/module/GtfsModule.java | 30 +++++++++++++------ .../graph_builder/module/GtfsModuleTest.java | 29 ++++++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 src/test/java/org/opentripplanner/graph_builder/module/GtfsModuleTest.java diff --git a/src/main/java/org/opentripplanner/graph_builder/model/GtfsBundle.java b/src/main/java/org/opentripplanner/graph_builder/model/GtfsBundle.java index 3de7f9d2948..e0f913697cd 100644 --- a/src/main/java/org/opentripplanner/graph_builder/model/GtfsBundle.java +++ b/src/main/java/org/opentripplanner/graph_builder/model/GtfsBundle.java @@ -21,6 +21,7 @@ public class GtfsBundle { private static final Logger LOG = LoggerFactory.getLogger(GtfsBundle.class); + private File path; private URL url; @@ -64,6 +65,8 @@ public GtfsBundle(File gtfsFile) { this.setPath(gtfsFile); } + public File getPath() { return path; } + public void setPath(File path) { this.path = path; } diff --git a/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java b/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java index cc2947e7f2a..3459e25486f 100644 --- a/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java +++ b/src/main/java/org/opentripplanner/graph_builder/module/GtfsModule.java @@ -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; @@ -64,9 +59,21 @@ public class GtfsModule implements GraphBuilderModule { int nextAgencyId = 1; // used for generating agency IDs to resolve ID conflicts - public List gtfsBundles; - public GtfsModule(List bundles) { this.gtfsBundles = bundles; } + + private List gtfsBundles; + + public Boolean getUseCached() { + return useCached; + } + + private Comparator compareByFileName = Comparator.comparing(bundle -> bundle.getPath().getName()); + + public GtfsModule(List bundles) { + List defensiveCopy = new ArrayList<>(bundles); + defensiveCopy.sort(compareByFileName); + this.gtfsBundles = defensiveCopy; + } public List provides() { List result = new ArrayList(); @@ -95,6 +102,8 @@ public void buildGraph(Graph graph, HashMap, 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) { @@ -392,4 +401,7 @@ public void checkInputs() { } } + public List getGtfsBundles() { + return Collections.unmodifiableList(gtfsBundles); + } } diff --git a/src/test/java/org/opentripplanner/graph_builder/module/GtfsModuleTest.java b/src/test/java/org/opentripplanner/graph_builder/module/GtfsModuleTest.java new file mode 100644 index 00000000000..c1b77715e87 --- /dev/null +++ b/src/test/java/org/opentripplanner/graph_builder/module/GtfsModuleTest.java @@ -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 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 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"))); + + } +} \ No newline at end of file