public static GroovyResolveResult[] filterSameSignatureCandidates(Collection<? extends GroovyResolveResult> candidates) {
    if (candidates.size() == 0) return GroovyResolveResult.EMPTY_ARRAY;
    if (candidates.size() == 1) return candidates.toArray(GroovyResolveResult.EMPTY_ARRAY);

    final List<GroovyResolveResult> result = new ArrayList<>();

    final Iterator<? extends GroovyResolveResult> allIterator = candidates.iterator();

    Map<String, List<GroovyResolveResult>> cache = new HashMap<>(candidates.size());

    while (allIterator.hasNext()) {
      final GroovyResolveResult currentResult = allIterator.next();

      final PsiMethod currentMethod;
      final PsiSubstitutor currentSubstitutor;
      if (currentResult instanceof GroovyMethodResult currentMethodResult) {
        currentMethod = currentMethodResult.getElement();
        currentSubstitutor = currentMethodResult.getContextSubstitutor();
      }
      else if (currentResult.getElement() instanceof PsiMethod) {
        currentMethod = (PsiMethod)currentResult.getElement();
        currentSubstitutor = currentResult.getSubstitutor();
      }
      else {
        result.add(currentResult);
        continue;
      }

      boolean isDominated = false;

      List<GroovyResolveResult> existingCandidates = cache.computeIfAbsent(getKey(currentMethod), (__) -> new SmartList<>());
      for (Iterator<GroovyResolveResult> iterator = existingCandidates.listIterator(); iterator.hasNext();) {
        GroovyResolveResult candidateResult = iterator.next();
        final PsiMethod otherMethod;
        final PsiSubstitutor otherSubstitutor;
        if (candidateResult instanceof GroovyMethodResult otherMethodResult) {
          otherMethod = otherMethodResult.getElement();
          otherSubstitutor = otherMethodResult.getContextSubstitutor();
        }
        else if (candidateResult.getElement() instanceof PsiMethod) {
          otherMethod = (PsiMethod)candidateResult.getElement();
          otherSubstitutor = candidateResult.getSubstitutor();
        } else {
          continue;
        }
        if (dominated(currentMethod, currentSubstitutor, otherMethod, otherSubstitutor)) {
          // if current method is dominated by other method
          // then do not add current method to result and skip rest other methods
          isDominated = true;
          break;
        }
        else if (dominated(otherMethod, otherSubstitutor, currentMethod, currentSubstitutor)) {
          // if other method is dominated by current method
          // then remove other from result
          iterator.remove();
        }
      }
      if (!isDominated) {
        existingCandidates.add(currentResult);
      }
    }

    for (List<GroovyResolveResult> resultsByName : cache.values()) {
      result.addAll(resultsByName);
    }

    return result.toArray(GroovyResolveResult.EMPTY_ARRAY);
  }
public static GroovyResolveResult[] filterSameSignatureCandidates(Collection<? extends GroovyResolveResult> candidates)