/**
   * Generates AdjacencyListGraph based on the values in the input file.
   *
   * @requires twitterStream is properly initialized
   * @param twitterStream initialized input stream for reading twitter file
   * @return generated graph based on input file
   */
  private static Graph readTwitterFile(FileInputStream twitterStream) {
    final int U1_INDEX = 0;
    final int U2_INDEX = 1;
    try {
      Graph g = new AdjacencyListGraph();
      BufferedReader twitterReader = new BufferedReader(new InputStreamReader(twitterStream));
      String line;
      while ((line = twitterReader.readLine()) != null) {

        // eliminate any unnecessary whitespace
        String[] columns = line.trim().replaceAll("\\s+", "").split("->");

        // first column is user 1
        // second column is user 2
        Vertex u1 = new Vertex(columns[U1_INDEX]);
        Vertex u2 = new Vertex(columns[U2_INDEX]);
        // System.out.println(columns[0]+","+columns[1]);
        g.addVertex(u1);
        g.addVertex(u2);
        g.addEdge(u1, u2);
        // System.out.println(line);
      }
      twitterReader.close();
      return g;
    } catch (Exception e) { // if something somehow goes wrong
      throw new RuntimeException(e);
    }
  }
  /**
   * Helper method for parseQuery. Writes the results of the queries to the output file.
   *
   * @param output initialized BufferedWriter for the output file
   * @param g initialized graph of edges and vertices
   * @param u1 one vertex used in commands
   * @param u2 another vertex used in commands
   * @param command command to execute
   */
  private static void printResults(
      BufferedWriter output, Graph g, Vertex u1, Vertex u2, String command) {
    final String commonInfluencers = "commonInfluencers";
    final String numRetweets = "numRetweets";

    final String VERTEX_NOT_FOUND_ERROR =
        "ERROR: One or more vertices does not exist in the graph.";
    final String INVALID_COMMAND_ERROR = "\tError: invalid command ";
    final String PATH_NOT_FOUND_ERROR = "\tPath not found.";

    List<Vertex> allVertices = new ArrayList<Vertex>(g.getVertices());

    try {
      output.write("query: " + command + " " + u1.toString() + " " + u2.toString());
      output.newLine();
      output.write("<result>");
      output.newLine();

      // check if vertices exist in graph
      if (!allVertices.contains(u1) || !allVertices.contains(u2)) {
        output.write(VERTEX_NOT_FOUND_ERROR);
        output.newLine();
        output.write("</result>");
        output.newLine();
        output.newLine();
        return;
      }
      // if query is commonInfluencers
      if (command.equals(commonInfluencers)) {
        List<Vertex> commonFollowers =
            new LinkedList<Vertex>(Algorithms.commonDownstreamVertices(g, u1, u2));
        for (Vertex v : commonFollowers) {
          output.write("\t" + v.toString());
          output.newLine();
        }
      }

      // if query is numRetweets
      else if (command.equals(numRetweets)) {
        // note switch in u1 and u2; this is because tweets go upstream
        int distance = Algorithms.shortestDistance(g, u2, u1);

        if (distance == -1) {
          output.write(PATH_NOT_FOUND_ERROR);
        } else {
          // implicitly convert distance to string as printing out ints somehow didn't work
          output.write("" + distance);
          // System.out.println(distance);
        }

        output.newLine();
      } else {
        output.write(INVALID_COMMAND_ERROR + command);
        output.newLine();
      }
      output.write("</result>");
      output.newLine();
      output.newLine();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }