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

org.jetbrains.kotlin.util.ArrayMapOwner.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 java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

@RequiresOptIn
annotation class Protected

abstract class AbstractArrayMapOwner : Iterable {
    protected abstract val arrayMap: ArrayMap
    protected abstract val typeRegistry: TypeRegistry

    abstract class AbstractArrayMapAccessor(
        protected val key: KClass,
        protected val id: Int
    ) {
        protected fun extractValue(thisRef: AbstractArrayMapOwner): T? {
            @Suppress("UNCHECKED_CAST")
            return thisRef.arrayMap[id] as T?
        }
    }

    protected abstract fun registerComponent(tClass: KClass, value: V)

    final override fun iterator(): Iterator = arrayMap.iterator()

    fun isEmpty(): Boolean = arrayMap.size == 0

    fun isNotEmpty(): Boolean = arrayMap.size != 0

    operator fun get(index: Int): V? = arrayMap[index]
}

class ArrayMapAccessor(
    key: KClass,
    id: Int,
    val default: T? = null
) : AbstractArrayMapOwner.AbstractArrayMapAccessor(key, id), ReadOnlyProperty, V> {
    override fun getValue(thisRef: AbstractArrayMapOwner, property: KProperty<*>): T {
        return extractValue(thisRef)
            ?: default
            ?: error("No '$key'($id) in array owner: $thisRef")
    }
}

class NullableArrayMapAccessor(
    key: KClass,
    id: Int
) : AbstractArrayMapOwner.AbstractArrayMapAccessor(key, id), ReadOnlyProperty, V?> {
    override fun getValue(thisRef: AbstractArrayMapOwner, property: KProperty<*>): T? {
        return extractValue(thisRef)
    }
}

abstract class TypeRegistry {
    private val idPerType = ConcurrentHashMap, Int>()
    private val idCounter = AtomicInteger(0)


    fun  generateAccessor(kClass: KClass, default: T? = null): ArrayMapAccessor {
        return ArrayMapAccessor(kClass, getId(kClass), default)
    }

    fun  generateNullableAccessor(kClass: KClass): NullableArrayMapAccessor {
        return NullableArrayMapAccessor(kClass, getId(kClass))
    }

    fun  generateAnyNullableAccessor(kClass: KClass): NullableArrayMapAccessor {
        return NullableArrayMapAccessor(kClass, getId(kClass))
    }

    fun  getId(kClass: KClass): Int {
        return idPerType.customComputeIfAbsent(kClass) { idCounter.getAndIncrement() }
    }

    abstract fun  ConcurrentHashMap, Int>.customComputeIfAbsent(
        kClass: KClass,
        compute: (KClass) -> Int
    ): Int

    fun allValuesThreadUnsafeForRendering(): Map, Int> {
        return idPerType
    }

    protected val indices: Collection
        get() = idPerType.values
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy