blob: b5020ce6495f4d02172cf1f1893b92249295aeeb [file] [log] [blame]
Ashley Rose0f25a252018-11-02 16:36:52 -04001/*
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
17package android.view.inspector;
18
Ashley Rosee8914812019-03-05 17:12:00 -050019import android.annotation.AnyRes;
Ashley Rosed2c5f452018-11-29 15:40:10 -050020import android.annotation.ColorInt;
21import android.annotation.ColorLong;
Ashley Rose0f25a252018-11-02 16:36:52 -040022import android.annotation.NonNull;
23import android.annotation.Nullable;
Ashley Rosed2c5f452018-11-29 15:40:10 -050024import android.graphics.Color;
Ashley Rose0f25a252018-11-02 16:36:52 -040025
26/**
27 * An interface for reading the properties of an inspectable object.
28 *
Ashley Rose618e0ef2019-01-15 19:10:15 -050029 * {@code PropertyReader} is defined as an interface that will be called by
30 * {@link InspectionCompanion#readProperties(Object, PropertyReader)}. This approach allows a
31 * client inspector to read the values of primitive properties without the overhead of
32 * instantiating a class to hold the property values for each inspection pass. If an inspectable
33 * remains unchanged between reading passes, it should be possible for a {@code PropertyReader} to
34 * avoid new allocations for subsequent reading passes.
35 *
Ashley Rose0f25a252018-11-02 16:36:52 -040036 * It has separate methods for all primitive types to avoid autoboxing overhead if a concrete
37 * implementation is able to work with primitives. Implementations should be prepared to accept
38 * {null} as the value of {@link PropertyReader#readObject(int, Object)}.
39 *
Ashley Rosed2c5f452018-11-29 15:40:10 -050040 * @see InspectionCompanion#readProperties(Object, PropertyReader)
Ashley Rose0f25a252018-11-02 16:36:52 -040041 */
42public interface PropertyReader {
43 /**
44 * Read a primitive boolean property.
45 *
46 * @param id Identifier of the property from a {@link PropertyMapper}
47 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -050048 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code boolean}
Ashley Rose0f25a252018-11-02 16:36:52 -040049 */
50 void readBoolean(int id, boolean value);
51
52 /**
53 * Read a primitive byte property.
54 *
55 * @param id Identifier of the property from a {@link PropertyMapper}
56 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -050057 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code byte}
Ashley Rose0f25a252018-11-02 16:36:52 -040058 */
59 void readByte(int id, byte value);
60
61 /**
62 * Read a primitive character property.
63 *
64 * @param id Identifier of the property from a {@link PropertyMapper}
65 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -050066 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code char}
Ashley Rose0f25a252018-11-02 16:36:52 -040067 */
68 void readChar(int id, char value);
69
70 /**
71 * Read a read a primitive double property.
72 *
73 * @param id Identifier of the property from a {@link PropertyMapper}
74 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -050075 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code double}
Ashley Rose0f25a252018-11-02 16:36:52 -040076 */
77 void readDouble(int id, double value);
78
79 /**
80 * Read a primitive float property.
81 *
82 * @param id Identifier of the property from a {@link PropertyMapper}
83 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -050084 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code float}
Ashley Rose0f25a252018-11-02 16:36:52 -040085 */
86 void readFloat(int id, float value);
87
88 /**
89 * Read a primitive integer property.
90 *
91 * @param id Identifier of the property from a {@link PropertyMapper}
92 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -050093 * @throws PropertyTypeMismatchException If the property ID is not mapped as an {@code int}
Ashley Rose0f25a252018-11-02 16:36:52 -040094 */
95 void readInt(int id, int value);
96
97 /**
98 * Read a primitive long property.
99 *
100 * @param id Identifier of the property from a {@link PropertyMapper}
101 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -0500102 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code long}
Ashley Rose0f25a252018-11-02 16:36:52 -0400103 */
104 void readLong(int id, long value);
105
106 /**
107 * Read a primitive short property.
108 *
109 * @param id Identifier of the property from a {@link PropertyMapper}
110 * @param value Value of the property
Ashley Rose618e0ef2019-01-15 19:10:15 -0500111 * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code short}
Ashley Rose0f25a252018-11-02 16:36:52 -0400112 */
113 void readShort(int id, short value);
114
115 /**
116 * Read any object as a property.
117 *
118 * If value is null, the property is marked as empty.
119 *
120 * @param id Identifier of the property from a {@link PropertyMapper}
121 * @param value Value of the property
122 * @throws PropertyTypeMismatchException If the property ID is not mapped as an object
123 */
124 void readObject(int id, @Nullable Object value);
125
126 /**
Ashley Rosed2c5f452018-11-29 15:40:10 -0500127 * Read a color packed into a {@link ColorInt} as a property.
128 *
129 * @param id Identifier of the property from a {@link PropertyMapper}
130 * @param value Value of the property
131 * @throws PropertyTypeMismatchException If the property ID is not mapped as a color
132 */
133 void readColor(int id, @ColorInt int value);
134
135 /**
136 * Read a color packed into a {@link ColorLong} as a property.
137 *
138 * @param id Identifier of the property from a {@link PropertyMapper}
139 * @param value Value of the property
140 * @throws PropertyTypeMismatchException If the property ID is not mapped as a color
141 */
142 void readColor(int id, @ColorLong long value);
143
144 /**
145 * Read a {@link Color} object as a property.
146 *
147 * @param id Identifier of the property from a {@link PropertyMapper}
148 * @param value Value of the property
149 * @throws PropertyTypeMismatchException If the property ID is not mapped as a color
150 */
151 void readColor(int id, @Nullable Color value);
152
153 /**
Ashley Rosee8914812019-03-05 17:12:00 -0500154 * Read {@link android.view.Gravity} packed into an primitive {@code int}.
Ashley Rosed2c5f452018-11-29 15:40:10 -0500155 *
156 * @param id Identifier of the property from a {@link PropertyMapper}
157 * @param value Value of the property
158 * @throws PropertyTypeMismatchException If the property ID is not mapped as a gravity property
159 */
160 void readGravity(int id, int value);
161
162 /**
Ashley Rosee8914812019-03-05 17:12:00 -0500163 * Read an enumeration packed into a primitive {@code int}.
Ashley Rosed2c5f452018-11-29 15:40:10 -0500164 *
165 * @param id Identifier of the property from a {@link PropertyMapper}
166 * @param value Value of the property
167 * @throws PropertyTypeMismatchException If the property ID is not mapped as an object
168 */
169 void readIntEnum(int id, int value);
170
171 /**
Ashley Rosee8914812019-03-05 17:12:00 -0500172 * Read a flag packed into a primitive {@code int}.
Ashley Rosed2c5f452018-11-29 15:40:10 -0500173 *
174 * @param id Identifier of the property from a {@link PropertyMapper}
175 * @param value Value of the property
176 * @throws PropertyTypeMismatchException If the property ID is not mapped as an object
177 */
178 void readIntFlag(int id, int value);
179
180 /**
Ashley Rosee8914812019-03-05 17:12:00 -0500181 * Read an integer that contains a resource ID.
182 *
183 * @param id Identifier of the property from a {@link PropertyMapper}
184 * @param value Value of the property
185 * @throws PropertyTypeMismatchException If the property ID is not mapped as a resource ID.
186 */
187 void readResourceId(int id, @AnyRes int value);
188
189 /**
Ashley Rose0f25a252018-11-02 16:36:52 -0400190 * Thrown if a client calls a typed read method for a property of a different type.
191 */
192 class PropertyTypeMismatchException extends RuntimeException {
193 public PropertyTypeMismatchException(
194 int id,
195 @NonNull String expectedPropertyType,
196 @NonNull String actualPropertyType,
197 @Nullable String propertyName) {
198 super(formatMessage(id, expectedPropertyType, actualPropertyType, propertyName));
199 }
200
201 public PropertyTypeMismatchException(
202 int id,
203 @NonNull String expectedPropertyType,
204 @NonNull String actualPropertyType) {
205 super(formatMessage(id, expectedPropertyType, actualPropertyType, null));
206 }
207
208 private static @NonNull String formatMessage(
209 int id,
210 @NonNull String expectedPropertyType,
211 @NonNull String actualPropertyType,
212 @Nullable String propertyName) {
213
214 if (propertyName == null) {
215 return String.format(
216 "Attempted to read property with ID 0x%08X as type %s, "
217 + "but the ID is of type %s.",
218 id,
219 expectedPropertyType,
220 actualPropertyType
221 );
222 } else {
223 return String.format(
224 "Attempted to read property \"%s\" with ID 0x%08X as type %s, "
225 + "but the ID is of type %s.",
226 propertyName,
227 id,
228 expectedPropertyType,
229 actualPropertyType
230 );
231 }
232 }
233 }
234}