public static ArrayList replace()

in tools/query_breakdown/src/main/java/com/google/bigquery/ReplacementLogic.java [22:64]


  public static ArrayList<String> replace (String component, int replacementLimit,
      ArrayList<String> options) {
    ArrayList<String> result = new ArrayList<>();

    /*
     * The component field in the input arguments of this function denotes the component that the
     * tool replaces from. Therefore, we use that information to determine good replacements if any.
     * For instance, in the example code below, we deem that A AS is a good replacement for AS in
     * cases where we have a AS just immediately following the previous WITH:
     *
     * if (component.equals("AS")) { result.add("A AS"); return result; }
     */

    // returns the filtered list if the number of possible replacements is smaller than the limit
    if (options.size() <= replacementLimit) {
      for (int i = 0; i < options.size(); i++) {
        // gets rid of cases such as <QUOTED_STRING>
        if (options.get(i).charAt(0) != '<' || options.get(i).length() <= 1) {
          result.add(options.get(i));
        }
      }
      return result;
    }

    // field to make sure we don't have duplicates in the result
    HashSet<Integer> seen = new HashSet<>();

    // randomly populate result until full
    while (result.size() < replacementLimit && seen.size() < options.size()) {
      int random = randomNumber(options.size() - 1);
      if (seen.contains(random)) {
        continue;
      }
      else if (options.get(random).charAt(0) == '<' && options.get(random).length() > 1) {
        seen.add(random);
      }
      else {
        result.add(options.get(random));
        seen.add(random);
      }
    }
    return result;
  }