Commit 5afadd68 authored by Zachary Seguin's avatar Zachary Seguin

WIP

parent 2dbc34cb
package ca.zacharyseguin.gtfs;
import ca.zacharyseguin.util.io.DateField;
import ca.zacharyseguin.util.io.Field;
/**
......@@ -34,11 +35,11 @@ public class Calendar
@Field("sunday")
private boolean sunday;
@Field("start_date")
private String startDate;
@DateField(value = "start_date", format = "yyyyMMdd")
private java.util.Calendar startDate;
@Field("end_date")
private String endDate;
@DateField(value = "end_date", format = "yyyyMMdd")
private java.util.Calendar endDate;
public Calendar()
{
......@@ -134,22 +135,22 @@ public class Calendar
this.sunday = sunday;
}
public String getStartDate()
public java.util.Calendar getStartDate()
{
return startDate;
}
public void setStartDate(String startDate)
public void setStartDate(java.util.Calendar startDate)
{
this.startDate = startDate;
}
public String getEndDate()
public java.util.Calendar getEndDate()
{
return endDate;
}
public void setEndDate(String endDate)
public void setEndDate(java.util.Calendar endDate)
{
this.endDate = endDate;
}
......
package ca.zacharyseguin.gtfs;
import ca.zacharyseguin.util.io.DateField;
import ca.zacharyseguin.util.io.Field;
/**
......@@ -14,8 +15,8 @@ public class CalendarDate
@Field("service_id")
private String serviceId;
@Field("date")
private String date;
@DateField(value = "date", format = "yyyyMMdd")
private java.util.Calendar date;
@Field("exception_type")
private ExceptionType exceptionType;
......@@ -37,12 +38,12 @@ public class CalendarDate
this.serviceId = serviceId;
}
public String getDate()
public java.util.Calendar getDate()
{
return date;
}
public void setDate(String date)
public void setDate(java.util.Calendar date)
{
this.date = date;
}
......
package ca.zacharyseguin.gtfs;
import ca.zacharyseguin.util.io.CSVParser;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
public class GTFS
{
public static String DATE_FORMAT = "yyyyMMdd";
public static String TIME_FORMAT = "HH:mm";
private Map<String, Agency> agencies;
private Map<String, Stop> stops;
private Map<String, Route> routes;
private Map<String ,Trip> trips;
private List<StopTime> stopTimes;
private List<Calendar> calendar;
private List<CalendarDate> calendarDates;
public GTFS(String directory) throws IOException
{
try
{
this.agencies = new HashMap<String, Agency>();
List<Agency> agencies = CSVParser.parse(Paths.get(directory, "agency.txt").toString(), Agency.class);
for (Agency agency : agencies)
this.agencies.put(agency.getId(), agency);
this.stops = new HashMap<String, Stop>();
List<Stop> stops = CSVParser.parse(Paths.get(directory, "stops.txt").toString(), Stop.class);
for (Stop stop : stops)
this.stops.put(stop.getId(), stop);
this.routes = new HashMap<String, Route>();
List<Route> routes = CSVParser.parse(Paths.get(directory, "routes.txt").toString(), Route.class);
for (Route route : routes)
this.routes.put(route.getId(), route);
this.trips = new HashMap<String, Trip>();
List<Trip> trips = CSVParser.parse(Paths.get(directory, "trips.txt").toString(), Trip.class);
for (Trip trip : trips)
this.trips.put(trip.getId(), trip);
this.stopTimes = CSVParser.parse(Paths.get(directory, "stop_times.txt").toString(), StopTime.class);
this.calendar = CSVParser.parse(Paths.get(directory, "calendar.txt").toString(), Calendar.class);
this.calendarDates = CSVParser.parse(Paths.get(directory, "calendar_dates.txt").toString(), CalendarDate.class);
}// End of try
catch (NoSuchMethodException e)
{
// should not happen :/
}// End of catch
}// End of constructor method
/**
* Returns a list of Service IDs for the specified date.
* @return List of service IDs for the specified date.
*/
public List<String> activeServiceIDs(java.util.Calendar date)
{
List<String> serviceIds = new ArrayList<String>();
// Load regular service
for (Calendar service : this.calendar)
{
boolean add = false;
switch(date.get(java.util.Calendar.DAY_OF_WEEK))
{
case java.util.Calendar.MONDAY:
add = service.hasMonday();
break;
case java.util.Calendar.TUESDAY:
add = service.hasTuesday();
break;
case java.util.Calendar.WEDNESDAY:
add = service.hasWednesday();
break;
case java.util.Calendar.THURSDAY:
add = service.hasThursday();
break;
case java.util.Calendar.FRIDAY:
add = service.hasFriday();
break;
case java.util.Calendar.SATURDAY:
add = service.hasSaturday();
break;
case java.util.Calendar.SUNDAY:
add = service.hasSunday();
break;
}// End of switch
if (add)
{
if (service.getStartDate().compareTo(date) <= 0 && service.getEndDate().compareTo(date) >= 0)
serviceIds.add(service.getServiceId());
}// End of if
}// End of for
// Get service exceptions
for (CalendarDate exception : this.calendarDates)
{
if ( exception.getDate().get(java.util.Calendar.YEAR) == date.get(java.util.Calendar.YEAR) &&
exception.getDate().get(java.util.Calendar.MONTH) == date.get(java.util.Calendar.MONTH) &&
exception.getDate().get(java.util.Calendar.DAY_OF_MONTH) == date.get(java.util.Calendar.DAY_OF_MONTH))
{
switch(exception.getExceptionType())
{
case ADDED:
serviceIds.add(exception.getServiceId());
break;
case REMOVED:
serviceIds.remove(exception.getServiceId());
break;
}// End of switch
}// End of if
}// End of for
return serviceIds;
}// End of activeServiceIDs method
/**
* Returns a list of Service IDs for the specified date.
* @return List of service IDs for specified date.
*/
public List<String> activeServiceIDs(Date date)
{
java.util.Calendar calendarDate = GregorianCalendar.getInstance();
calendarDate.setTime(date);
return this.activeServiceIDs(calendarDate);
}// End of activeServiceIDs
/**
* Returns a list of Service IDs for today.
* @return List of service IDs for today.
*/
public List<String> activeServiceIDs()
{
return this.activeServiceIDs(GregorianCalendar.getInstance());
}// End of activeServiceIDs
public Map<String, Agency> getAgencies()
{
return agencies;
}
public Map<String, Stop> getStops()
{
return stops;
}
public Map<String, Route> getRoutes()
{
return routes;
}
public Map<String, Trip> getTrips()
{
return trips;
}
public List<StopTime> getStopTimes()
{
return stopTimes;
}
public List<Calendar> getCalendar()
{
return calendar;
}
public List<CalendarDate> getCalendarDates()
{
return calendarDates;
}
}
......@@ -2,26 +2,29 @@ package ca.zacharyseguin.gtfs;
import ca.zacharyseguin.util.io.CSVParser;
import java.nio.file.Paths;
import java.util.List;
public class Main
{
public static void main(String[] args) throws Exception
{
List<Agency> agencies = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/agency.txt", Agency.class);
List<Route> routes = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/routes.txt", Route.class);
List<Stop> stops = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/stops.txt", Stop.class);
List<Trip> trips = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/trips.txt", Trip.class);
List<StopTime> stopTimes = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/stop_times.txt", StopTime.class);
List<Calendar> calendar = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/calendar.txt", Calendar.class);
List<CalendarDate> calendarDates = CSVParser.parse("/Users/zachary/tmp/grt_gtfs/calendar_dates.txt", CalendarDate.class);
GTFS gtfs = new GTFS("/Users/zachary/tmp/grt_gtfs");
System.out.println("Loaded " + agencies.size() + " agencies.");
System.out.println("Loaded " + routes.size() + " routes.");
System.out.println("Loaded " + stops.size() + " stops.");
System.out.println("Loaded " + trips.size() + " trips.");
System.out.println("Loaded " + stopTimes.size() + " stop times.");
System.out.println("Loaded " + calendar.size() + " calendar services.");
System.out.println("Loaded " + calendarDates.size() + " calendar dates.");
System.out.println("Loaded " + gtfs.getAgencies().size() + " agencies.");
System.out.println("Loaded " + gtfs.getRoutes().size() + " routes.");
System.out.println("Loaded " + gtfs.getStops().size() + " stops.");
System.out.println("Loaded " + gtfs.getTrips().size() + " trips.");
System.out.println("Loaded " + gtfs.getStopTimes().size() + " stop times.");
System.out.println("Loaded " + gtfs.getCalendar().size() + " calendar services.");
System.out.println("Loaded " + gtfs.getCalendarDates().size() + " calendar dates.");
System.out.println();
List<String> serviceIds = gtfs.activeServiceIDs();
for (String serviceId : serviceIds)
{
System.out.println(serviceId);
}
}// End of main method
}// End of class
\ No newline at end of file
......@@ -7,10 +7,9 @@ import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class CSVParser
{
......@@ -24,12 +23,14 @@ public class CSVParser
{
List<T> objects = new ArrayList<T>();
Map<String, Setter> setters = new HashMap<String, Setter>();
Map<String, String> dateTimeFormats = new HashMap<String, String>();
// Decode fields
java.lang.reflect.Field[] fields = type.getDeclaredFields();
for (java.lang.reflect.Field field : fields)
{
Field annotation = (Field)field.getAnnotation(Field.class);
DateField dateAnnotation = (DateField)field.getAnnotation(DateField.class);
if (annotation != null)
{
......@@ -49,6 +50,26 @@ public class CSVParser
throw new NoSuchMethodException("Field " + field.getName() + " has no setter (" + "set" + setterName + ").");
}// End of catch
}// End of if
if (dateAnnotation != null)
{
String setterName = field.getName();
setterName = setterName.toUpperCase().charAt(0) + setterName.substring(1, setterName.length());
try
{
Setter setter = new Setter();
setter.method = type.getMethod("set" + setterName, field.getType());
setter.type = field.getType();
setters.put(dateAnnotation.value(), setter);
dateTimeFormats.put(dateAnnotation.value(), dateAnnotation.format());
}
catch (Exception e)
{
throw new NoSuchMethodException("Field " + field.getName() + " has no setter (" + "set" + setterName + ").");
}// End of catch
}// End of if
}// End of for
// Read the CSV file
......@@ -148,6 +169,27 @@ public class CSVParser
}// End of catch
}// End of else if
else if (setter.type == java.util.Calendar.class)
{
try
{
SimpleDateFormat sdf = new SimpleDateFormat(dateTimeFormats.get(headers[x]));
java.util.Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(sdf.parse(row[x]));
setter.method.invoke(object, calendar);
}// End of try
catch (InvocationTargetException e)
{
}// End of catch
catch (ParseException e)
{
e.printStackTrace();
// TODO: Handle/Inform of exception
}
}// End of else if
else
{
try
......
package ca.zacharyseguin.util.io;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Interface for declaring a CSV Date Field.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DateField
{
/**
* The name of the field, as provided in the CSV header.
*/
public String value();
/**
* The format of the date. (See {#link java.text.SimpleDateFormat})
* @see java.text.SimpleDateFormat
*/
public String format();
}// End of @interface
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment