question: protected void checkIntegrity ( ) { PrintStream errors = System . err ; Hashtable otherIntersections = new Hashtable ( ) ; Object bothIntersections = new Object ( ) ; Object inRoadsVector = new Object ( ) ; for ( Enumeration e = intersections . elements ( ) ; e . hasMoreElements ( ) ; ) { Intersection intersection = ( Intersection ) e . nextElement ( ) ; int nRoads = intersection . getRoadCount ( ) ; if ( nRoads == 0 ) { errors . println ( "Dangling intersection" ) ; errors . println ( "  Intersection = " + intersection ) ; continue ; } for ( int i = 0 ; i < nRoads ; i ++ ) { Road road = intersection . getRoad ( i ) ; Object other = otherIntersections . get ( road ) ; if ( other == null ) { otherIntersections . put ( road , road . getOtherIntersection ( intersection ) ) ; } else if ( other == intersection ) { otherIntersections . put ( road , bothIntersections ) ; } else { errors . println ( "Misconnected" ) ; errors . println ( "          Road = " + road ) ; errors . println ( "    Road.Other = " + other ) ; errors . println ( "  Intersection = " + intersection ) ; } } } for ( Enumeration e = roads . elements ( ) ; e . hasMoreElements ( ) ; ) { Road road = ( Road ) e . nextElement ( ) ; Object other = otherIntersections . get ( road ) ; if ( other == null ) { errors . println ( "Road not found in intersections" ) ; errors . println ( "          Road = " + road ) ; } else if ( other != bothIntersections ) { errors . println ( "Road incompletely connected" ) ; errors . println ( "          Road = " + road ) ; errors . println ( "    Road.Other = " + other ) ; } else if ( other == inRoadsVector ) { errors . println ( "Road doubly listed" ) ; errors . println ( "          Road = " + road ) ; } otherIntersections . put ( road , inRoadsVector ) ; } for ( Enumeration e = otherIntersections . keys ( ) ; e . hasMoreElements ( ) ; ) { Road road = ( Road ) e . nextElement ( ) ; Object other = otherIntersections . get ( road ) ; if ( other != inRoadsVector ) { errors . println ( "Road not listed" ) ; errors . println ( "          Road = " + road ) ; } } }
target:  Check the integrity of our data structures. Scan the known intersections. Note intersections with no roads. Scan the roads of the intersection: Each road has two intersections. If the road has already been encountered, then we recorded its "other" intersection and that must match this intersection. If it doesn't match, record an error. If it does match reset its recorded other intersection to be a special marker indicating that both ends of the road have been accounted for. If the road has not already been encountered, then record its "other" intersection. Scan the known roads. Every road should accounted for in the "other" intersection table and should be marked as having both intersections accounted for. Note the roads which were not found in the first scan and the roads which were found, but for which both intersections were not found. Remark every road. Finally scan the other intersection table for entries which were not marked as being in the roads vector.