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

jsMain.kotlinx.io.pool.DefaultPool.kt Maven / Gradle / Ivy

package kotlinx.io.pool

actual abstract class DefaultPool actual constructor(actual final override val capacity: Int) : ObjectPool {
    private val instances = arrayOfNulls(capacity)
    private var size = 0

    protected actual abstract fun produceInstance(): T
    protected actual open fun disposeInstance(instance: T) {}

    protected actual open fun clearInstance(instance: T): T = instance
    protected actual open fun validateInstance(instance: T) {}

    actual final override fun borrow(): T {
        if (size == 0) return produceInstance()
        val idx = --size

        @Suppress("UNCHECKED_CAST")
        val instance = instances[idx] as T
        instances[idx] = null

        return clearInstance(instance)
    }

    actual final override fun recycle(instance: T) {
        validateInstance(instance)
        if (size == capacity) {
            disposeInstance(instance)
        } else {
            instances[size++] = instance
        }
    }

    actual final override fun dispose() {
        for (i in 0 until size) {
            @Suppress("UNCHECKED_CAST")
            val instance = instances[i] as T
            instances[i] = null
            disposeInstance(instance)
        }
        size = 0
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy