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

com.neko233.config233.reader.convert.TextConvertValueStrategyFactory.kt Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package com.neko233.config233.reader.convert

import com.neko233.config233.utils.PackageScannerForConfig233

/**
 * text value -> value by fieldType(Class) 工厂
 */
class TextConvertValueStrategyFactory {

    companion object {
        @JvmStatic
        val instance: TextConvertValueStrategyFactory = TextConvertValueStrategyFactory()
    }

    private val strategies = mutableMapOf, JavaTypeConvertTextValueStrategy<*>>()

    init {
        val implClassList = PackageScannerForConfig233.scanClass(this::class.java, true) {
            if (it == JavaTypeConvertTextValueStrategy::class.java) {
                return@scanClass false
            }
            return@scanClass JavaTypeConvertTextValueStrategy::class.java.isAssignableFrom(it)
        }

        for (clazz in implClassList) {
            val obj = clazz.newInstance() as JavaTypeConvertTextValueStrategy
            registerStrategy(obj)
        }
    }

    fun  getStrategy(dataType: Class): JavaTypeConvertTextValueStrategy? {
        return strategies[dataType] as JavaTypeConvertTextValueStrategy?
    }

    private fun registerStrategy(strategy: JavaTypeConvertTextValueStrategy<*>) {
        strategies[strategy.dataType()] = strategy
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy