Convert ToolchainCollection to use AutoValue.
Also Break the subclass relationship between ExecGroupCollection and ToolchainCollection.
This adds toString, hashCode, and equals implementations for both classes.
PiperOrigin-RevId: 314338648
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ToolchainCollection.java b/src/main/java/com/google/devtools/build/lib/analysis/ToolchainCollection.java
index 206a8d6..4991889 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ToolchainCollection.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ToolchainCollection.java
@@ -15,6 +15,7 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
+import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
@@ -29,30 +30,56 @@
* @param <T> any class that extends ToolchainContext. This generic allows ToolchainCollection to be
* used, e.g., both before and after toolchain resolution.
*/
-public class ToolchainCollection<T extends ToolchainContext> {
+@AutoValue
+public abstract class ToolchainCollection<T extends ToolchainContext> {
// This is intentionally a string that would fail {@code Identifier.isValid} so that
// users can't create a group with the same name.
@VisibleForTesting public static final String DEFAULT_EXEC_GROUP_NAME = "default-exec-group";
/** A map of execution group names to toolchain contexts. */
- private final ImmutableMap<String, T> toolchainContexts;
+ public abstract ImmutableMap<String, T> getContextMap();
- private ToolchainCollection(Map<String, T> contexts) {
- Preconditions.checkArgument(contexts.containsKey(DEFAULT_EXEC_GROUP_NAME));
- toolchainContexts = ImmutableMap.copyOf(contexts);
+ T getDefaultToolchainContext() {
+ return getContextMap().get(DEFAULT_EXEC_GROUP_NAME);
}
- ToolchainCollection(ToolchainCollection<T> toCopy) {
- toolchainContexts = ImmutableMap.copyOf(toCopy.getContextMap());
+ boolean hasToolchainContext(String execGroup) {
+ return getContextMap().containsKey(execGroup);
+ }
+
+ public T getToolchainContext(String execGroup) {
+ return getContextMap().get(execGroup);
+ }
+
+ public ImmutableSet<Label> getResolvedToolchains() {
+ return getContextMap().values().stream()
+ .flatMap(c -> c.resolvedToolchainLabels().stream())
+ .collect(toImmutableSet());
+ }
+
+ ImmutableSet<String> getExecGroups() {
+ return getContextMap().keySet();
+ }
+
+ @SuppressWarnings("unchecked")
+ public ToolchainCollection<ToolchainContext> asToolchainContexts() {
+ return (ToolchainCollection<ToolchainContext>) this;
+ }
+
+ /** Returns a new builder for {@link ToolchainCollection} instances. */
+ public static <T extends ToolchainContext> Builder<T> builder() {
+ return new Builder<T>();
}
/** Builder for ToolchainCollection. */
- public static class Builder<T extends ToolchainContext> {
+ public static final class Builder<T extends ToolchainContext> {
+ // This is not immutable so that we can check for duplicate keys easily.
private final Map<String, T> toolchainContexts = new HashMap<>();
public ToolchainCollection<T> build() {
- return new ToolchainCollection<>(toolchainContexts);
+ Preconditions.checkArgument(toolchainContexts.containsKey(DEFAULT_EXEC_GROUP_NAME));
+ return new AutoValue_ToolchainCollection<T>(ImmutableMap.copyOf(toolchainContexts));
}
public void addContext(String execGroup, T context) {
@@ -68,34 +95,4 @@
return this;
}
}
-
- T getDefaultToolchainContext() {
- return toolchainContexts.get(DEFAULT_EXEC_GROUP_NAME);
- }
-
- boolean hasToolchainContext(String execGroup) {
- return toolchainContexts.containsKey(execGroup);
- }
-
- public T getToolchainContext(String execGroup) {
- return toolchainContexts.get(execGroup);
- }
-
- public ImmutableSet<Label> getResolvedToolchains() {
- return toolchainContexts.values().stream()
- .flatMap(c -> c.resolvedToolchainLabels().stream())
- .collect(toImmutableSet());
- }
-
- ImmutableSet<String> getExecGroups() {
- return toolchainContexts.keySet();
- }
-
- public ToolchainCollection<ToolchainContext> asToolchainContexts() {
- return new ToolchainCollection<>(ImmutableMap.copyOf(toolchainContexts));
- }
-
- public ImmutableMap<String, T> getContextMap() {
- return toolchainContexts;
- }
}