Initial
[frank.git] / prod / net / jaekl / frank / octranspo / Trip.java
1 package net.jaekl.frank.octranspo;
2
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7
8 import net.jaekl.qd.util.ParseUtils;
9 import net.jaekl.qd.xml.ParseResult;
10 import net.jaekl.qd.xml.XmlParseException;
11
12 public class Trip extends ParseResult {
13         static final String TRIP = "Trip";
14         
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";
24         
25         static final String[] INTERNAL = { TRIP_DESTINATION,
26                                                TRIP_START_TIME,
27                                                ADJUSTED_SCHEDULE_TIME,
28                                                ADJUSTMENT_AGE,
29                                                LAST_TRIP_OF_SCHEDULE,
30                                                BUS_TYPE,
31                                                GPS_SPEED,
32                                                LATITUDE,
33                                                LONGITUDE };
34         static final Object[][] EXTERNAL = {};
35         
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
45     
46     DateFormat m_dateFormat;
47     Date       m_constructed;   // DateTime when this object was constructed
48
49     public Trip() {
50         super(TRIP, INTERNAL, EXTERNAL);
51         m_dest = "";
52         m_start = new Date();
53         m_adjTime = 0;
54         m_adjAge = 0.0;
55         m_lastTrip = false;
56         m_busType = "";
57         m_speed = 0.0;
58         m_long = 0.0;
59         m_lat = 0.0;
60         
61         m_dateFormat = new SimpleDateFormat("hh:mm");
62         m_constructed = new Date();
63     }
64     
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;
75         m_lat = other.m_lat;
76         m_constructed = other.m_constructed;
77         }
78
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; }
88     
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)); }
91     
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)); } 
94
95     // ---------------------------
96     // ParseResult implementation:
97     
98         @Override
99         public void endContents(String uri, String localName, String qName,     String chars) throws XmlParseException 
100         {
101                 try {
102                         if (TRIP_DESTINATION.equals(localName)) {
103                                 m_dest = chars;
104                         }
105                         else if (TRIP_START_TIME.equals(localName)) {
106                                 m_start = m_dateFormat.parse(chars);
107                         }
108                         else if (ADJUSTED_SCHEDULE_TIME.equals(localName)) {
109                                 m_adjTime = ParseUtils.parseInt(chars);
110                         }
111                         else if (ADJUSTMENT_AGE.equals(localName)) {
112                                 m_adjAge = ParseUtils.parseDouble(chars);
113                         }
114                         else if (LAST_TRIP_OF_SCHEDULE.equals(localName)) {
115                                 m_lastTrip = Boolean.parseBoolean(chars);
116                         }
117                         else if (BUS_TYPE.equals(localName)) {
118                                 m_busType = chars;
119                         }
120                         else if (GPS_SPEED.equals(localName)) {
121                                 m_speed = ParseUtils.parseDouble(chars);
122                         }
123                         else if (LONGITUDE.equals(localName)) {
124                                 m_long = ParseUtils.parseDouble(chars);
125                         }
126                         else if (LATITUDE.equals(localName)) {
127                                 m_lat = ParseUtils.parseDouble(chars);
128                         }
129                 } catch (ParseException pe) {
130                         throw new XmlParseException(pe);
131                 }
132         }
133         
134         @Override
135         public void endExternal(String uri, String localName, String qName) throws XmlParseException
136         {
137                 // no externally-parsed children
138         }
139 }
140