public static Map<MavenProject, String> mapModuleNames(MavenProjectsTree tree,
                                                         Collection<MavenProject> projects,
                                                         Map<VirtualFile, String> existingPomModuleName) {
    var mavenProjectToModuleName = new HashMap<MavenProject, String>();
    NameItem[] names = new NameItem[projects.size()];

    int i = 0;
    for (MavenProject each : projects) {
      names[i++] = new NameItem(tree, each, existingPomModuleName.get(each.getFile()));
    }

    Arrays.sort(names);

    Map<String, Integer> nameCountersLowerCase = new HashMap<>();

    for (i = 0; i < names.length; i++) {
      if (names[i].hasDuplicatedGroup) continue;

      for (int k = i + 1; k < names.length; k++) {
        // IDEA-320329 check should be non-case-sensitive
        if (names[i].originalName.equalsIgnoreCase(names[k].originalName)) {
          nameCountersLowerCase.put(names[i].originalName.toLowerCase(ROOT), 0);

          if (names[i].groupId.equals(names[k].groupId)) {
            names[i].hasDuplicatedGroup = true;
            names[k].hasDuplicatedGroup = true;
          }
        }
      }
    }

    Set<String> existingNames = new HashSet<>();

    for (NameItem name : names) {
      if (name.existingName != null) {
        existingNames.add(name.getResultName());
      }
    }

    for (NameItem nameItem : names) {
      if (nameItem.existingName == null) {

        Integer c = nameCountersLowerCase.get(nameItem.originalName.toLowerCase(ROOT));

        if (c != null) {
          nameItem.number = c;
          nameCountersLowerCase.put(nameItem.originalName.toLowerCase(ROOT), c + 1);
        }

        do {
          String name = nameItem.getResultName();
          if (existingNames.add(name)) break;

          nameItem.number++;
          nameCountersLowerCase.put(nameItem.originalName.toLowerCase(ROOT), nameItem.number + 1);
        }
        while (true);
      }
    }

    for (NameItem each : names) {
      mavenProjectToModuleName.put(each.project, each.getResultName());
    }

    return mavenProjectToModuleName;
  }
public static Map<MavenProject, String> mapModuleNames(MavenProjectsTree tree,
                                                         Collection<MavenProject> projects,
                                                         Map<VirtualFile, String> existingPomModuleName)