Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view.inspector; |
| 18 | |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 19 | import android.annotation.AttrRes; |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | |
| 22 | /** |
| 23 | * An interface for mapping the string names of inspectable properties to integer identifiers. |
| 24 | * |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 25 | * This interface is consumed by {@link InspectionCompanion#mapProperties(PropertyMapper)}. |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 26 | * |
| 27 | * Mapping properties to IDs enables quick comparisons against shadow copies of inspectable |
| 28 | * objects without performing a large number of string comparisons. |
| 29 | * |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 30 | * @see InspectionCompanion#mapProperties(PropertyMapper) |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 31 | */ |
| 32 | public interface PropertyMapper { |
| 33 | /** |
| 34 | * Map a string name to an integer ID for a primitive boolean property. |
| 35 | * |
| 36 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 37 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 38 | * @return An integer ID for the property |
| 39 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 40 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 41 | int mapBoolean(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Map a string name to an integer ID for a primitive byte property. |
| 45 | * |
| 46 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 47 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 48 | * @return An integer ID for the property |
| 49 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 50 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 51 | int mapByte(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Map a string name to an integer ID for a primitive char property. |
| 55 | * |
| 56 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 57 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 58 | * @return An integer ID for the property |
| 59 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 60 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 61 | int mapChar(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Map a string name to an integer ID for a primitive double property. |
| 65 | * |
| 66 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 67 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 68 | * @return An integer ID for the property |
| 69 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 70 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 71 | int mapDouble(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Map a string name to an integer ID for a primitive float property. |
| 75 | * |
| 76 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 77 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 78 | * @return An integer ID for the property |
| 79 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 80 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 81 | int mapFloat(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Map a string name to an integer ID for a primitive int property. |
| 85 | * |
| 86 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 87 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 88 | * @return An integer ID for the property |
| 89 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 90 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 91 | int mapInt(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Map a string name to an integer ID for a primitive long property. |
| 95 | * |
| 96 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 97 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 98 | * @return An integer ID for the property |
| 99 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 100 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 101 | int mapLong(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Map a string name to an integer ID for a primitive short property. |
| 105 | * |
| 106 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 107 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 108 | * @return An integer ID for the property |
| 109 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 110 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 111 | int mapShort(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * Map a string name to an integer ID for an object property. |
| 115 | * |
| 116 | * @param name The name of the property |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 117 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 118 | * @return An integer ID for the property |
| 119 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 120 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 121 | int mapObject(@NonNull String name, @AttrRes int attributeId); |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 122 | |
| 123 | /** |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 124 | * Map a string name to an integer ID for a color property. |
| 125 | * |
| 126 | * @param name The name of the property |
| 127 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
| 128 | * @return An integer ID for the property |
| 129 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 130 | * @see android.graphics.Color |
| 131 | */ |
| 132 | int mapColor(@NonNull String name, @AttrRes int attributeId); |
| 133 | |
| 134 | /** |
| 135 | * Map a string name to an integer ID for a gravity property. |
| 136 | * |
| 137 | * @param name The name of the property |
| 138 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
| 139 | * @return An integer ID for the property |
| 140 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 141 | * @see android.view.Gravity |
| 142 | */ |
| 143 | int mapGravity(@NonNull String name, @AttrRes int attributeId); |
| 144 | |
| 145 | /** |
| 146 | * Map a string name to an integer ID for an enumeration packed into an int property. |
| 147 | * |
| 148 | * @param name The name of the property |
| 149 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
| 150 | * @param mapping A mapping from int to String |
| 151 | * @return An integer ID for the property |
| 152 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 153 | */ |
| 154 | int mapIntEnum( |
| 155 | @NonNull String name, |
| 156 | @AttrRes int attributeId, |
Ashley Rose | a61e7cd | 2019-01-23 14:37:42 -0500 | [diff] [blame] | 157 | @NonNull IntEnumMapping mapping); |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 158 | |
| 159 | /** |
Ashley Rose | e891481 | 2019-03-05 17:12:00 -0500 | [diff] [blame] | 160 | * Map a string name to an integer ID for an attribute that contains resource IDs. |
| 161 | * |
| 162 | * @param name The name of the property |
| 163 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
| 164 | * @return An integer ID for the property |
| 165 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 166 | */ |
| 167 | int mapResourceId(@NonNull String name, @AttrRes int attributeId); |
| 168 | |
| 169 | /** |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 170 | * Map a string name to an integer ID for a flag set packed into an int property. |
| 171 | * |
| 172 | * @param name The name of the property |
| 173 | * @param attributeId If the property is from an XML attribute, the resource ID of the property |
Ashley Rose | a61e7cd | 2019-01-23 14:37:42 -0500 | [diff] [blame] | 174 | * @param mapping A mapping from int to a set of strings |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 175 | * @return An integer ID for the property |
| 176 | * @throws PropertyConflictException If the property name is already mapped as another type. |
| 177 | */ |
| 178 | int mapIntFlag( |
| 179 | @NonNull String name, |
| 180 | @AttrRes int attributeId, |
| 181 | @NonNull IntFlagMapping mapping); |
| 182 | /** |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 183 | * Thrown from a map method if a property name is already mapped as different type. |
| 184 | */ |
| 185 | class PropertyConflictException extends RuntimeException { |
| 186 | public PropertyConflictException( |
| 187 | @NonNull String name, |
| 188 | @NonNull String newPropertyType, |
| 189 | @NonNull String existingPropertyType) { |
| 190 | super(String.format( |
| 191 | "Attempted to map property \"%s\" as type %s, but it is already mapped as %s.", |
| 192 | name, |
| 193 | newPropertyType, |
| 194 | existingPropertyType |
| 195 | )); |
| 196 | } |
| 197 | } |
| 198 | } |