Skip to content

Commit

Permalink
Format with new settings
Browse files Browse the repository at this point in the history
  • Loading branch information
munterfi committed Apr 26, 2024
1 parent 7c999ec commit f99beed
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 48 deletions.
18 changes: 9 additions & 9 deletions src/main/java/ch/naviqore/gtfs/schedule/GtfsScheduleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import ch.naviqore.gtfs.schedule.type.ExceptionType;
import ch.naviqore.gtfs.schedule.type.RouteType;
import ch.naviqore.gtfs.schedule.type.ServiceDayTime;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.csv.CSVRecord;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
Expand All @@ -12,8 +15,6 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.csv.CSVRecord;

/**
* GTFS CSV records parser
Expand All @@ -39,13 +40,12 @@ public GtfsScheduleParser(GtfsScheduleBuilder builder) {
public void parse(CSVRecord record, GtfsScheduleFile fileType) {
Set<GtfsScheduleFile> warnings = EnumSet.noneOf(GtfsScheduleFile.class);
parsers.getOrDefault(fileType, r -> {
if (!warnings.contains(fileType)) {
log.warn("Unsupported GTFS file type for parsing: {}", fileType);
} else {
warnings.add(fileType);
}
})
.accept(record);
if (!warnings.contains(fileType)) {
log.warn("Unsupported GTFS file type for parsing: {}", fileType);
} else {
warnings.add(fileType);
}
}).accept(record);
}

private void initializeParsers() {
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/ch/naviqore/gtfs/schedule/GtfsScheduleReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ private static void readFromZip(File zipFile, GtfsScheduleParser parser) throws
ZipEntry entry = zf.getEntry(fileType.getFileName());
if (entry != null) {
log.info("Reading GTFS file from ZIP: {}", entry.getName());
try (InputStreamReader reader = new InputStreamReader(
BOMInputStream.builder().setInputStream(zf.getInputStream(entry))
.setByteOrderMarks(ByteOrderMark.UTF_8).setInclude(false).get(),
StandardCharsets.UTF_8)) {
try (InputStreamReader reader = new InputStreamReader(BOMInputStream.builder()
.setInputStream(zf.getInputStream(entry))
.setByteOrderMarks(ByteOrderMark.UTF_8)
.setInclude(false)
.get(), StandardCharsets.UTF_8)) {
readCsvRecords(reader, parser, fileType);
}
} else {
Expand All @@ -69,16 +70,19 @@ private static void readFromZip(File zipFile, GtfsScheduleParser parser) throws
}
}

private static void readCsvFile(File file, GtfsScheduleParser parser, GtfsScheduleFile fileType) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(
file); BOMInputStream bomInputStream = BOMInputStream.builder().setInputStream(fileInputStream)
.setByteOrderMarks(ByteOrderMark.UTF_8).get(); InputStreamReader reader = new InputStreamReader(
bomInputStream, StandardCharsets.UTF_8)) {
private static void readCsvFile(File file, GtfsScheduleParser parser,
GtfsScheduleFile fileType) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(file);
BOMInputStream bomInputStream = BOMInputStream.builder()
.setInputStream(fileInputStream)
.setByteOrderMarks(ByteOrderMark.UTF_8)
.get(); InputStreamReader reader = new InputStreamReader(bomInputStream, StandardCharsets.UTF_8)) {
readCsvRecords(reader, parser, fileType);
}
}

private static void readCsvRecords(InputStreamReader reader, GtfsScheduleParser recordParser, GtfsScheduleFile fileType) throws IOException {
private static void readCsvRecords(InputStreamReader reader, GtfsScheduleParser recordParser,
GtfsScheduleFile fileType) throws IOException {
CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().setIgnoreHeaderCase(true).setTrim(true).build();
try (CSVParser csvParser = new CSVParser(reader, format)) {
log.debug("CSV Headers: {}", csvParser.getHeaderMap().keySet());
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/ch/naviqore/gtfs/schedule/model/Calendar.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package ch.naviqore.gtfs.schedule.model;

import ch.naviqore.gtfs.schedule.type.ExceptionType;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;

@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
public final class Calendar {
Expand All @@ -25,7 +21,8 @@ public final class Calendar {
private final List<Trip> trips = new ArrayList<>();

/**
* Determines if the service is operational on a specific day, considering both regular service days and exceptions.
* Determines if the service is operational on a specific day, considering both regular service days and
* exceptions.
*
* @param date the date to check for service availability
* @return true if the service is operational on the given date, false otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class GtfsSchedule {
* @return GtfsScheduleDay containing only the active routes, stops, and trips for the specified date.
*/
public GtfsScheduleDay getScheduleForDay(LocalDate date) {
Map<String, Trip> activeTrips = trips.entrySet().stream()
Map<String, Trip> activeTrips = trips.entrySet()
.stream()
.filter(entry -> entry.getValue().getCalendar().isServiceAvailable(date))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ public int compareTo(StopTime o) {
return this.departure.compareTo(o.departure);
}
}

