private static String findBestCandidateForMove(List<String> paths, String path, Set<String> optional) {
    if (paths == null) return null;

    boolean mandatory = !optional.contains(path);
    String best = "";

    String[] dirs = path.split("/");
    int common = 0;
    for (String other : paths) {
      if (mandatory && optional.contains(other)) continue;  // mandatory targets must not use optional sources
      String[] others = other.split("/");
      for (int i = 0; i < dirs.length && i < others.length; i++) {
        if (dirs[dirs.length - i - 1].equals(others[others.length - i - 1])) {
          if (i + 1 > common) {
            best = other;
            common = i + 1;
          }
          // check root folders of candidates with the same matches
          else if (i + 1 == common && compareRootFolders(dirs, best.split("/")) < compareRootFolders(dirs, other.split("/"))) {
            best = other;
          }
        }
        else {
          break;
        }
      }
    }

    return !best.isEmpty() ? best : null;
  }
private static String findBestCandidateForMove(List<String> paths, String path, Set<String> optional)