From 2cb4f48dc5f674a4a99228cce7ca04aef5a0c335 Mon Sep 17 00:00:00 2001 From: gregce Date: Tue, 29 May 2018 10:47:33 -0700 Subject: Merge ComposingSplitTransition and ComposingPatchTransition. Part of https://docs.google.com/document/d/1_UJKmAQ9EE8i3Pl0il3YLTYr-Q9EKYYyLatt2zohfyM/edit# PiperOrigin-RevId: 198420365 --- .../transitions/ComposingPatchTransition.java | 72 -------------- .../transitions/ComposingSplitTransition.java | 103 --------------------- .../config/transitions/ComposingTransition.java | 85 +++++++++++++++++ 3 files changed, 85 insertions(+), 175 deletions(-) delete mode 100644 src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingPatchTransition.java delete mode 100644 src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingSplitTransition.java create mode 100644 src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingTransition.java (limited to 'src/main/java/com/google/devtools/build/lib/analysis/config/transitions') diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingPatchTransition.java b/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingPatchTransition.java deleted file mode 100644 index 1fec601858..0000000000 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingPatchTransition.java +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2017 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.analysis.config.transitions; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Iterables; -import com.google.devtools.build.lib.analysis.config.BuildOptions; -import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; - -/** - * A {@link ComposingSplitTransition} that only supports {@link PatchTransition}s - * - *

Calling code that doesn't want to have to handle splits should prefer this version. - */ -@AutoCodec -public class ComposingPatchTransition implements PatchTransition { - private final ComposingSplitTransition delegate; - - /** - * Creates a {@link ComposingPatchTransition} that applies the sequence: {@code fromOptions -> - * transition1 -> transition2 -> toOptions }. - * - *

Note that it's possible to create silly transitions with this constructor (e.g., if one or - * both of the transitions is NoTransition). Use composePatchTransitions instead, which checks for - * these states and avoids instantiation appropriately. - * - * @see TransitionResolver#composePatchTransitions - */ - public ComposingPatchTransition(PatchTransition transition1, PatchTransition transition2) { - this(new ComposingSplitTransition(transition1, transition2)); - } - - @AutoCodec.Instantiator - ComposingPatchTransition(ComposingSplitTransition delegate) { - Preconditions.checkArgument(delegate.isPatchOnly()); - this.delegate = delegate; - } - - @Override - public BuildOptions patch(BuildOptions options) { - return Iterables.getOnlyElement(delegate.split(options)); - } - - @Override - public String getName() { - return delegate.getName(); - } - - @Override - public int hashCode() { - return delegate.hashCode(); - } - - @Override - public boolean equals(Object other) { - return other instanceof ComposingPatchTransition - && ((ComposingPatchTransition) other).delegate.equals(this.delegate); - } -} - diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingSplitTransition.java b/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingSplitTransition.java deleted file mode 100644 index 4eb6ae0e89..0000000000 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingSplitTransition.java +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2017 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.analysis.config.transitions; - -import com.google.common.collect.ImmutableList; -import com.google.devtools.build.lib.analysis.config.BuildOptions; -import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; -import java.util.List; -import java.util.Objects; - -/** - * A configuration transition that composes two other transitions in an ordered sequence. - * - *

Example: - * - *

- *   transition1: { someSetting = $oldVal + " foo" }
- *   transition2: { someSetting = $oldVal + " bar" }
- *   ComposingSplitTransition(transition1, transition2): { someSetting = $oldVal + " foo bar" }
- * 
- * - *

