| 1 | |
|---|
| 2 | package atmsdriver.model; |
|---|
| 3 | |
|---|
| 4 | import java.text.ParseException; |
|---|
| 5 | import java.text.SimpleDateFormat; |
|---|
| 6 | import java.util.Date; |
|---|
| 7 | import java.util.Scanner; |
|---|
| 8 | import java.util.logging.Level; |
|---|
| 9 | import java.util.logging.Logger; |
|---|
| 10 | import tmcsim.client.ATMSBatchDriver; |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * Traffic Event represents some occurrence in the traffic on |
|---|
| 14 | * a highway network. A traffic event occurs at a particular time |
|---|
| 15 | * and belongs to a unique simulation incident. The event occurs on |
|---|
| 16 | * a specified section of a given highway route. Each event specifies |
|---|
| 17 | * the level of traffic congestion by a color. Events are comparable |
|---|
| 18 | * by the time of their occurrence. |
|---|
| 19 | * @author jdalbey |
|---|
| 20 | */ |
|---|
| 21 | public final class TrafficEvent implements Comparable<TrafficEvent> |
|---|
| 22 | { |
|---|
| 23 | public final String incident; |
|---|
| 24 | public final String eventTime; |
|---|
| 25 | public final Date eventDate; // for convenience |
|---|
| 26 | public final int routeNumber; |
|---|
| 27 | public final LoopDetector.DOTCOLOR color; |
|---|
| 28 | public final Station.DIRECTION dir; |
|---|
| 29 | public final double postmile; |
|---|
| 30 | public final double range; |
|---|
| 31 | public final String rawString; |
|---|
| 32 | private final static SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); |
|---|
| 33 | |
|---|
| 34 | /** Create an event from a string as in this example: |
|---|
| 35 | * 181 00:12:30 405 S 0.6 11.0 G |
|---|
| 36 | * @param eventString |
|---|
| 37 | * @return traffic event |
|---|
| 38 | * @throws Scanner exception if string improperly formatted |
|---|
| 39 | */ |
|---|
| 40 | public TrafficEvent(String eventString) throws ParseException |
|---|
| 41 | { |
|---|
| 42 | this.rawString = eventString; |
|---|
| 43 | Scanner lineScan = new Scanner(eventString); |
|---|
| 44 | this.incident = lineScan.next(); |
|---|
| 45 | this.eventTime = lineScan.next(); // time field |
|---|
| 46 | // may throw parseexception |
|---|
| 47 | this.eventDate = formatter.parse(eventTime); |
|---|
| 48 | this.routeNumber = lineScan.nextInt(); |
|---|
| 49 | this.dir = Station.DIRECTION.toDirection(lineScan.next()); |
|---|
| 50 | this.postmile = lineScan.nextDouble(); |
|---|
| 51 | this.range = lineScan.nextDouble(); |
|---|
| 52 | this.color = LoopDetector.DOTCOLOR.toDotColor(lineScan.next()); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | @Override |
|---|
| 56 | public int compareTo(TrafficEvent o) |
|---|
| 57 | { |
|---|
| 58 | return eventDate.compareTo(o.eventDate); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | @Override |
|---|
| 62 | public String toString() |
|---|
| 63 | { |
|---|
| 64 | return rawString; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|