Changeset 90 in tmcsimulator for trunk


Ignore:
Timestamp:
10/10/2017 04:35:30 PM (9 years ago)
Author:
jdalbey
Message:

Station.java - DIRECTION enum shortened by using existing enum methods "values()" and "toString()".

Location:
trunk/src/atmsdriver
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/atmsdriver/NetworkLoader.java

    r84 r90  
    228228        sc.nextInt(); // skip count 
    229229        sc.nextInt(); // fwy 
    230         DIRECTION dir = DIRECTION.getEnum(sc.next()); // direction 
     230        DIRECTION dir = DIRECTION.toDirection(sc.next()); // direction 
    231231        sc.nextDouble(); 
    232232         
     
    261261                scLine.nextInt(); // skip count 
    262262                int fwy = scLine.nextInt(); // fwy 
    263                 DIRECTION dir = DIRECTION.getEnum(scLine.next()); // direction 
     263                DIRECTION dir = DIRECTION.toDirection(scLine.next()); // direction 
    264264                double postmile = scLine.nextDouble(); 
    265265                String ldsName = getLocation(strLine); /************* DOESNT GRAB WHOLE???? *///// 
  • trunk/src/atmsdriver/model/Highways.java

    r89 r90  
    102102            DIRECTION hwyDir = hwyKey.get(hwyNum); 
    103103            Collections.sort(highwayTable.get(hwyKey)); 
    104             System.out.println("Adding highway: " + hwyNum + " " + hwyDir.name); 
     104            System.out.println("Adding highway: " + hwyNum + " " + hwyDir.toString().charAt(0)); 
    105105            highways.add(new Highway(hwyNum,  
    106106                    hwyDir, hwyStations)); 
  • trunk/src/atmsdriver/model/Station.java

    r88 r90  
    2525    final private String location; 
    2626    final private List<LoopDetector> loops; 
    27     final private int freeway; 
     27    final private int highwayNum; 
    2828    final private double postmile; 
    2929    final private DIRECTION direction; 
     
    3535    /* Constructor */ 
    3636    public Station(int lineNum, int ldsID, int drop, 
    37             String location, List<LoopDetector> loops, int fwy, 
     37            String location, List<LoopDetector> loops, int hwy, 
    3838            DIRECTION direction, double postmile) { 
    3939        this.lineNum = lineNum; 
     
    4444        this.postmile = postmile; 
    4545        this.direction = direction; 
    46         this.freeway = fwy; 
     46        this.highwayNum = hwy; 
    4747 
    4848        this.MLTotVol = 0; 
     
    5252    public int getHighwayNumber() 
    5353    { 
    54         return this.freeway; 
     54        return this.highwayNum; 
    5555    } 
    5656     
     
    7373        build.append(Integer.toString(this.drop)); 
    7474        build.append(" "); 
    75         build.append(Integer.toString(this.freeway)); 
    76         build.append(" "); 
    77         build.append(this.direction.name); 
     75        build.append(Integer.toString(this.highwayNum)); 
     76        build.append(" "); 
     77        build.append(this.direction.getLetter()); 
    7878        build.append(" "); 
    7979        build.append(Double.toString(this.postmile)); 
     
    9393    } 
    9494 
     95    /** Compare this Station to another by postmile. 
     96     * Note: This might be better as a Comparator since it checks only one field. 
     97     */ 
    9598    @Override 
    9699    public int compareTo(Object otherStation) { 
     100        // check for identity 
     101        if (this == otherStation) return 0; 
    97102        // check that Object is of type Station, if not throw exception 
    98103        if (!(otherStation instanceof Station)) { 
     
    163168 
    164169        Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag); 
    165         directionElement.appendChild(theDoc.createTextNode(this.direction.name)); 
     170        directionElement.appendChild(theDoc.createTextNode(""+this.direction.getLetter())); 
    166171        stationElement.appendChild(directionElement); 
    167172 
    168173        Element freewayElement = theDoc.createElement(XML_TAGS.FREEWAY.tag); 
    169         freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.freeway))); 
     174        freewayElement.appendChild(theDoc.createTextNode(String.valueOf(this.highwayNum))); 
    170175        stationElement.appendChild(freewayElement); 
    171176 
     
    194199    public static enum DIRECTION { 
    195200 
    196         NORTH("N"), 
    197         SOUTH("S"), 
    198         EAST("E"), 
    199         WEST("W"); 
    200  
    201         String name; 
    202  
    203         DIRECTION(String name) { 
    204             this.name = name; 
    205         } 
    206  
     201        NORTH, 
     202        SOUTH, 
     203        EAST, 
     204        WEST; 
     205 
     206        // All the first letters of the values, in order. 
     207        private static String allLetters = "NSEW"; 
     208 
     209        /** Return the first letter of this enum. 
     210         *  
     211         * @return String first letter of this enum. 
     212         */ 
     213        public String getLetter() 
     214        { 
     215            return this.toString().substring(0,1); 
     216        } 
     217         
    207218        /** 
    208          * Returns the direction enum, given a string value. 
     219         * Returns a direction given its first character. 
    209220         * 
    210          * @param name str value for enum 
    211          * @return enum for given str value 
     221         * @param letter the first character of a direction 
     222         * @return direction corresponding to letter 
     223         * @pre letter must be one of allLetters 
    212224         */ 
    213         public static DIRECTION getEnum(String name) { 
    214             switch (name) { 
    215                 case "S": 
    216                     return SOUTH; 
    217                 case "N": 
    218                     return NORTH; 
    219                 case "E": 
    220                     return EAST; 
    221                 case "W": 
    222                     return WEST; 
    223                 default: 
    224                     return null; 
    225             } 
    226         } 
     225        public static DIRECTION toDirection(String letter) 
     226        { 
     227            return values()[allLetters.indexOf(letter.charAt(0))]; 
     228        } 
     229         
    227230    } 
    228231} 
Note: See TracChangeset for help on using the changeset viewer.