diff options
author | ccalvarin <ccalvarin@google.com> | 2017-10-16 22:18:32 +0200 |
---|---|---|
committer | Jakob Buchgraber <buchgr@google.com> | 2017-10-18 10:27:58 +0200 |
commit | 7cd9e883dd31f54cd505844aa1f1e0ed7bd9f380 (patch) | |
tree | e72e67a2f22108d490aaf4b5a59e5727e855547d /src/main/java/com/google/devtools/common/options/OptionPriority.java | |
parent | b6bf11217c30123827d36a35a3614ba8e200f349 (diff) |
Track Option placement within a priority category.
An option has precedence over previous options at the same enum-valued priority. Track its placement in this ordering explicitly.
This will allow after-the-fact expansion of expansion options such that they correctly take precedence or not compared to other mentions of the same flag. This is needed to fix --config's expansion.
RELNOTES: None.
PiperOrigin-RevId: 172367996
Diffstat (limited to 'src/main/java/com/google/devtools/common/options/OptionPriority.java')
-rw-r--r-- | src/main/java/com/google/devtools/common/options/OptionPriority.java | 118 |
1 files changed, 83 insertions, 35 deletions
diff --git a/src/main/java/com/google/devtools/common/options/OptionPriority.java b/src/main/java/com/google/devtools/common/options/OptionPriority.java index a28f012822..96c471ed74 100644 --- a/src/main/java/com/google/devtools/common/options/OptionPriority.java +++ b/src/main/java/com/google/devtools/common/options/OptionPriority.java @@ -13,50 +13,98 @@ // limitations under the License. package com.google.devtools.common.options; +import java.util.Objects; + /** - * The priority of option values, in order of increasing priority. - * - * <p>In general, new values for options can only override values with a lower or - * equal priority. Option values provided in annotations in an options class are - * implicitly at the priority {@code DEFAULT}. + * The position of an option in the interpretation order. Options are interpreted using a + * last-option-wins system for single valued options, and are listed in that order for + * multiple-valued options. * - * <p>The ordering of the priorities is the source-code order. This is consistent - * with the automatically generated {@code compareTo} method as specified by the - * Java Language Specification. DO NOT change the source-code order of these - * values, or you will break code that relies on the ordering. + * <p>The position of the option is in category order, and within the priority category in index + * order. */ -public enum OptionPriority { +public class OptionPriority implements Comparable<OptionPriority> { + private final PriorityCategory priorityCategory; + private final int index; - /** - * The priority of values specified in the {@link Option} annotation. This - * should never be specified in calls to {@link OptionsParser#parse}. - */ - DEFAULT, + private OptionPriority(PriorityCategory priorityCategory, int index) { + this.priorityCategory = priorityCategory; + this.index = index; + } - /** - * Overrides default options at runtime, while still allowing the values to be - * overridden manually. - */ - COMPUTED_DEFAULT, + public static OptionPriority lowestOptionPriorityAtCategory(PriorityCategory category) { + return new OptionPriority(category, 0); + } - /** - * For options coming from a configuration file or rc file. - */ - RC_FILE, + public static OptionPriority nextOptionPriority(OptionPriority priority) { + return new OptionPriority(priority.priorityCategory, priority.index + 1); + } - /** - * For options coming from the command line. - */ - COMMAND_LINE, + public PriorityCategory getPriorityCategory() { + return priorityCategory; + } - /** - * For options coming from invocation policy. - */ - INVOCATION_POLICY, + @Override + public int compareTo(OptionPriority o) { + if (priorityCategory.equals(o.priorityCategory)) { + return index - o.index; + } + return priorityCategory.ordinal() - o.priorityCategory.ordinal(); + } + + @Override + public boolean equals(Object o) { + if (o instanceof OptionPriority) { + OptionPriority other = (OptionPriority) o; + return other.priorityCategory.equals(priorityCategory) && other.index == index; + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(priorityCategory, index); + } /** - * This priority can be used to unconditionally override any user-provided options. - * This should be used rarely and with caution! + * The priority of option values, in order of increasing priority. + * + * <p>In general, new values for options can only override values with a lower or equal priority. + * Option values provided in annotations in an options class are implicitly at the priority {@code + * DEFAULT}. + * + * <p>The ordering of the priorities is the source-code order. This is consistent with the + * automatically generated {@code compareTo} method as specified by the Java Language + * Specification. DO NOT change the source-code order of these values, or you will break code that + * relies on the ordering. */ - SOFTWARE_REQUIREMENT; + public enum PriorityCategory { + + /** + * The priority of values specified in the {@link Option} annotation. This should never be + * specified in calls to {@link OptionsParser#parse}. + */ + DEFAULT, + + /** + * Overrides default options at runtime, while still allowing the values to be overridden + * manually. + */ + COMPUTED_DEFAULT, + + /** For options coming from a configuration file or rc file. */ + RC_FILE, + + /** For options coming from the command line. */ + COMMAND_LINE, + + /** For options coming from invocation policy. */ + INVOCATION_POLICY, + + /** + * This priority can be used to unconditionally override any user-provided options. This should + * be used rarely and with caution! + */ + SOFTWARE_REQUIREMENT + } } |