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

eu.vendeli.tgbot.core.ClassManagerImpl.kt Maven / Gradle / Ivy

There is a newer version: 6.6.0
Show newest version
package eu.vendeli.tgbot.core

import eu.vendeli.tgbot.interfaces.ClassManager

/**
 * Default [ClassManager] implementation
 *
 * @constructor Create empty ClassManagerImpl
 */
class ClassManagerImpl : ClassManager {

    // keep class instances
    private val instances by lazy { mutableMapOf() }

    /**
     * Get instance of class
     *
     * @param clazz
     * @param initParams
     * @return class
     */
    override fun getInstance(clazz: Class<*>, vararg initParams: Any?): Any = instances.getOrElse(clazz.name) {
        if (initParams.isEmpty()) clazz.declaredConstructors.first().newInstance()
        else {
            clazz.declaredConstructors.first().newInstance(initParams)
        }.also { instances[clazz.name] = it }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy