aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/packages/EnumFilterConverter.java
blob: 43dff04a28411944505265b41a4ece4235f143ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2014 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.packages;

import com.google.devtools.build.lib.util.StringUtil;
import com.google.devtools.common.options.Converter;
import com.google.devtools.common.options.OptionsParsingException;

import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Converter that translates a string of the form "value1,value2,-value3,value4"
 * into a corresponding set of allowed Enum values.
 *
 * <p>Values preceded by '-' are excluded from this set. So "value1,-value2,value3"
 * translates to the set [EnumType.value1, EnumType.value3].
 *
 * <p>If *all* values are exclusions (e.g. "-value1,-value2,-value3"), the returned
 * set contains all values for the Enum type *except* those specified.
 */
class EnumFilterConverter<E extends Enum<E>> implements Converter<Set<E>> {

  private final Set<String> allowedValues = new LinkedHashSet<>();
  private final Class<E> typeClass;
  private final String prettyEnumName;

  /**
   * Constructor.
   *
   * @param typeClass this should be E.class (Java generics can't infer that directly)
   * @param userFriendlyName a user-friendly description of this enum type
   */
  public EnumFilterConverter(Class<E> typeClass, String userFriendlyName) {
    this.typeClass = typeClass;
    this.prettyEnumName = userFriendlyName;
    for (E value : EnumSet.allOf(typeClass)) {
      allowedValues.add(value.name());
    }
  }

  /**
   * Returns the set of allowed values for the option.
   *
   * Implements {@link #convert(String)}.
   */
  @Override
  public Set<E> convert(String input) throws OptionsParsingException {
    if (input.isEmpty()) {
      return Collections.emptySet();
    }
    EnumSet<E> includedSet = EnumSet.noneOf(typeClass);
    EnumSet<E> excludedSet = EnumSet.noneOf(typeClass);
    for (String value : input.split(",", -1)) {
      boolean excludeFlag = value.startsWith("-");
      String s = (excludeFlag ? value.substring(1) : value).toUpperCase();
      if (!allowedValues.contains(s)) {
        throw new OptionsParsingException("Invalid " + prettyEnumName + " filter '" + value +
            "' in the input '" + input + "'");
      }
      (excludeFlag ? excludedSet : includedSet).add(E.valueOf(typeClass, s));
    }
    if (includedSet.isEmpty()) {
      includedSet = EnumSet.complementOf(excludedSet);
    } else {
      includedSet.removeAll(excludedSet);
    }
    if (includedSet.isEmpty()) {
      throw new OptionsParsingException(
          Character.toUpperCase(prettyEnumName.charAt(0)) + prettyEnumName.substring(1) +
          " filter '" + input + "' definition cannot match any tests");
    }
    return includedSet;
  }

  /**
   * Implements {@link #getTypeDescription()}.
   */
  @Override
  public final String getTypeDescription() {
    return "comma-separated list of values: "
        + StringUtil.joinEnglishList(allowedValues).toLowerCase();
  }
}