Child transitions can be {@link SplitTransition}s, {@link PatchTransition}s, or any - * combination thereof. We implement this class as a {@link SplitTransition} since that abstraction - * captures all possible combinations. - */ -@AutoCodec -public class ComposingSplitTransition implements SplitTransition { - private ConfigurationTransition transition1; - private ConfigurationTransition transition2; - - @Override - public String getName() { - return "(" + transition1.getName() + " + " + transition2.getName() + ")"; - } - - /** - * Creates a {@link ComposingSplitTransition} that applies the sequence: {@code fromOptions -> - * transition1 -> transition2 -> toOptions }. - * - *

Note that it's possible to create silly transitions with this constructor (e.g., if one or - * both of the transitions is NoTransition). Use composeTransitions instead, which checks for - * these states and avoids instantiation appropriately. - * - * @see TransitionResolver#composeTransitions - */ - @AutoCodec.Instantiator - public ComposingSplitTransition( - ConfigurationTransition transition1, ConfigurationTransition transition2) { - this.transition1 = transition1; - this.transition2 = transition2; - } - - @Override - public List split(BuildOptions buildOptions) { - ImmutableList.Builder toOptions = ImmutableList.builder(); - for (BuildOptions transition1Options : transition1.apply(buildOptions)) { - toOptions.addAll(transition2.apply(transition1Options)); - } - return toOptions.build(); - } - - @Override - public int hashCode() { - return Objects.hash(transition1, transition2); - } - - @Override - public boolean equals(Object other) { - return other instanceof ComposingSplitTransition - && ((ComposingSplitTransition) other).transition1.equals(this.transition1) - && ((ComposingSplitTransition) other).transition2.equals(this.transition2); - } - - /** - * Returns whether this transition contains only patches (and is thus suitable as a delegate - * for {@link ComposingPatchTransition}). - */ - public boolean isPatchOnly() { - return transition1 instanceof PatchTransition && transition2 instanceof PatchTransition; - } - - /** - * Allows this transition to be used in patch-only contexts if it contains only - * {@link PatchTransition}s. - * - *

Can only be called if {@link #isPatchOnly()} returns true. - */ - public ComposingPatchTransition asPatch() { - return new ComposingPatchTransition(this); - } -} diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingTransition.java b/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingTransition.java new file mode 100644 index 0000000000..9e0f770a1f --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/transitions/ComposingTransition.java @@ -0,0 +1,85 @@ +// Copyright 2017 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.analysis.config.transitions; + +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.analysis.config.BuildOptions; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; +import java.util.List; +import java.util.Objects; + +/** + * A configuration transition that composes two other transitions in an ordered sequence. + * + *

Example: + * + *

+ *   transition1: { someSetting = $oldVal + " foo" }
+ *   transition2: { someSetting = $oldVal + " bar" }
+ *   ComposingTransition(transition1, transition2): { someSetting = $oldVal + " foo bar" }
+ * 
+ */ +@AutoCodec +public class ComposingTransition implements ConfigurationTransition { + private ConfigurationTransition transition1; + private ConfigurationTransition transition2; + + /** + * Creates a {@link ComposingTransition} that applies the sequence: {@code fromOptions -> + * transition1 -> transition2 -> toOptions }. + * + *

Note that it's possible to create silly transitions with this constructor (e.g., if one or + * both of the transitions is {@link NoTransition}). Use + * {@link com.google.devtools.build.lib.analysis.config.TransitionResolver#composeTransitions} + * for these cases - it checks for for these states and avoids instantiation appropriately. + */ + @AutoCodec.Instantiator + public ComposingTransition( + ConfigurationTransition transition1, ConfigurationTransition transition2) { + this.transition1 = transition1; + this.transition2 = transition2; + } + + @Override + public List apply(BuildOptions buildOptions) { + ImmutableList.Builder toOptions = ImmutableList.builder(); + for (BuildOptions transition1Options : transition1.apply(buildOptions)) { + toOptions.addAll(transition2.apply(transition1Options)); + } + return toOptions.build(); + } + + @Override + public String reasonForOverride() { + return "Basic abstraction for combining other transitions"; + } + + @Override + public String getName() { + return "(" + transition1.getName() + " + " + transition2.getName() + ")"; + } + + @Override + public int hashCode() { + return Objects.hash(transition1, transition2); + } + + @Override + public boolean equals(Object other) { + return other instanceof ComposingTransition + && ((ComposingTransition) other).transition1.equals(this.transition1) + && ((ComposingTransition) other).transition2.equals(this.transition2); + } +} -- cgit v1.2.3