org.jetbrains.kotlin.util.AttributeArrayOwner.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.util
import kotlin.reflect.KClass
/**
* [AttributeArrayOwner] based on different implementations of [ArrayMap] and switches them
* depending on array map fullness
* [AttributeArrayOwner] can be used in classes with many instances,
* like user data for Fir elements or attributes for cone types
*
* Note that you can remove attributes from [AttributeArrayOwner] despite
* from components in [ComponentArrayOwner]
*/
abstract class AttributeArrayOwner protected constructor(
arrayMap: ArrayMap
) : AbstractArrayMapOwner() {
final override var arrayMap: ArrayMap = arrayMap
private set
@Suppress("UNCHECKED_CAST")
constructor() : this(EmptyArrayMap as ArrayMap)
final override fun registerComponent(keyQualifiedName: String, value: T) {
val id = typeRegistry.getId(keyQualifiedName)
when (arrayMap.size) {
0 -> {
arrayMap = OneElementArrayMap(value, id)
return
}
1 -> {
val map = arrayMap as OneElementArrayMap
if (map.index == id) {
arrayMap = OneElementArrayMap(value, id)
return
} else {
arrayMap = ArrayMapImpl()
arrayMap[map.index] = map.value
}
}
}
arrayMap[id] = value
}
protected fun removeComponent(tClass: KClass) {
val id = typeRegistry.getId(tClass)
if (arrayMap[id] == null) return
@Suppress("UNCHECKED_CAST")
when (arrayMap.size) {
1 -> arrayMap = EmptyArrayMap as ArrayMap
else -> {
val map = arrayMap as ArrayMapImpl
map.remove(id)
if (map.size == 1) {
val (index, value) = map.entries().first()
arrayMap = OneElementArrayMap(value, index)
}
}
}
}
}