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.ColorInt; |
| 20 | import android.annotation.ColorLong; |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 23 | import android.graphics.Color; |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * An interface for reading the properties of an inspectable object. |
| 27 | * |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 28 | * {@code PropertyReader} is defined as an interface that will be called by |
| 29 | * {@link InspectionCompanion#readProperties(Object, PropertyReader)}. This approach allows a |
| 30 | * client inspector to read the values of primitive properties without the overhead of |
| 31 | * instantiating a class to hold the property values for each inspection pass. If an inspectable |
| 32 | * remains unchanged between reading passes, it should be possible for a {@code PropertyReader} to |
| 33 | * avoid new allocations for subsequent reading passes. |
| 34 | * |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 35 | * It has separate methods for all primitive types to avoid autoboxing overhead if a concrete |
| 36 | * implementation is able to work with primitives. Implementations should be prepared to accept |
| 37 | * {null} as the value of {@link PropertyReader#readObject(int, Object)}. |
| 38 | * |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 39 | * @see InspectionCompanion#readProperties(Object, PropertyReader) |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 40 | */ |
| 41 | public interface PropertyReader { |
| 42 | /** |
| 43 | * Read a primitive boolean property. |
| 44 | * |
| 45 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 46 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 47 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code boolean} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 48 | */ |
| 49 | void readBoolean(int id, boolean value); |
| 50 | |
| 51 | /** |
| 52 | * Read a primitive byte property. |
| 53 | * |
| 54 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 55 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 56 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code byte} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 57 | */ |
| 58 | void readByte(int id, byte value); |
| 59 | |
| 60 | /** |
| 61 | * Read a primitive character property. |
| 62 | * |
| 63 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 64 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 65 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code char} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 66 | */ |
| 67 | void readChar(int id, char value); |
| 68 | |
| 69 | /** |
| 70 | * Read a read a primitive double property. |
| 71 | * |
| 72 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 73 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 74 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code double} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 75 | */ |
| 76 | void readDouble(int id, double value); |
| 77 | |
| 78 | /** |
| 79 | * Read a primitive float property. |
| 80 | * |
| 81 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 82 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 83 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code float} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 84 | */ |
| 85 | void readFloat(int id, float value); |
| 86 | |
| 87 | /** |
| 88 | * Read a primitive integer property. |
| 89 | * |
| 90 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 91 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 92 | * @throws PropertyTypeMismatchException If the property ID is not mapped as an {@code int} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 93 | */ |
| 94 | void readInt(int id, int value); |
| 95 | |
| 96 | /** |
| 97 | * Read a primitive long property. |
| 98 | * |
| 99 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 100 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 101 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code long} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 102 | */ |
| 103 | void readLong(int id, long value); |
| 104 | |
| 105 | /** |
| 106 | * Read a primitive short property. |
| 107 | * |
| 108 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 109 | * @param value Value of the property |
Ashley Rose | 618e0ef | 2019-01-15 19:10:15 -0500 | [diff] [blame] | 110 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code short} |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 111 | */ |
| 112 | void readShort(int id, short value); |
| 113 | |
| 114 | /** |
| 115 | * Read any object as a property. |
| 116 | * |
| 117 | * If value is null, the property is marked as empty. |
| 118 | * |
| 119 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 120 | * @param value Value of the property |
| 121 | * @throws PropertyTypeMismatchException If the property ID is not mapped as an object |
| 122 | */ |
| 123 | void readObject(int id, @Nullable Object value); |
| 124 | |
| 125 | /** |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 126 | * Read a color packed into a {@link ColorInt} as a property. |
| 127 | * |
| 128 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 129 | * @param value Value of the property |
| 130 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a color |
| 131 | */ |
| 132 | void readColor(int id, @ColorInt int value); |
| 133 | |
| 134 | /** |
| 135 | * Read a color packed into a {@link ColorLong} as a property. |
| 136 | * |
| 137 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 138 | * @param value Value of the property |
| 139 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a color |
| 140 | */ |
| 141 | void readColor(int id, @ColorLong long value); |
| 142 | |
| 143 | /** |
| 144 | * Read a {@link Color} object as a property. |
| 145 | * |
| 146 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 147 | * @param value Value of the property |
| 148 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a color |
| 149 | */ |
| 150 | void readColor(int id, @Nullable Color value); |
| 151 | |
| 152 | /** |
| 153 | * Read {@link android.view.Gravity} packed into an primitive {int}. |
| 154 | * |
| 155 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 156 | * @param value Value of the property |
| 157 | * @throws PropertyTypeMismatchException If the property ID is not mapped as a gravity property |
| 158 | */ |
| 159 | void readGravity(int id, int value); |
| 160 | |
| 161 | /** |
| 162 | * Read an enumeration packed into a primitive {int}. |
| 163 | * |
| 164 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 165 | * @param value Value of the property |
| 166 | * @throws PropertyTypeMismatchException If the property ID is not mapped as an object |
| 167 | */ |
| 168 | void readIntEnum(int id, int value); |
| 169 | |
| 170 | /** |
| 171 | * Read a flag packed into a primitive {int}. |
| 172 | * |
| 173 | * @param id Identifier of the property from a {@link PropertyMapper} |
| 174 | * @param value Value of the property |
| 175 | * @throws PropertyTypeMismatchException If the property ID is not mapped as an object |
| 176 | */ |
| 177 | void readIntFlag(int id, int value); |
| 178 | |
| 179 | /** |
Ashley Rose | 0f25a25 | 2018-11-02 16:36:52 -0400 | [diff] [blame] | 180 | * Thrown if a client calls a typed read method for a property of a different type. |
| 181 | */ |
| 182 | class PropertyTypeMismatchException extends RuntimeException { |
| 183 | public PropertyTypeMismatchException( |
| 184 | int id, |
| 185 | @NonNull String expectedPropertyType, |
| 186 | @NonNull String actualPropertyType, |
| 187 | @Nullable String propertyName) { |
| 188 | super(formatMessage(id, expectedPropertyType, actualPropertyType, propertyName)); |
| 189 | } |
| 190 | |
| 191 | public PropertyTypeMismatchException( |
| 192 | int id, |
| 193 | @NonNull String expectedPropertyType, |
| 194 | @NonNull String actualPropertyType) { |
| 195 | super(formatMessage(id, expectedPropertyType, actualPropertyType, null)); |
| 196 | } |
| 197 | |
| 198 | private static @NonNull String formatMessage( |
| 199 | int id, |
| 200 | @NonNull String expectedPropertyType, |
| 201 | @NonNull String actualPropertyType, |
| 202 | @Nullable String propertyName) { |
| 203 | |
| 204 | if (propertyName == null) { |
| 205 | return String.format( |
| 206 | "Attempted to read property with ID 0x%08X as type %s, " |
| 207 | + "but the ID is of type %s.", |
| 208 | id, |
| 209 | expectedPropertyType, |
| 210 | actualPropertyType |
| 211 | ); |
| 212 | } else { |
| 213 | return String.format( |
| 214 | "Attempted to read property \"%s\" with ID 0x%08X as type %s, " |
| 215 | + "but the ID is of type %s.", |
| 216 | propertyName, |
| 217 | id, |
| 218 | expectedPropertyType, |
| 219 | actualPropertyType |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |