Address JDK8/JDK6 issues with computeIfAbsent

This commit is contained in:
Irene Dea
2021-12-01 14:09:54 -08:00
committed by Dmitriy Novozhilov
parent 374d287d08
commit 3c4989b672
8 changed files with 45 additions and 6 deletions
@@ -78,9 +78,14 @@ abstract class TypeRegistry<K : Any, V : Any> {
}
fun <T : K> getId(kClass: KClass<T>): Int {
return idPerType.computeIfAbsent(kClass) { idCounter.getAndIncrement() }
return idPerType.customComputeIfAbsent(kClass) { idCounter.getAndIncrement() }
}
abstract fun <T : K> ConcurrentHashMap<KClass<out K>, Int>.customComputeIfAbsent(
kClass: KClass<T>,
compute: (KClass<out K>) -> Int
): Int
fun allValuesThreadUnsafeForRendering(): Map<KClass<out K>, Int> {
return idPerType
}
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.types.model.AnnotationMarker
import org.jetbrains.kotlin.util.AttributeArrayOwner
import org.jetbrains.kotlin.util.TypeRegistry
import org.jetbrains.kotlin.utils.addIfNotNull
import java.util.concurrent.ConcurrentHashMap
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
@@ -41,6 +42,15 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
return generateNullableAccessor<TypeAttribute<*>, T>(T::class) as ReadOnlyProperty<TypeAttributes, T?>
}
override fun <T : TypeAttribute<*>> ConcurrentHashMap<KClass<out TypeAttribute<*>>, Int>.customComputeIfAbsent(
kClass: KClass<T>,
compute: (KClass<out TypeAttribute<*>>) -> Int
): Int {
return this[kClass] ?: synchronized(this) {
this[kClass] ?: compute(kClass).also { this.putIfAbsent(kClass, it) }
}
}
val Empty: TypeAttributes = TypeAttributes(listOf(AnnotationsTypeAttribute(Annotations.EMPTY)))
fun create(attributes: List<TypeAttribute<*>>): TypeAttributes {