diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax/Type.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/Type.java | 109 |
1 files changed, 21 insertions, 88 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Type.java b/src/main/java/com/google/devtools/build/lib/syntax/Type.java index 37c280c26c..714cb5da31 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/Type.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/Type.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.syntax; -import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -24,7 +23,6 @@ import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.util.LoggingUtil; import com.google.devtools.build.lib.util.StringCanonicalizer; import java.util.ArrayList; -import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -51,14 +49,6 @@ import javax.annotation.Nullable; */ public abstract class Type<T> { - private final Function<Object, Iterable<? extends Object>> flattenFunction = - new Function<Object, Iterable<? extends Object>>() { - @Override - public Iterable<? extends Object> apply(Object value) { - return extractLabels(value); - } - }; - protected Type() {} /** @@ -132,14 +122,13 @@ public abstract class Type<T> { */ public abstract T getDefaultValue(); - /** - * Returns whether there exists an {@code x} such that {@code extractLabels(x)} will return a - * non-{@code NO_LABELS} value. - */ - protected abstract boolean containsLabels(); + /** Function accepting a (potentially null) object value. See {@link #visitLabels}. */ + public static interface LabelVisitor { + void visit(@Nullable Object object) throws InterruptedException; + } /** - * Extracts all the labels from the given instance of the type. + * Pall labels associated with the instance of the type to visitor. * * <p>This is used to support reliable label visitation in * {@link com.google.devtools.build.lib.packages.AbstractAttributeMapper#visitLabels}. To preserve @@ -147,12 +136,7 @@ public abstract class Type<T> { * words, be careful about defining default instances in base types that get auto-inherited by * their children. Keep all definitions as explicit as possible. */ - public abstract Iterable<? extends Object> extractLabels(Object value); - - /** - * {@link #extractLabels} return value for types that don't contain labels. - */ - protected static final ImmutableList<Object> NO_LABELS = ImmutableList.of(); + public abstract void visitLabels(LabelVisitor visitor, Object value) throws InterruptedException; /** * Implementation of concatenation for this type (e.g. "val1 + val2"). Returns null to @@ -278,13 +262,7 @@ public abstract class Type<T> { } @Override - protected boolean containsLabels() { - return false; - } - - @Override - public Iterable<? extends Object> extractLabels(Object value) { - return NO_LABELS; + public void visitLabels(LabelVisitor visitor, Object value) { } @Override @@ -310,13 +288,7 @@ public abstract class Type<T> { } @Override - protected boolean containsLabels() { - return false; - } - - @Override - public Collection<Object> extractLabels(Object value) { - return NO_LABELS; + public void visitLabels(LabelVisitor visitor, Object value) { } @Override @@ -355,13 +327,7 @@ public abstract class Type<T> { } @Override - protected boolean containsLabels() { - return false; - } - - @Override - public Iterable<Object> extractLabels(Object value) { - return NO_LABELS; + public void visitLabels(LabelVisitor visitor, Object value) { } @Override @@ -411,13 +377,7 @@ public abstract class Type<T> { } @Override - protected boolean containsLabels() { - return false; - } - - @Override - public Collection<Object> extractLabels(Object value) { - return NO_LABELS; + public void visitLabels(LabelVisitor visitor, Object value) { } @Override @@ -462,23 +422,13 @@ public abstract class Type<T> { private final Map<KeyT, ValueT> empty = ImmutableMap.of(); - private final Function< - Map.Entry<KeyT, ValueT>, Iterable<? extends Object>> mapEntryFlattenFunction = - new Function<Map.Entry<KeyT, ValueT>, Iterable<? extends Object>>() { - @Override - public Iterable<? extends Object> apply(Entry<KeyT, ValueT> entry) { - Iterable<? extends Object> flattenedKeys = keyType.extractLabels(entry.getKey()); - Iterable<? extends Object> flattenedValues = valueType.extractLabels(entry.getValue()); - if (keyType.containsLabels() && valueType.containsLabels()) { - return Iterables.concat(flattenedKeys, flattenedValues); - } else if (keyType.containsLabels()) { - return flattenedKeys; - } else if (valueType.containsLabels()) { - return flattenedValues; - } - throw new IllegalStateException(this.toString()); - } - }; + @Override + public void visitLabels(LabelVisitor visitor, Object value) throws InterruptedException { + for (Entry<KeyT, ValueT> entry : cast(value).entrySet()) { + keyType.visitLabels(visitor, entry.getKey()); + valueType.visitLabels(visitor, entry.getValue()); + } + } public static <KEY, VALUE> DictType<KEY, VALUE> create( Type<KEY> keyType, Type<VALUE> valueType) { @@ -531,18 +481,6 @@ public abstract class Type<T> { public Map<KeyT, ValueT> getDefaultValue() { return empty; } - - @Override - protected boolean containsLabels() { - return keyType.containsLabels() || valueType.containsLabels(); - } - - @Override - public Iterable<Object> extractLabels(Object value) { - return containsLabels() - ? Iterables.concat(Iterables.transform(cast(value).entrySet(), mapEntryFlattenFunction)) - : NO_LABELS; - } } /** A type for lists of a given element type */ @@ -577,15 +515,10 @@ public abstract class Type<T> { } @Override - protected boolean containsLabels() { - return elemType.containsLabels(); - } - - @Override - public Iterable<? extends Object> extractLabels(Object value) { - return containsLabels() - ? Iterables.concat(Iterables.transform(cast(value), elemType.flattenFunction)) - : NO_LABELS; + public void visitLabels(LabelVisitor visitor, Object value) throws InterruptedException { + for (ElemT elem : cast(value)) { + elemType.visitLabels(visitor, elem); + } } @Override |