Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1.     /**
  2.      * Generates a HashMap that represents the MTR network with the stations and
  3.      * all their immediate connections.
  4.      *
  5.      * @param lineMap
  6.      *            the lines and their respective stations
  7.      * @return a map of the stations and their respective Node object
  8.      */
  9.     public HashMap<String, Node<String, String>> generateNodesMap(Map<String, String[]> lineMap) {
  10.         // initialise map that stores all the station nodes, with the respective
  11.         // station's name as key
  12.         HashMap<String, Node<String, String>> nodesMap = new HashMap<String, Node<String, String>>(64);
  13.  
  14.         // iterate through every station line
  15.         for (Entry<String, String[]> pair : lineMap.entrySet()) {
  16.             // retrieve station line
  17.             String line = pair.getKey();
  18.             // retrieve stations
  19.             String[] stations = pair.getValue();
  20.  
  21.             // declare and initialise a variable to indicate if the current
  22.             // iteration is the first
  23.             boolean first = true;
  24.  
  25.             // store the node found in the previous iteration to be used in
  26.             // creating an edge
  27.             Node<String, String> prevNode = null;
  28.  
  29.             // iterate through the stations
  30.             Iterator<String> itr = Arrays.asList(stations).iterator();
  31.             while (itr.hasNext()) {
  32.                 // retrieve the next station
  33.                 String station = itr.next();
  34.  
  35.                 // retrieve already existing node of the station, or create a
  36.                 // new one
  37.                 Node<String, String> node;
  38.                 if (nodesMap.containsKey(station)) {
  39.                     node = nodesMap.get(station);
  40.                 } else {
  41.                     node = new Node<String, String>(station);
  42.                 }
  43.  
  44.                 // check if it is the first iteration
  45.                 if (first) {
  46.                     // ignore creating an edge between a (non-existent) previous
  47.                     // node and tell the subsequent iterations that the first
  48.                     // iteration has occurred
  49.                     first = false;
  50.                     // create edge between current node and previous node
  51.                 } else {
  52.                     // create a new edge between two nodes
  53.                     Edge<String, String> edge = new Edge<String, String>(prevNode, node, line);
  54.                     // add the generated edge to the two stations
  55.                     prevNode.addEdge(edge);
  56.                     node.addEdge(edge);
  57.                 }
  58.                 // add the station to the nodes map with the key as the
  59.                 // station's name
  60.                 nodesMap.put(station, node);
  61.  
  62.                 // store the previously modified station
  63.                 prevNode = node;
  64.             }
  65.         }
  66.  
  67.         return nodesMap;
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement