Home | History | Annotate | Download | only in cts
      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 
     34     int getId(String name) {
     35         return mPropertyIds.getOrDefault(name, 0);
     36     }
     37 
     38     int getAttributeId(String name) {
     39         return mAttributeIds.getOrDefault(name, 0);
     40     }
     41 
     42     ValueType getValueType(String name) {
     43         return mValueTypes.getOrDefault(name, ValueType.NONE);
     44     }
     45 
     46     IntFunction<String> getIntEnumMapping(String name) {
     47         return mIntEnumMappings.get(name);
     48     }
     49 
     50     IntFunction<Set<String>> getIntFlagMapping(String name) {
     51         return mIntFlagMappings.get(name);
     52     }
     53 
     54     @Override
     55     public int mapBoolean(String name, int attributeId) {
     56         return map(name, attributeId);
     57     }
     58 
     59     @Override
     60     public int mapByte(String name, int attributeId) {
     61         return map(name, attributeId);
     62     }
     63 
     64     @Override
     65     public int mapChar(String name, int attributeId) {
     66         return map(name, attributeId);
     67     }
     68 
     69     @Override
     70     public int mapDouble(String name, int attributeId) {
     71         return map(name, attributeId);
     72     }
     73 
     74     @Override
     75     public int mapFloat(String name, int attributeId) {
     76         return map(name, attributeId);
     77     }
     78 
     79     @Override
     80     public int mapInt(String name, int attributeId) { return map(name, attributeId); }
     81 
     82     @Override
     83     public int mapLong(String name, int attributeId) {
     84         return map(name, attributeId);
     85     }
     86 
     87     @Override
     88     public int mapShort(String name, int attributeId) {
     89         return map(name, attributeId);
     90     }
     91 
     92     @Override
     93     public int mapObject(String name, int attributeId) {
     94         return map(name, attributeId);
     95     }
     96 
     97     @Override
     98     public int mapColor(String name, int attributeId) {
     99         return map(name, attributeId, ValueType.COLOR);
    100     }
    101 
    102     @Override
    103     public int mapGravity(String name, int attributeId) {
    104         return map(name, attributeId, ValueType.GRAVITY);
    105     }
    106 
    107     @Override
    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
    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
    120     public int mapResourceId(String name, int attributeId) {
    121         return map(name, attributeId, ValueType.RESOURCE_ID);
    122     }
    123 
    124     private int map(String name, int attributeId) {
    125         return map(name, attributeId, ValueType.NONE);
    126     }
    127 
    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