private static Predicate validateQuery()

in java/com/google/gerrit/plugins/checks/api/QueryPendingChecks.java [169:203]


  private static Predicate<Check> validateQuery(Predicate<Check> predicate)
      throws BadRequestException {
    int numCheckPredicates = countPredicates(predicate, CheckerPredicate.class);
    int numSchemePredicates = countPredicates(predicate, CheckerSchemePredicate.class);
    String exceptionMessage =
        String.format(
            "query must be '%s:<checker-uuid>' or '%s:<checker-uuid> AND <other-operators>' or '%s:<checker-scheme>' or '%s:<checker-scheme> AND <other-operators>'",
            CheckQueryBuilder.FIELD_CHECKER,
            CheckQueryBuilder.FIELD_CHECKER,
            CheckQueryBuilder.FIELD_SCHEME,
            CheckQueryBuilder.FIELD_SCHEME);
    if (numCheckPredicates + numSchemePredicates != 1) {
      throw new BadRequestException(
          String.format(
              "query must contain exactly 1 '%s' operator or '%s' operator",
              CheckQueryBuilder.FIELD_CHECKER, CheckQueryBuilder.FIELD_SCHEME));
    }

    // the root predicate must either be an AndPredicate ....
    if (predicate instanceof AndPredicate) {
      // if the root predicate is an AndPredicate, any of its direct children must be a
      // CheckerPredicate or CheckerSchemePredicate, the other child predicates can be anything
      // (including any combination of
      // AndPredicate, OrPredicate and NotPredicate).
      if (predicate.getChildren().stream().noneMatch(CheckerPredicate.class::isInstance)
          && predicate.getChildren().stream().noneMatch(CheckerSchemePredicate.class::isInstance)) {
        throw new BadRequestException(exceptionMessage);
      }
      // ... or a CheckerPredicate / CheckerSchemePredicate
    } else if (!(predicate instanceof CheckerPredicate
        || predicate instanceof CheckerSchemePredicate)) {
      throw new BadRequestException(exceptionMessage);
    }
    return predicate;
  }