// Copyright 2019 The Bazel Authors. All rights reserved.
//
// 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.devtools.build.lib.query2;

import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/** Encapsulate the action filters parsed from aquery command. */
public class AqueryActionFilter {
  // TODO(leba): Use Enum for list of filters.
  private final ImmutableMap<String, List<Pattern>> filterMap;

  private AqueryActionFilter(Builder builder) {
    filterMap = ImmutableMap.copyOf(builder.filterMap);
  }

  public static AqueryActionFilter emptyInstance() {
    return builder().build();
  }

  public static Builder builder() {
    return new Builder();
  }

  public boolean hasFilterForFunction(String function) {
    return filterMap.containsKey(function);
  }

  /**
   * Returns whether the input string matches ALL the filter patterns of a specific type parsed from
   * aquery command.
   *
   * @param function the name of the aquery function (inputs, outputs, mnemonic)
   * @param input the string to be matched against
   */
  public boolean matchesAllPatternsForFunction(String function, String input) {
    if (!hasFilterForFunction(function)) {
      return false;
    }

    return filterMap.get(function).stream()
        .map(pattern -> pattern.matcher(input).matches())
        .reduce(true, Boolean::logicalAnd);
  }

  /** Builder class for {@code AqueryActionFilter} */
  public static class Builder {
    private final Map<String, List<Pattern>> filterMap;

    public Builder() {
      filterMap = new HashMap<>();
    }

    public Builder put(String key, Pattern value) {
      filterMap.putIfAbsent(key, new ArrayList<>());
      filterMap.get(key).add(value);

      return this;
    }

    public AqueryActionFilter build() {
      return new AqueryActionFilter(this);
    }
  }
}

@@ -688,26 +688,26 @@
 collect.
-ImmutableM
+HashMultim
 ap;%0Aimpo
@@ -713,96 +713,92 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.M
+com.google.common.collect.ImmutableMultimap;%0Aimport com.google.common.collect.Multim
 ap;%0A
@@ -1003,16 +1003,21 @@
 mutableM
+ultim
 ap%3CStrin
@@ -1015,37 +1015,32 @@
 map%3CString, 
-List%3C
 Pattern%3E
 %3E filterMap;
@@ -1019,33 +1019,32 @@
 String, Pattern%3E
-%3E
  filterMap;%0A%0A  p
@@ -1084,24 +1084,24 @@
  builder) %7B%0A
-
     filterMa
@@ -1114,16 +1114,21 @@
 mutableM
+ultim
 ap.copyO
@@ -2092,16 +2092,21 @@
  final M
+ultim
 ap%3CStrin
@@ -2112,21 +2112,16 @@
 ng, 
-List%3C
 Pattern%3E
 %3E fi
@@ -2116,17 +2116,16 @@
 Pattern%3E
-%3E
  filterM
@@ -2173,22 +2173,28 @@
 ap =
- new
  HashM
-ap%3C%3E
+ultimap.create
 ();%0A
@@ -2275,79 +2275,22 @@
 .put
-IfAbsent(key, new ArrayList%3C%3E());%0A      filterMap.get(key).add(
+(key, 
 value);%0A
 %0A   
@@ -2285,17 +2285,16 @@
 value);%0A
-%0A
       re

Use MultiMap for AqueryActionFilter