1 package net.jaekl.frank.octranspo;
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
8 import net.jaekl.qd.util.ParseUtils;
9 import net.jaekl.qd.xml.ParseResult;
10 import net.jaekl.qd.xml.XmlParseException;
12 public class Trip extends ParseResult {
13 static final String TRIP = "Trip";
15 static final String TRIP_DESTINATION = "TripDestination";
16 static final String TRIP_START_TIME = "TripStartTime";
17 static final String ADJUSTED_SCHEDULE_TIME = "AdjustedScheduleTime";
18 static final String ADJUSTMENT_AGE = "AdjustmentAge";
19 static final String LAST_TRIP_OF_SCHEDULE = "LastTripOfSchedule";
20 static final String BUS_TYPE = "BusType";
21 static final String GPS_SPEED = "GPSSpeed";
22 static final String LATITUDE = "Latitude";
23 static final String LONGITUDE = "Longitude";
25 static final String[] INTERNAL = { TRIP_DESTINATION,
27 ADJUSTED_SCHEDULE_TIME,
29 LAST_TRIP_OF_SCHEDULE,
34 static final Object[][] EXTERNAL = {};
36 String m_dest; // destination
37 Date m_start; // time at which the trip started / is scheduled to start
38 int m_adjTime; // minutes until bus is predicted to arrive
39 double m_adjAge; // time since the last GPS data was received, in minutes (possibly fractional)
40 boolean m_lastTrip; // is this the last scheduled trip of the day?
41 String m_busType; // type of bus
42 double m_speed; // speed (km/h) when last polled
43 double m_long; // longitude
44 double m_lat; // latitude
46 DateFormat m_dateFormat;
47 Date m_constructed; // DateTime when this object was constructed
50 super(TRIP, INTERNAL, EXTERNAL);
61 m_dateFormat = new SimpleDateFormat("hh:mm");
62 m_constructed = new Date();
65 public Trip(Trip other) {
66 super(TRIP, INTERNAL, EXTERNAL);
67 m_dest = other.m_dest;
68 m_start = other.m_start;
69 m_adjTime = other.m_adjTime;
70 m_adjAge = other.m_adjAge;
71 m_lastTrip = other.m_lastTrip;
72 m_busType = other.m_busType;
73 m_speed = other.m_speed;
74 m_long = other.m_long;
76 m_constructed = other.m_constructed;
79 public String getDest() { return m_dest; }
80 public Date getStart() { return m_start; }
81 public int getAdjTime() { return m_adjTime; }
82 public double getAdjAge() { return m_adjAge; }
83 public boolean isLastTrip() { return m_lastTrip; }
84 public String getBusType() { return m_busType; }
85 public double getSpeed() { return m_speed; }
86 public double getLongitude() { return m_long; }
87 public double getLatitude() { return m_lat; }
89 // Estimated (Date)Time of Arrival of this trip at the stop
90 public Date getETA() { return new Date(m_constructed.getTime() + (long)(1000 * 60 * m_adjTime)); }
92 // (Date)Time when the GPS for this bus was last read
93 public Date getGPSTime() { return new Date(m_constructed.getTime() - (long)(1000 * 60 * m_adjAge)); }
95 // ---------------------------
96 // ParseResult implementation:
99 public void endContents(String uri, String localName, String qName, String chars) throws XmlParseException
102 if (TRIP_DESTINATION.equals(localName)) {
105 else if (TRIP_START_TIME.equals(localName)) {
106 m_start = m_dateFormat.parse(chars);
108 else if (ADJUSTED_SCHEDULE_TIME.equals(localName)) {
109 m_adjTime = ParseUtils.parseInt(chars);
111 else if (ADJUSTMENT_AGE.equals(localName)) {
112 m_adjAge = ParseUtils.parseDouble(chars);
114 else if (LAST_TRIP_OF_SCHEDULE.equals(localName)) {
115 m_lastTrip = Boolean.parseBoolean(chars);
117 else if (BUS_TYPE.equals(localName)) {
120 else if (GPS_SPEED.equals(localName)) {
121 m_speed = ParseUtils.parseDouble(chars);
123 else if (LONGITUDE.equals(localName)) {
124 m_long = ParseUtils.parseDouble(chars);
126 else if (LATITUDE.equals(localName)) {
127 m_lat = ParseUtils.parseDouble(chars);
129 } catch (ParseException pe) {
130 throw new XmlParseException(pe);
135 public void endExternal(String uri, String localName, String qName) throws XmlParseException
137 // no externally-parsed children