Support IsOperands in change queries from plugins

Plugins currently can add search operators and "has" operands to change
queries. Now support "is" operands using a "is:<operand>_<plugin>"
syntax very similar to the "has" syntax.

Plugins can achieve this by implementing ChangeIsOperandFactory and
returning a Predicate<ChangeData>. A sample ChangeIsOpenrandFactory
class implementing, and registering, a new is:sample_pluginName
operand is shown below:

  public class SampleIsOperand implements ChangeIsOperandFactory {
    public static class Module extends AbstractModule {
      @Override
      protected void configure() {
        bind(ChangeIsOperandFactory.class)
            .annotatedWith(Exports.named("sample")
            .to(SampleIsOperand.class);
      }
    }

    @Override
    public Predicate<ChangeData> create(ChangeQueryBuilder builder){
      return new IsSamplePredicate();
    }
  }

Change-Id: Ic694673be806e23a294e0a30cb3bb1b7e59b46f8
diff --git a/Documentation/dev-plugins.txt b/Documentation/dev-plugins.txt
index c3df396..a773276 100644
--- a/Documentation/dev-plugins.txt
+++ b/Documentation/dev-plugins.txt
@@ -742,7 +742,8 @@
 Plugin methods implementing search operands (returning a
 `Predicate<ChangeData>`), must be defined on a class implementing
 one of the `ChangeQueryBuilder.ChangeOperandsFactory` interfaces
-(.e.g., ChangeQueryBuilder.ChangeHasOperandFactory).  The specific
+(.e.g., ChangeQueryBuilder.ChangeHasOperandFactory or
+ChangeQueryBuilder.ChangeIsOperandFactory).  The specific
 `ChangeOperandFactory` class must also be bound to the `DynamicSet` from
 a module's `configure()` method in the plugin.
 
diff --git a/java/com/google/gerrit/server/config/GerritGlobalModule.java b/java/com/google/gerrit/server/config/GerritGlobalModule.java
index c7a4a9a..21a1ba0 100644
--- a/java/com/google/gerrit/server/config/GerritGlobalModule.java
+++ b/java/com/google/gerrit/server/config/GerritGlobalModule.java
@@ -433,6 +433,7 @@
     DynamicMap.mapOf(binder(), DynamicOptions.DynamicBean.class);
     DynamicMap.mapOf(binder(), ChangeQueryBuilder.ChangeOperatorFactory.class);
     DynamicMap.mapOf(binder(), ChangeQueryBuilder.ChangeHasOperandFactory.class);
+    DynamicMap.mapOf(binder(), ChangeQueryBuilder.ChangeIsOperandFactory.class);
     DynamicSet.setOf(binder(), ChangeAttributeFactory.class);
 
     install(new GitwebConfig.LegacyModule(cfg));
diff --git a/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java b/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java
index c9ae126..464ba81 100644
--- a/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java
+++ b/java/com/google/gerrit/server/query/change/ChangeQueryBuilder.java
@@ -117,12 +117,14 @@
    * <p>bind(ChangeHasOperandFactory.class) .annotatedWith(Exports.named("your has operand"))
    * .to(YourClass.class);
    */
-  private interface ChangeOperandFactory {
+  public interface ChangeOperandFactory {
     Predicate<ChangeData> create(ChangeQueryBuilder builder) throws QueryParseException;
   }
 
   public interface ChangeHasOperandFactory extends ChangeOperandFactory {}
 
+  public interface ChangeIsOperandFactory extends ChangeOperandFactory {}
+
   private static final Pattern PAT_LEGACY_ID = Pattern.compile("^[1-9][0-9]*$");
   private static final Pattern PAT_CHANGE_ID = Pattern.compile(CHANGE_ID_PATTERN);
   private static final Pattern DEF_CHANGE =
@@ -218,6 +220,7 @@
     final CommentsUtil commentsUtil;
     final ConflictsCache conflictsCache;
     final DynamicMap<ChangeHasOperandFactory> hasOperands;
+    final DynamicMap<ChangeIsOperandFactory> isOperands;
     final DynamicMap<ChangeOperatorFactory> opFactories;
     final GitRepositoryManager repoManager;
     final GroupBackend groupBackend;
@@ -244,6 +247,7 @@
         ChangeIndexRewriter rewriter,
         DynamicMap<ChangeOperatorFactory> opFactories,
         DynamicMap<ChangeHasOperandFactory> hasOperands,
+        DynamicMap<ChangeIsOperandFactory> isOperands,
         IdentifiedUser.GenericFactory userFactory,
         Provider<CurrentUser> self,
         PermissionBackend permissionBackend,
@@ -273,6 +277,7 @@
           rewriter,
           opFactories,
           hasOperands,
+          isOperands,
           userFactory,
           self,
           permissionBackend,
@@ -304,6 +309,7 @@
         ChangeIndexRewriter rewriter,
         DynamicMap<ChangeOperatorFactory> opFactories,
         DynamicMap<ChangeHasOperandFactory> hasOperands,
+        DynamicMap<ChangeIsOperandFactory> isOperands,
         IdentifiedUser.GenericFactory userFactory,
         Provider<CurrentUser> self,
         PermissionBackend permissionBackend,
@@ -351,6 +357,7 @@
       this.starredChangesUtil = starredChangesUtil;
       this.accountCache = accountCache;
       this.hasOperands = hasOperands;
+      this.isOperands = isOperands;
       this.groupMembers = groupMembers;
       this.changeIsVisbleToPredicateFactory = changeIsVisbleToPredicateFactory;
       this.operatorAliasConfig = operatorAliasConfig;
@@ -364,6 +371,7 @@
           rewriter,
           opFactories,
           hasOperands,
+          isOperands,
           userFactory,
           Providers.of(otherUser),
           permissionBackend,
@@ -643,6 +651,14 @@
       throw new QueryParseException("'is:wip' operator is not supported by change index version");
     }
 
+    // for plugins the value will be operandName_pluginName
+    List<String> names = Lists.newArrayList(Splitter.on('_').split(value));
+    if (names.size() == 2) {
+      ChangeIsOperandFactory op = args.isOperands.get(names.get(1), names.get(0));
+      if (op != null) {
+        return op.create(this);
+      }
+    }
     return status(value);
   }
 