6 changes: 4 additions & 2 deletions src/main/java/ch/naviqore/gtfs/schedule/type/RouteType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public enum RouteType {
RAIL(2, "Rail. Used for intercity or long-distance travel."),
BUS(3, "Bus. Used for short- and long-distance bus routes."),
FERRY(4, "Ferry. Used for short- and long-distance boat service."),
CABLE_TRAM(5, "Cable tram. Used for street-level rail cars where the cable runs beneath the vehicle (e.g., cable car in San Francisco)."),
AERIAL_LIFT(6, "Aerial lift, suspended cable car (e.g., gondola lift, aerial tramway). Cable transport where cabins, cars, gondolas or open chairs are suspended by means of one or more cables."),
CABLE_TRAM(5,
"Cable tram. Used for street-level rail cars where the cable runs beneath the vehicle (e.g., cable car in San Francisco)."),
AERIAL_LIFT(6,
"Aerial lift, suspended cable car (e.g., gondola lift, aerial tramway). Cable transport where cabins, cars, gondolas or open chairs are suspended by means of one or more cables."),
FUNICULAR(7, "Funicular. Any rail system designed for steep inclines."),
TROLLEYBUS(11, "Trolleybus. Electric buses that draw power from overhead wires using poles."),
MONORAIL(12, "Monorail. Railway in which the track consists of a single rail or a beam.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package ch.naviqore.raptor.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor(access = AccessLevel.PACKAGE)
public class RouteTraversalBuilder {

Expand All @@ -29,7 +28,7 @@ public RouteTraversalBuilder addStopTime() {
return this;
}

public RouteTraversalBuilder addRouteStop(String id, String routeid) {
public RouteTraversalBuilder addRouteStop(String id, String routeId) {
if (routes.containsKey(id)) {
throw new IllegalArgumentException("Route " + id + " already exists");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import ch.naviqore.gtfs.schedule.GtfsScheduleBenchmarkData.Dataset;
import ch.naviqore.gtfs.schedule.model.GtfsSchedule;
import ch.naviqore.gtfs.schedule.model.GtfsScheduleDay;
import java.io.IOException;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.time.LocalDate;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class GtfsScheduleBenchmark {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
@Log4j2
public final class GtfsScheduleBenchmarkData {
private static final Path DATA_DIRECTORY = Path.of("benchmark/input");
private static final HttpClient httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS)
private static final HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();

public static void downloadFile(String fileURL, Path directory, String fileName) throws IOException, InterruptedException {
public static void downloadFile(String fileURL, Path directory,
String fileName) throws IOException, InterruptedException {
Path filePath = directory.resolve(fileName);
if (Files.notExists(filePath)) {
log.info("Downloading file: {}", fileURL);
Expand Down Expand Up @@ -69,8 +71,8 @@ public static String get(Dataset dataset) throws IOException, InterruptedExcepti
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public enum Dataset {
SWITZERLAND("https://opentransportdata.swiss/en/dataset/timetable-2024-gtfs2020/permalink"), GERMANY(
"https://download.gtfs.de/germany/free/latest.zip");
SWITZERLAND("https://opentransportdata.swiss/en/dataset/timetable-2024-gtfs2020/permalink"),
GERMANY("https://download.gtfs.de/germany/free/latest.zip");

private final String url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down

0 comments on commit f99beed

Please sign in to comment.