Address JDK8/JDK6 issues with computeIfAbsent
This commit is contained in:
committed by
Dmitriy Novozhilov
parent
374d287d08
commit
3c4989b672
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user