diff --git a/javatests/com/google/gerrit/acceptance/api/change/PluginOperatorsIT.java b/javatests/com/google/gerrit/acceptance/api/change/PluginOperatorsIT.java
new file mode 100644
index 0000000..68307cf
--- /dev/null
+++ b/javatests/com/google/gerrit/acceptance/api/change/PluginOperatorsIT.java
@@ -0,0 +1,102 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.acceptance.api.change;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.extensions.annotations.Exports;
+import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.extensions.restapi.AuthException;
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.TopLevelResource;
+import com.google.gerrit.index.query.Matchable;
+import com.google.gerrit.index.query.OperatorPredicate;
+import com.google.gerrit.index.query.Predicate;
+import com.google.gerrit.index.query.QueryParseException;
+import com.google.gerrit.server.permissions.PermissionBackendException;
+import com.google.gerrit.server.query.change.ChangeData;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.restapi.change.QueryChanges;
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import java.util.List;
+import org.junit.Test;
+
+public class PluginOperatorsIT extends AbstractDaemonTest {
+  @Inject private Provider<QueryChanges> queryChangesProvider;
+
+  @Test
+  public void getChangeWithIsOperator() throws Exception {
+    QueryChanges queryChanges = queryChangesProvider.get();
+    queryChanges.addQuery("is:changeNumberEven_myplugin");
+
+    String oddChangeId = createChange().getChangeId();
+    String evenChangeId = createChange().getChangeId();
+    assertThat(getChanges(queryChanges)).hasSize(0);
+
+    try (AutoCloseable ignored = installPlugin("myplugin", IsOperatorModule.class)) {
+      List<ChangeInfo> changes = getChanges(queryChanges);
+      assertThat(changes).hasSize(1);
+
+      String outputChangeId = ((ChangeInfo) changes.get(0)).changeId;
+      assertThat(outputChangeId).isEqualTo(evenChangeId);
+      assertThat(outputChangeId).isNotEqualTo(oddChangeId);
+    }
+
+    assertThat(getChanges(queryChanges)).hasSize(0);
+  }
+
+  protected static class IsOperatorModule extends AbstractModule {
+    @Override
+    public void configure() {
+      bind(ChangeQueryBuilder.ChangeIsOperandFactory.class)
+          .annotatedWith(Exports.named("changeNumberEven"))
+          .to(SampleIsOperand.class);
+    }
+  }
+
+  private static class SampleIsOperand implements ChangeQueryBuilder.ChangeIsOperandFactory {
+    @Override
+    public Predicate<ChangeData> create(ChangeQueryBuilder builder) throws QueryParseException {
+      return new IsSamplePredicate();
+    }
+  }
+
+  private static class IsSamplePredicate extends OperatorPredicate<ChangeData>
+      implements Matchable<ChangeData> {
+
+    public IsSamplePredicate() {
+      super("is", "changeNumberEven");
+    }
+
+    @Override
+    public boolean match(ChangeData changeData) {
+      int id = changeData.getId().get();
+      return id % 2 == 0;
+    }
+
+    @Override
+    public int getCost() {
+      return 0;
+    }
+  }
+
+  private List<ChangeInfo> getChanges(QueryChanges queryChanges)
+      throws AuthException, PermissionBackendException, BadRequestException {
+    return (List<ChangeInfo>) queryChanges.apply(TopLevelResource.INSTANCE).value();
+  }
+}
diff --git a/javatests/com/google/gerrit/server/index/change/FakeQueryBuilder.java b/javatests/com/google/gerrit/server/index/change/FakeQueryBuilder.java
index a936d28..521af2f 100644
--- a/javatests/com/google/gerrit/server/index/change/FakeQueryBuilder.java
+++ b/javatests/com/google/gerrit/server/index/change/FakeQueryBuilder.java
@@ -44,6 +44,7 @@
             null,
             null,
             null,
+            null,
             indexes,
             null,
             null,