1 /*
<lambda>null2  * Copyright (C) 2024 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 com.android.systemui.kairos.internal.util
18 
19 import java.util.concurrent.ConcurrentHashMap
20 
21 internal class ConcurrentNullableHashMap<K : Any, V>
22 private constructor(private val inner: ConcurrentHashMap<K, Any>) {
23     constructor() : this(ConcurrentHashMap())
24 
25     @Suppress("UNCHECKED_CAST")
26     operator fun get(key: K): V? = inner[key]?.takeIf { it !== NullValue } as V?
27 
28     @Suppress("UNCHECKED_CAST")
29     fun put(key: K, value: V?): V? =
30         inner.put(key, value ?: NullValue)?.takeIf { it !== NullValue } as V?
31 
32     operator fun set(key: K, value: V?) {
33         put(key, value)
34     }
35 
36     @Suppress("UNCHECKED_CAST")
37     fun toMap(): Map<K, V> = inner.mapValues { (_, v) -> v.takeIf { it !== NullValue } as V }
38 
39     fun clear() {
40         inner.clear()
41     }
42 
43     fun isNotEmpty(): Boolean = inner.isNotEmpty()
44 }
45 
46 private object NullValue
47