public CallableFunction select()

in runtime/src/main/java/com/google/cloud/verticals/foundations/dataharmonization/function/DefaultOverloadSelector.java [199:252]


  public CallableFunction select(List<CallableFunction> overloadGroup, Data[] args) {
    if (overloadGroup == null || overloadGroup.isEmpty()) {
      throw new NoMatchingOverloadsException();
    }

    // If there is only one option, then it's not really an overload.
    if (overloadGroup.size() == 1) {
      double dist = distance(overloadGroup.get(0).getSignature(), args);
      if (Double.isInfinite(dist)) {
        throw new NoMatchingOverloadsException(overloadGroup.get(0), args);
      }

      return overloadGroup.get(0);
    }

    // Find the closest matching distance.
    CallableFunction closest = null;
    double minDistance = Double.POSITIVE_INFINITY;
    boolean multipleClosest = true;

    for (CallableFunction candidate : overloadGroup) {
      double distance = distance(candidate.getSignature(), args);
      if (Double.isInfinite(distance)) {
        continue;
      }

      if (Double.isInfinite(minDistance) || distance < minDistance) {
        minDistance = distance;
        closest = candidate;
        multipleClosest = false;
      } else if (minDistance == distance) {
        multipleClosest = true;
      }
    }

    // Fail if closest is infinite, i.e. mismatch.
    if (Double.isInfinite(minDistance)) {
      throw new NoMatchingOverloadsException(overloadGroup, args);
    }

    // If multiple closest matches, then we only find them now to save on performance during initial
    // search.
    if (multipleClosest) {
      double finalMinDistance = minDistance;
      ImmutableList<CallableFunction> allClosest =
          overloadGroup.stream()
              .filter(c -> distance(c.getSignature(), args) == finalMinDistance)
              .collect(toImmutableList());

      throw new MultipleMatchingOverloadsException(allClosest, args);
    }

    return closest;
  }