com.neko233.config233.reader.convert.TextConvertValueStrategyFactory.kt Maven / Gradle / Ivy
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
}
}