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

commonMain.kotlinx.serialization.json.internal.SchemaCache.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.json.internal

import kotlinx.serialization.descriptors.*
import kotlin.native.concurrent.*

private typealias DescriptorData = MutableMap, T>

/**
 * A type-safe map for storing custom information (such as format schema), associated with [SerialDescriptor].
 *
 * This cache uses ConcurrentHashMap on JVM and regular maps on other platforms.
 * To be able to work with it from multiple threads in Kotlin/Native, use @[ThreadLocal] in appropriate places.
 */
internal class DescriptorSchemaCache {
    // 16 is default CHM size, as we do not know number of descriptors in an application (but it's likely not 1)
    private val map: MutableMap> = createMapForCache(16)

    @Suppress("UNCHECKED_CAST")
    public operator fun  set(descriptor: SerialDescriptor, key: Key, value: T) {
        // Initial capacity = number of known DescriptorSchemaCache.Key instances
        map.getOrPut(descriptor, { createMapForCache(2) })[key as Key] = value as Any
    }

    public fun  getOrPut(descriptor: SerialDescriptor, key: Key, defaultValue: () -> T): T {
        get(descriptor, key)?.let { return it }
        val value = defaultValue()
        set(descriptor, key, value)
        return value
    }

    @Suppress("UNCHECKED_CAST")
    public operator fun  get(descriptor: SerialDescriptor, key: Key): T? {
        return map[descriptor]?.get(key as Key) as? T
    }

    /**
     * A key for associating user data of type [T] with a given [SerialDescriptor].
     */
    public class Key {}
}


/**
 * Creates a ConcurrentHashMap on JVM and regular HashMap on other platforms.
 * To make actual use of cache in Kotlin/Native, mark a top-level object with this map
 * as a @[ThreadLocal].
 */
internal expect fun  createMapForCache(initialCapacity: Int): MutableMap




© 2015 - 2024 Weber Informatics LLC | Privacy Policy