diff options
author | 2016-02-16 23:59:33 +0000 | |
---|---|---|
committer | 2016-02-17 09:55:25 +0000 | |
commit | 74a39f09e87aa8c0698610046bd8119619a9d2c8 (patch) | |
tree | c5e412531d1f3f0b1f2f1be9784d8bbff51c8bfd /src/main/java/com | |
parent | d5d42790fbfb14d1ba176a24e8c238258eedf706 (diff) |
Add a method to get a single value from AspectParameters.
I found this to be so common I think we should just provide it on the AspectParameters class.
--
MOS_MIGRATED_REVID=114803710
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/AspectParameters.java | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/AspectParameters.java b/src/main/java/com/google/devtools/build/lib/packages/AspectParameters.java index f261687e17..0cf175e130 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/AspectParameters.java +++ b/src/main/java/com/google/devtools/build/lib/packages/AspectParameters.java @@ -13,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.lib.packages; +import static com.google.common.collect.Iterables.getOnlyElement; + import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableMultimap; @@ -20,8 +22,6 @@ import com.google.common.collect.Multimap; import java.util.Objects; -import javax.annotation.Nullable; - /** * Objects of this class contain values of some attributes of rules. Used for passing this * information to the aspects. @@ -60,11 +60,20 @@ public final class AspectParameters { /** * Returns collection of values for specified key, or null if key is missing. */ - @Nullable public ImmutableCollection<String> getAttribute(String key) { return attributes.get(key); } + /** + * Similar to {@link #getAttribute}}, but asserts that there's only one value for the provided + * key. + * Uses Guava's {@link Iterables#getOnlyElement}, which may throw exceptions if there isn't + * exactly one element. + */ + public String getOnlyValueOfAttribute(String key) { + return getOnlyElement(getAttribute(key)); + } + public boolean isEmpty() { return this.equals(AspectParameters.EMPTY); } |