1 /*
2  * Copyright 2022 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 androidx.compose.ui.res
18 
19 import android.content.res.Resources
20 import android.util.TypedValue
21 import androidx.annotation.DrawableRes
22 import androidx.collection.MutableIntObjectMap
23 
24 internal class ResourceIdCache {
25 
26     private val resIdPathMap = MutableIntObjectMap<TypedValue>()
27 
28     /**
29      * Resolve the path of the provided resource identifier within the given resources object. This
30      * first checks its internal cache before attempting to resolve the resource with the Android
31      * resource system
32      */
resolveResourcePathnull33     fun resolveResourcePath(res: Resources, @DrawableRes id: Int): TypedValue {
34         synchronized(this) {
35             var value = resIdPathMap[id]
36             if (value == null) {
37                 value = TypedValue()
38                 res.getValue(id, value, true)
39                 resIdPathMap.put(id, value)
40             }
41             return value
42         }
43     }
44 
clearnull45     fun clear() {
46         synchronized(this) { resIdPathMap.clear() }
47     }
48 }
49