1 /*
2  * Copyright 2019 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.processor.view.inspector.cts;
18 
19 import android.view.inspector.InspectableProperty.ValueType;
20 import android.view.inspector.PropertyMapper;
21 
22 import java.util.HashMap;
23 import java.util.Set;
24 import java.util.function.IntFunction;
25 
26 class TestPropertyMapper implements PropertyMapper {
27     private final HashMap<String, Integer> mPropertyIds = new HashMap<>();
28     private final HashMap<String, Integer> mAttributeIds = new HashMap<>();
29     private final HashMap<String, ValueType> mValueTypes = new HashMap<>();
30     private final HashMap<String, IntFunction<String>> mIntEnumMappings = new HashMap<>();
31     private final HashMap<String, IntFunction<Set<String>>> mIntFlagMappings = new HashMap<>();
32     private int mNextId = 1;
33 
getId(String name)34     int getId(String name) {
35         return mPropertyIds.getOrDefault(name, 0);
36     }
37 
getAttributeId(String name)38     int getAttributeId(String name) {
39         return mAttributeIds.getOrDefault(name, 0);
40     }
41 
getValueType(String name)42     ValueType getValueType(String name) {
43         return mValueTypes.getOrDefault(name, ValueType.NONE);
44     }
45 
getIntEnumMapping(String name)46     IntFunction<String> getIntEnumMapping(String name) {
47         return mIntEnumMappings.get(name);
48     }
49 
getIntFlagMapping(String name)50     IntFunction<Set<String>> getIntFlagMapping(String name) {
51         return mIntFlagMappings.get(name);
52     }
53 
54     @Override
mapBoolean(String name, int attributeId)55     public int mapBoolean(String name, int attributeId) {
56         return map(name, attributeId);
57     }
58 
59     @Override
mapByte(String name, int attributeId)60     public int mapByte(String name, int attributeId) {
61         return map(name, attributeId);
62     }
63 
64     @Override
mapChar(String name, int attributeId)65     public int mapChar(String name, int attributeId) {
66         return map(name, attributeId);
67     }
68 
69     @Override
mapDouble(String name, int attributeId)70     public int mapDouble(String name, int attributeId) {
71         return map(name, attributeId);
72     }
73 
74     @Override
mapFloat(String name, int attributeId)75     public int mapFloat(String name, int attributeId) {
76         return map(name, attributeId);
77     }
78 
79     @Override
mapInt(String name, int attributeId)80     public int mapInt(String name, int attributeId) { return map(name, attributeId); }
81 
82     @Override
mapLong(String name, int attributeId)83     public int mapLong(String name, int attributeId) {
84         return map(name, attributeId);
85     }
86 
87     @Override
mapShort(String name, int attributeId)88     public int mapShort(String name, int attributeId) {
89         return map(name, attributeId);
90     }
91 
92     @Override
mapObject(String name, int attributeId)93     public int mapObject(String name, int attributeId) {
94         return map(name, attributeId);
95     }
96 
97     @Override
mapColor(String name, int attributeId)98     public int mapColor(String name, int attributeId) {
99         return map(name, attributeId, ValueType.COLOR);
100     }
101 
102     @Override
mapGravity(String name, int attributeId)103     public int mapGravity(String name, int attributeId) {
104         return map(name, attributeId, ValueType.GRAVITY);
105     }
106 
107     @Override
mapIntEnum(String name, int attributeId, IntFunction<String> intEnumMapping)108     public int mapIntEnum(String name, int attributeId, IntFunction<String> intEnumMapping) {
109         mIntEnumMappings.put(name, intEnumMapping);
110         return map(name, attributeId, ValueType.INT_ENUM);
111     }
112 
113     @Override
mapIntFlag(String name, int attributeId, IntFunction<Set<String>> intFlagMapping)114     public int mapIntFlag(String name, int attributeId, IntFunction<Set<String>> intFlagMapping) {
115         mIntFlagMappings.put(name, intFlagMapping);
116         return map(name, attributeId, ValueType.INT_FLAG);
117     }
118 
119     @Override
mapResourceId(String name, int attributeId)120     public int mapResourceId(String name, int attributeId) {
121         return map(name, attributeId, ValueType.RESOURCE_ID);
122     }
123 
map(String name, int attributeId)124     private int map(String name, int attributeId) {
125         return map(name, attributeId, ValueType.NONE);
126     }
127 
map(String name, int attributeId, ValueType valueType)128     private int map(String name, int attributeId, ValueType valueType) {
129         if (mPropertyIds.containsKey(name)) {
130             throw new PropertyConflictException(name, "all", "all");
131         }
132         mAttributeIds.put(name, attributeId);
133         mValueTypes.put(name, valueType);
134         return mPropertyIds.computeIfAbsent(name, n -> mNextId++);
135     }
136 }
137