diff options
author | Tor Norbye <tnorbye@google.com> | 2012-03-21 18:04:18 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-03-22 14:47:16 -0700 |
commit | 61684adfd7345f1a0df24bcf9176e6f528295bbb (patch) | |
tree | 2b08ae518b0a0150ec610a85679501626b611c62 /rule_api/src/com/android | |
parent | 6b09013b6cb0fdcb48350a60bb3ed924ac9f562d (diff) | |
download | sdk-61684adfd7345f1a0df24bcf9176e6f528295bbb.zip sdk-61684adfd7345f1a0df24bcf9176e6f528295bbb.tar.gz sdk-61684adfd7345f1a0df24bcf9176e6f528295bbb.tar.bz2 |
Change AttributeInfo.getFormats() to returning an EnumSet<Format>
This changeset changes the signature of AttributeInfo.getFormats()
from returning a Format[] to returning an EnumSet<Format>.
Furthermore, it defines a number of constants for the various common
format sets such that they can be reused.
Nearly all uses of the getFormats() method was really just trying to
see "is format X among the formats", so using enumsets is more natural
since it has a contains method. This also lets us replace some
attribute init code which was building up a set from the array for
this exact same purpose just reuse the format set directly.
In the attribute parser, rather than computing the uppercase version
of each format string ("dimension"=>"DIMENSION") and then doing a
Format.valueOf("DIMENSION"), we now compute a map for all the format
constants as lowercase to the corresponding format instance, and use
that map when parsing the attrs.xml file.
Note that there is a small semantic change from having an array of
formats to using an enumset: The format specified an
ordering. However, it does not look like any code depended on this,
and it's also not clear that the ordering in attrs.xml is
intentional. For example, it contains both "color|reference" and
"reference|color", and in both cases the intent is that the attribute
can specify an actual color or a reference to a color. Now with an
enum set, the order used when traversing the formats will always be in
their natural order, and for this purpose the REFERENCE type is moved
to the end since it's the least specific.
Change-Id: I1170cff48086f5cc13e4b70a35deea1f9979c883
Diffstat (limited to 'rule_api/src/com/android')
-rw-r--r-- | rule_api/src/com/android/ide/common/api/IAttributeInfo.java | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/rule_api/src/com/android/ide/common/api/IAttributeInfo.java b/rule_api/src/com/android/ide/common/api/IAttributeInfo.java index e1bb067..7fd253c 100644 --- a/rule_api/src/com/android/ide/common/api/IAttributeInfo.java +++ b/rule_api/src/com/android/ide/common/api/IAttributeInfo.java @@ -20,6 +20,8 @@ import com.android.annotations.NonNull; import com.android.annotations.Nullable; import com.google.common.annotations.Beta; +import java.util.EnumSet; + /** * Information about an attribute as gathered from the attrs.xml file where * the attribute was declared. This must include a format (string, reference, float, etc.), @@ -38,32 +40,55 @@ public interface IAttributeInfo { BOOLEAN, INTEGER, FLOAT, - REFERENCE, COLOR, DIMENSION, FRACTION, ENUM, - FLAG; + FLAG, + REFERENCE; + + public static final EnumSet<Format> NONE = EnumSet.noneOf(Format.class); + public static final EnumSet<Format> FLAG_SET = EnumSet.of(FLAG); + public static final EnumSet<Format> ENUM_SET = EnumSet.of(ENUM); + public static final EnumSet<Format> COLOR_SET = EnumSet.of(COLOR); + public static final EnumSet<Format> STRING_SET = EnumSet.of(STRING); + public static final EnumSet<Format> BOOLEAN_SET = EnumSet.of(BOOLEAN); + public static final EnumSet<Format> INTEGER_SET = EnumSet.of(INTEGER); + public static final EnumSet<Format> FLOAT_SET = EnumSet.of(FLOAT); + public static final EnumSet<Format> DIMENSION_SET = EnumSet.of(DIMENSION); + public static final EnumSet<Format> REFERENCE_SET = EnumSet.of(REFERENCE); /** - * Returns true if and only if this format is in the given array of - * formats + * Returns an EnumSet containing only this format (which should not be + * modified by the client) * - * @param formats An array of formats, or null. - * @return True if and only if the given array (if any) contains this - * format. + * @return a new enum set containing exactly this format */ - public boolean in(@Nullable Format[] formats) { - if (formats == null) { - return false; + @NonNull + public EnumSet<Format> asSet() { + switch (this) { + case BOOLEAN: + return BOOLEAN_SET; + case COLOR: + return COLOR_SET; + case DIMENSION: + return DIMENSION_SET; + case ENUM: + return ENUM_SET; + case FLAG: + return FLAG_SET; + case FLOAT: + return FLOAT_SET; + case INTEGER: + return INTEGER_SET; + case STRING: + return STRING_SET; + case REFERENCE: + return REFERENCE_SET; + case FRACTION: + default: + return EnumSet.of(this); } - for (Format f : formats) { - if (f == this) { - return true; - } - } - - return false; } } @@ -74,7 +99,7 @@ public interface IAttributeInfo { /** Returns the formats of the attribute. Cannot be null. * Should have at least one format. */ @NonNull - public Format[] getFormats(); + public EnumSet<Format> getFormats(); /** Returns the values for enums. null for other types. */ @Nullable |