hw6
Class TransitSystem

java.lang.Object
  |
  +--hw6.TransitSystem
All Implemented Interfaces:
java.util.EventListener, TransitBusListener

public class TransitSystem
extends java.lang.Object
implements TransitBusListener

This class builds and manages the transit system for homework 6.


Constructor Summary
TransitSystem(java.lang.String hostname, int port)
          Create a set of buses for this transit system.
 
Method Summary
 void dataReceived(hw6.TransitBusEvent evt)
          This method is called every time a new bus information data record arrives.
 java.util.ArrayList getBusList()
          Create a list of all the buses in the system.
 void start()
          This method is called to start the system operating.
 void stop()
          This method is called to stop the system operating.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransitSystem

public TransitSystem(java.lang.String hostname,
                     int port)
              throws java.io.IOException
Create a set of buses for this transit system.

Parameters:
hostname - the network host that is supplying the data
port - the port that the data is being read from
Method Detail

start

public void start()
           throws java.io.IOException
This method is called to start the system operating. This means that data records will start arriving from the TrafficReceiver. Throws an IOException if there is a problem actually connecting to the data source.

Throws:
java.io.IOException - in the event of a connection failure

stop

public void stop()
This method is called to stop the system operating. This means that data records will stop arriving from the TrafficReceiver.


dataReceived

public void dataReceived(hw6.TransitBusEvent evt)
This method is called every time a new bus information data record arrives. It sets the VIN in the previously created MutableVehicle object, then uses that as the key to see if we already have this Vehicle in the Map. If we have it, then we retrieve it and update it in place. If we don't have it, a new TransitBus is created using the information from the event object, and then that is added to the Map. Note that the Vehicle itself is used as both the key and the value in the Map.

Specified by:
dataReceived in interface TransitBusListener
Parameters:
evt - the TransitBusEvent that describes what happened

getBusList

public java.util.ArrayList getBusList()
Create a list of all the buses in the system. This is a little more elaborate than just returning a reference to a view of the Map, because we don't want changes in the objects that are being stored in the Map to cause problems for the user of the returned list, and vice versa. Thus, we clone all the TransitBus objects and store references to them in the new List. If anything goes wrong with the cloning, this method returns null.

Returns:
a List containing clones of all the currently defined TransitBus objects, or null if anything goes wrong with the cloning operations.