class RuleWithMaxFilter implements RuleMatchFilter {

  @Override
  public final List<RuleMatch> filter(List<RuleMatch> ruleMatches) {<FILL_FUNCTION_BODY>}

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

  private boolean haveSameRule(RuleMatch match, RuleMatch nextMatch) {
    if (!(match.getRule() instanceof AbstractPatternRule) || !(nextMatch.getRule() instanceof AbstractPatternRule)) {
      return false;
    }
    String id1 = match.getRule().getId();
    String subId1 = ((AbstractPatternRule) match.getRule()).getSubId();
    String subId2 = ((AbstractPatternRule) nextMatch.getRule()).getSubId();
    if (subId1 == null &&  subId2 != null) {
      return false;
    }
    if (subId1 != null && subId2 == null) {
      return false;
    }
    return id1 != null && id1.equals(nextMatch.getRule().getId()) &&
        (subId1 == null && subId2 == null || subId1 != null && subId1.equals(subId2));
  }

}

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