Class TimeTableDataManager


  • public class TimeTableDataManager
    extends java.lang.Object
    Provide data base management services.

    The data structure was migrated from a MySQL database. As such, it contains tables implemented as TreeMaps and records implemented as Classes. The logical relationships are handled using foreign keys.

     Data Structure:
       Layout -- Global data.
         TrainTypes -- Assigned to trains for diagram colors.
         Segments -- Used for division / sub-division arrangements.
           Stations -- Any place a train can stop.
         Schedules -- Basic information about a schedule.
           Trains -- Train characteristics.
             Stops -- A junction between a train and a station that contains arrival and departure times.
     
    • Constructor Detail

      • TimeTableDataManager

        public TimeTableDataManager​(boolean loadData)
        Create a TimeTableDataManager instance.
        Parameters:
        loadData - False to create an empty instance, otherwise load the data
    • Method Detail

      • getDataManager

        public static TimeTableDataManager getDataManager()
        Use the InstanceManager to only allow a single data manager instance.
        Returns:
        the current or new data manager.
      • addStation

        public void addStation​(int id,
                               Station newStation)
        Add a new station. Create a SegmentStation instance. Add it to the SegmentStation list.
        Parameters:
        id - map id.
        newStation - the new station.
      • addStop

        public void addStop​(int id,
                            Stop newStop)
      • deleteLayout

        public void deleteLayout​(int id)
        Delete the layout if there are no train types, segments or schedules.
        Parameters:
        id - The layout id.
        Throws:
        java.lang.IllegalArgumentException - LAYOUT_HAS_CHILDREN
      • deleteTrainType

        public void deleteTrainType​(int id)
        Delete the train type if there are no train references.
        Parameters:
        id - The train type id.
        Throws:
        java.lang.IllegalArgumentException - TYPE_HAS_REFERENCE
      • deleteSegment

        public void deleteSegment​(int id)
        Delete the segment if it has no stations.
        Parameters:
        id - The segment id.
        Throws:
        java.lang.IllegalArgumentException - SEGMENT_HAS_CHILDREN
      • deleteStation

        public void deleteStation​(int id)
        Delete the station if there are no stop references.
        Parameters:
        id - The station id.
        Throws:
        java.lang.IllegalArgumentException - STATION_HAS_REFERENCE
      • deleteSchedule

        public void deleteSchedule​(int id)
        Delete the schedule if it has no trains.
        Parameters:
        id - The schedule id.
        Throws:
        java.lang.IllegalArgumentException - SCHEDULE_HAS_CHILDREN
      • deleteTrain

        public void deleteTrain​(int id)
        Delete the train if it has no stops.
        Parameters:
        id - The train id.
        Throws:
        java.lang.IllegalArgumentException - TRAIN_HAS_CHILDREN
      • deleteStop

        public void deleteStop​(int id)
        Delete the stop and update train schedule.
        Parameters:
        id - The stop id.
      • getNextId

        public int getNextId​(java.lang.String type)
        Get the last key from the map and add 1.
        Parameters:
        type - The record type which is used to select the appropriate map.
        Returns:
        the next id, or 0 if there is an error.
      • getLayouts

        public java.util.List<LayoutgetLayouts​(boolean sort)
        Create a list of layouts
        Parameters:
        sort - If true, sort the resulting list
        Returns:
        a list of layouts
      • getTrainTypes

        public java.util.List<TrainTypegetTrainTypes​(int fKeyLayout,
                                                       boolean sort)
        Create a list of train types
        Parameters:
        fKeyLayout - If non-zero, select the types that have the specified foreign key
        sort - If true, sort the resulting list
        Returns:
        a list of train types
      • getSegments

        public java.util.List<SegmentgetSegments​(int fKeyLayout,
                                                   boolean sort)
        Create a list of segments
        Parameters:
        fKeyLayout - If non-zero, select the segments that have the specified foreign key
        sort - If true, sort the resulting list
        Returns:
        a list of segments
      • getStations

        public java.util.List<StationgetStations​(int fKeySegment,
                                                   boolean sort)
        Create a list of stations
        Parameters:
        fKeySegment - If non-zero, select the stations that have the specified foreign key
        sort - If true, sort the resulting list
        Returns:
        a list of stations
      • getSchedules

        public java.util.List<SchedulegetSchedules​(int fKeyLayout,
                                                     boolean sort)
        Create a list of schedules
        Parameters:
        fKeyLayout - If non-zero, select the schedules that have the specified foreign key
        sort - If true, sort the resulting list
        Returns:
        a list of schedules
      • getTrains

        public java.util.List<TraingetTrains​(int fKeySchedule,
                                               int fKeyType,
                                               boolean sort)
        Create a list of trains
        Parameters:
        fKeySchedule - If non-zero, select the trains that have the specified foreign key
        fKeyType - If non-zero, select the trains that have the specified foreign key
        sort - If true, sort the resulting list
        Returns:
        a list of trains
      • getStops

        public java.util.List<StopgetStops​(int fKeyTrain,
                                             int fKeyStation,
                                             boolean sort)
        Create a list of stops
        Parameters:
        fKeyTrain - If non-zero, select the stops that have the specified foreign key
        fKeyStation - If non-zero, select the stops that have the specified foreign key
        sort - If true, sort the resulting list
        Returns:
        a list of stops
      • calculateLayoutTrains

        void calculateLayoutTrains​(int layoutId,
                                   boolean updateStops)
        Update the stops for all of the trains for this layout. Invoked by updates to fast clock speed, metric, scale and station distances.
        Parameters:
        layoutId - The id for the layout that has been updated.
        updateStops - True for update
      • calculateScheduleTrains

        void calculateScheduleTrains​(int scheduleId,
                                     boolean updateStops)
        Update the stop times for all of the trains that use this schedule.
        Parameters:
        scheduleId - The id for the schedule that has been updated.
        updateStops - True for update
      • calculateTrain

        public void calculateTrain​(int trainId,
                                   boolean updateStops)
        Calculate the arrival and departure times for all of the stops.
        Parameters:
        trainId - The id of the train to be updated.
        updateStops - When true, update the arrive and depart times.
        Throws:
        java.lang.IllegalArgumentException - when stop times are outside of the schedule times or a segment change failed. The TIME_OUT_OF_RANGE exception message includes the stop id and train name. The SEGMENT_CHANGE_ERROR message includes the segment name and the station name. The tilde character is used as the string separator.
      • validateTime

        public boolean validateTime​(int checkStart,
                                    int checkDuration,
                                    int checkTime)
        Check to see if the supplied time is within the time range for the supplied schedule. If the duration is 24 hours, then all times are valid. Otherwise, we need to calculate the valid range, which can span midnight.
        Parameters:
        checkStart - The schedule start hour.
        checkDuration - The schedule duration.
        checkTime - The time value to be check.
        Returns:
        true if the time is valid.