blob: 00b18d172eed0ad624ddd99fad94b1a53e51999c [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 Rosed2c5f452018-11-29 15:40:10 -050019import android.annotation.AttrRes;
Ashley Rose0f25a252018-11-02 16:36:52 -040020import android.annotation.NonNull;
21
22/**
23 * An interface for mapping the string names of inspectable properties to integer identifiers.
24 *
Ashley Rosed2c5f452018-11-29 15:40:10 -050025 * This interface is consumed by {@link InspectionCompanion#mapProperties(PropertyMapper)}.
Ashley Rose0f25a252018-11-02 16:36:52 -040026 *
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 Rosed2c5f452018-11-29 15:40:10 -050030 * @see InspectionCompanion#mapProperties(PropertyMapper)
Ashley Rose0f25a252018-11-02 16:36:52 -040031 */
32public 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 Rosed2c5f452018-11-29 15:40:10 -050037 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040038 * @return An integer ID for the property
39 * @throws PropertyConflictException If the property name is already mapped as another type.
40 */
Ashley Rosed2c5f452018-11-29 15:40:10 -050041 int mapBoolean(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -040042
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 Rosed2c5f452018-11-29 15:40:10 -050047 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040048 * @return An integer ID for the property
49 * @throws PropertyConflictException If the property name is already mapped as another type.
50 */
Ashley Rosed2c5f452018-11-29 15:40:10 -050051 int mapByte(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -040052
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 Rosed2c5f452018-11-29 15:40:10 -050057 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040058 * @return An integer ID for the property
59 * @throws PropertyConflictException If the property name is already mapped as another type.
60 */
Ashley Rosed2c5f452018-11-29 15:40:10 -050061 int mapChar(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -040062
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 Rosed2c5f452018-11-29 15:40:10 -050067 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040068 * @return An integer ID for the property
69 * @throws PropertyConflictException If the property name is already mapped as another type.
70 */
Ashley Rosed2c5f452018-11-29 15:40:10 -050071 int mapDouble(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -040072
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 Rosed2c5f452018-11-29 15:40:10 -050077 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040078 * @return An integer ID for the property
79 * @throws PropertyConflictException If the property name is already mapped as another type.
80 */
Ashley Rosed2c5f452018-11-29 15:40:10 -050081 int mapFloat(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -040082
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 Rosed2c5f452018-11-29 15:40:10 -050087 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040088 * @return An integer ID for the property
89 * @throws PropertyConflictException If the property name is already mapped as another type.
90 */
Ashley Rosed2c5f452018-11-29 15:40:10 -050091 int mapInt(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -040092
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 Rosed2c5f452018-11-29 15:40:10 -050097 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -040098 * @return An integer ID for the property
99 * @throws PropertyConflictException If the property name is already mapped as another type.
100 */
Ashley Rosed2c5f452018-11-29 15:40:10 -0500101 int mapLong(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -0400102
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 Rosed2c5f452018-11-29 15:40:10 -0500107 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -0400108 * @return An integer ID for the property
109 * @throws PropertyConflictException If the property name is already mapped as another type.
110 */
Ashley Rosed2c5f452018-11-29 15:40:10 -0500111 int mapShort(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -0400112
113 /**
114 * Map a string name to an integer ID for an object property.
115 *
116 * @param name The name of the property
Ashley Rosed2c5f452018-11-29 15:40:10 -0500117 * @param attributeId If the property is from an XML attribute, the resource ID of the property
Ashley Rose0f25a252018-11-02 16:36:52 -0400118 * @return An integer ID for the property
119 * @throws PropertyConflictException If the property name is already mapped as another type.
120 */
Ashley Rosed2c5f452018-11-29 15:40:10 -0500121 int mapObject(@NonNull String name, @AttrRes int attributeId);
Ashley Rose0f25a252018-11-02 16:36:52 -0400122
123 /**
Ashley Rosed2c5f452018-11-29 15:40:10 -0500124 * 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 Rosea61e7cd2019-01-23 14:37:42 -0500157 @NonNull IntEnumMapping mapping);
Ashley Rosed2c5f452018-11-29 15:40:10 -0500158
159 /**
160 * Map a string name to an integer ID for a flag set packed into an int property.
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
Ashley Rosea61e7cd2019-01-23 14:37:42 -0500164 * @param mapping A mapping from int to a set of strings
Ashley Rosed2c5f452018-11-29 15:40:10 -0500165 * @return An integer ID for the property
166 * @throws PropertyConflictException If the property name is already mapped as another type.
167 */
168 int mapIntFlag(
169 @NonNull String name,
170 @AttrRes int attributeId,
171 @NonNull IntFlagMapping mapping);
172 /**
Ashley Rose0f25a252018-11-02 16:36:52 -0400173 * Thrown from a map method if a property name is already mapped as different type.
174 */
175 class PropertyConflictException extends RuntimeException {
176 public PropertyConflictException(
177 @NonNull String name,
178 @NonNull String newPropertyType,
179 @NonNull String existingPropertyType) {
180 super(String.format(
181 "Attempted to map property \"%s\" as type %s, but it is already mapped as %s.",
182 name,
183 newPropertyType,
184 existingPropertyType
185 ));
186 }
187 }
188}