class BigQueryStrings {

  public static TableReference parseTableReference(String tableRefString) {
    // Logic mirrored from cloud/helix/clients/cli/bigquery_client.py.
    TableReference tableRef = new TableReference();
    int projectIdEnd = tableRefString.lastIndexOf(':');
    String datasetAndTableString = tableRefString;
    if (projectIdEnd != -1) {
      tableRef.setProjectId(tableRefString.substring(0, projectIdEnd));

      // Omit the ':' from the remaining datasetId.tableId substring.
      datasetAndTableString = tableRefString.substring(projectIdEnd + 1);
    }

    Preconditions.checkArgument(datasetAndTableString.matches(DATASET_AND_TABLE_REGEX),
        "Invalid datasetAndTableString '%s'; must match regex '%s'.",
        datasetAndTableString, DATASET_AND_TABLE_REGEX);

    List<String> idParts = DOT_SPLITTER.splitToList(datasetAndTableString);
    tableRef.setDatasetId(idParts.get(0));
    tableRef.setTableId(idParts.get(1));
    return tableRef;
  }

}