All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.util.AttributeArrayOwner.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * 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(tClass: KClass, value: T) {
        val id = typeRegistry.getId(tClass)
        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)
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy