xref: /aosp_15_r20/external/leakcanary2/shark-graph/src/main/java/shark/GraphContext.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package shark
2 
3 /**
4  * In memory store that can be used to store objects in a given [HeapGraph] instance.
5  * This is a simple [MutableMap] of [String] to [Any], but with unsafe generics access.
6  */
7 class GraphContext {
8   private val store = mutableMapOf<String, Any?>()
getnull9   operator fun <T> get(key: String): T? {
10     @Suppress("UNCHECKED_CAST")
11     return store[key] as T?
12   }
13 
14   /**
15    * @see MutableMap.getOrPut
16    */
getOrPutnull17   fun <T> getOrPut(
18     key: String,
19     defaultValue: () -> T
20   ): T {
21     @Suppress("UNCHECKED_CAST")
22     return store.getOrPut(key) {
23       defaultValue()
24     } as T
25   }
26 
27   /**
28    * @see MutableMap.set
29    */
setnull30   operator fun <T> set(
31     key: String,
32     value: T
33   ) {
34     store[key] = (value as Any?)
35   }
36 
37   /**
38    * @see MutableMap.containsKey
39    */
containsnull40   operator fun contains(key: String): Boolean {
41     return key in store
42   }
43 
44   /**
45    * @see MutableMap.remove
46    */
minusAssignnull47   operator fun minusAssign(key: String) {
48     @Suppress("UNCHECKED_CAST")
49     store -= key
50   }
51 }
52