com.github.skjolber.dc.GtfsFeed Maven / Gradle / Ivy
package com.github.skjolber.dc;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.github.skjolber.dc.model.Agency;
import com.github.skjolber.dc.model.Route;
import com.github.skjolber.dc.model.Service;
import com.github.skjolber.dc.model.ServiceCalendar;
import com.github.skjolber.dc.model.ServiceCalendarDate;
import com.github.skjolber.dc.model.Stop;
import com.github.skjolber.dc.model.StopTime;
import com.github.skjolber.dc.model.Transfer;
import com.github.skjolber.dc.model.Trip;
/**
* GTFS feed content.
*
*/
public class GtfsFeed {
private static final Pattern PATTERN = Pattern.compile("^(\\d{4})(\\d{2})(\\d{2})$");
private String agencyId;
protected Map agencyById = new HashMap<>(4096);
protected Map routeById = new HashMap<>(4096);
protected Map tripById = new HashMap<>(4096);
protected Map stopById = new HashMap<>(4096);
protected Map serviceById = new HashMap<>(4096);
protected List stopTimes = new ArrayList<>(4096);
protected List stops = new ArrayList<>(4096);
protected List trips = new ArrayList<>(4096);
protected List agencies = new ArrayList<>(4096);
protected List routes = new ArrayList<>(4096);
protected List transfers = new ArrayList<>(4096);
protected Map dates = new ConcurrentHashMap<>(4096);
protected List calendars = new ArrayList<>(4096);
protected List calendarDates = new ArrayList<>(4096);
public void setAgencyId(String agencyId) {
this.agencyId = agencyId;
}
public String getAgencyId() {
return agencyId;
}
public Map getRouteById() {
return routeById;
}
public void addTripById(Map trips) {
tripById.putAll(trips);
}
public void addAgenciesById(Map trips) {
agencyById.putAll(trips);
}
public Map getTripById() {
return tripById;
}
public void addStopById(Map stops) {
stopById.putAll(stops);
}
public Map getStopById() {
return stopById;
}
public void putRouteById(String id, Route route) {
this.routeById.put(id, route);
}
public void putTripById(String id, Trip trip) {
this.tripById.put(id, trip);
}
public void putServiceById(String id, Service trip) {
this.serviceById.put(id, trip);
}
public void putStopById(String id, Stop stop) {
this.stopById.put(id, stop);
}
public Stop getStop(String id) {
return stopById.get(id);
}
public Route getRoute(String id) {
return routeById.get(id);
}
public Trip getTrip(String id) {
return tripById.get(id);
}
public void setStopById(Map stopById) {
this.stopById = stopById;
}
public Agency getAgency(String id) {
return agencyById.get(id);
}
public void setAgencyById(Map agencyById) {
this.agencyById = agencyById;
}
public void setTripById(Map tripById) {
this.tripById = tripById;
}
public List getStopTimes() {
return stopTimes;
}
public List getStops() {
return stops;
}
public List getTrips() {
return trips;
}
public List getAgencies() {
return agencies;
}
public List getRoutes() {
return routes;
}
public LocalDate getDate(String value, boolean create) {
LocalDate localDate = dates.get(value);
if(localDate == null) {
if(create) {
localDate = parseString(value);
dates.put(value, localDate);
}
}
return localDate;
}
/**
* Parse a service date from a string in "YYYYMMDD" format.
*
* @param value a string of the form "YYYYMMDD"
* @return a new {@link LocalDate} object
*/
public static LocalDate parseString(String value) {
Matcher matcher = PATTERN.matcher(value);
if (!matcher.matches())
throw new IllegalArgumentException("Unexpected date '" + value + "'");
int year = Integer.parseInt(matcher.group(1));
int month = Integer.parseInt(matcher.group(2));
int day = Integer.parseInt(matcher.group(3));
return LocalDate.of(year, month, day);
}
public List getCalendars() {
return calendars;
}
public List getCalendarDates() {
return calendarDates;
}
public void add(String key, Stop stop) {
stopById.put(key, stop);
}
public void addStopTimes(List stopTimes) {
this.stopTimes.addAll(stopTimes);
}
public void addTrip(Trip trip) {
this.tripById.put(trip.getId(), trip);
}
public void addTrips(List trips) {
for(Trip trip : trips) {
this.tripById.put(trip.getId(), trip);
}
}
public void addStops(List stops) {
for(Stop stop: stops) {
this.stopById.put(stop.getId(), stop);
}
}
public void add(List agencies) {
for(Agency agency: agencies) {
this.agencyById.put(agency.getId(), agency);
}
}
public void addRoutes(List routes) {
for(Route route : routes) {
this.routeById.put(route.getId(), route);
}
}
public Service getService(String key) {
return serviceById.get(key);
}
public Map getServices() {
return serviceById;
}
public List getTransfers() {
return transfers;
}
}