class SameRuleGroupFilter implements RuleMatchFilter {

  /**
   * @param ruleMatches list of matches
   */
  @Override
  public List<RuleMatch> filter(List<RuleMatch> ruleMatches) {<FILL_FUNCTION_BODY>}

  private boolean overlapAndMatch(RuleMatch match, RuleMatch nextMatch) {
    return overlaps(match, nextMatch) && haveSameRuleGroup(match, nextMatch);
  }

  boolean overlaps(RuleMatch match, RuleMatch nextMatch) {
    if (match.getFromPos() <= nextMatch.getToPos() && match.getToPos() >= nextMatch.getFromPos()) {
      return true;
    }
    return false;
  }

  private boolean haveSameRuleGroup(RuleMatch match, RuleMatch nextMatch) {
    String id1 = match.getRule().getId();
    return id1 != null && id1.equals(nextMatch.getRule().getId());
  }

}

    Collections.sort(ruleMatches);
    List<RuleMatch> filteredRules = new ArrayList<>();
    for (int i = 0; i < ruleMatches.size(); i++) {
      RuleMatch match = ruleMatches.get(i);
      while (i < ruleMatches.size() - 1 && overlapAndMatch(match, ruleMatches.get(i + 1))) {
        i++;  // skip next match
      }
      filteredRules.add(match);
    }
    return filteredRules;
  
<no_super_class>