Skip to content

Commit

Permalink
Sort GTFS files alphabetically inside GtfsModule
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Mar 16, 2020
1 parent b272f14 commit 9fb309c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
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")));

}
}

0 comments on commit 9fb309c

Please sign in to comment.