implementations.FactoryCodecProvider.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluid-json-coding Show documentation
Show all versions of fluid-json-coding Show documentation
A JSON library written in pure Kotlin (coding extension)
package com.github.fluidsonic.fluid.json
import java.util.concurrent.*
import kotlin.reflect.*
internal class FactoryCodecProvider internal constructor(
private val valueClass: KClass,
private val factory: (actualClass: KClass) -> JSONCodecProvider?
) : JSONCodecProvider {
private val codecsByClass = ConcurrentHashMap, Codecs<*, Context>>()
override fun decoderCodecForType(decodableType: JSONCodingType): JSONDecoderCodec? =
decodableType.rawClass.takeIfSubclassOf(valueClass)
?.let { codecsForClass(it).decoderCodec }
override fun encoderCodecForClass(encodableClass: KClass): JSONEncoderCodec? =
encodableClass.takeIfSubclassOf(valueClass)
?.let { codecsForClass(it).encoderCodec }
@Suppress("UNCHECKED_CAST")
private fun codecsForClass(actualClass: KClass) =
codecsByClass.getOrPut(actualClass) {
factory(actualClass as KClass)
?.let { provider ->
Codecs(
decoderCodec = provider.decoderCodecForType(jsonCodingType(actualClass)),
encoderCodec = provider.encoderCodecForClass(actualClass)
)
}
?: Codecs.empty()
} as Codecs
private data class Codecs(
val decoderCodec: JSONDecoderCodec?,
val encoderCodec: JSONEncoderCodec?
) {
companion object {
private val empty: Codecs = Codecs(decoderCodec = null, encoderCodec = null)
@Suppress("UNCHECKED_CAST")
fun empty() =
empty as Codecs
}
}
}
@JvmName("JSONCodecProviderForDecoding")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factory(
factory: (valueClass: KClass) -> JSONDecoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Any::class, factory = factory)
@JvmName("JSONCodecProviderForDecodingWithContext")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factory(
factory: (valueClass: KClass) -> JSONDecoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Any::class, factory = factory)
@JvmName("JSONCodecProviderForEncoding")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factory(
factory: (valueClass: KClass) -> JSONEncoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Any::class, factory = factory)
@JvmName("JSONCodecProviderForEncodingWithContext")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factory(
factory: (valueClass: KClass) -> JSONEncoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Any::class, factory = factory)
@JvmName("JSONCodecProviderForCoding")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factory(
factory: (valueClass: KClass) -> JSONCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Any::class, factory = factory)
@JvmName("JSONCodecProviderForCodingWithContext")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factory(
factory: (valueClass: KClass) -> JSONCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Any::class, factory = factory)
@JvmName("JSONCodecProviderForDecodingSpecificValue")
@Suppress("FunctionName")
inline fun JSONCodecProvider.Companion.factoryOf(
noinline factory: (valueClass: KClass) -> JSONDecoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Value::class, factory = factory)
@JvmName("JSONCodecProviderForDecodingSpecificValue")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factoryOf(
valueClass: KClass,
factory: (valueClass: KClass) -> JSONDecoderCodec?
): JSONCodecProvider =
FactoryCodecProvider(valueClass = valueClass, factory = factory)
@JvmName("JSONCodecProviderForDecodingSpecificValueWithContext")
@Suppress("FunctionName")
inline fun JSONCodecProvider.Companion.factoryOf(
noinline factory: (valueClass: KClass) -> JSONDecoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Value::class, factory = factory)
@JvmName("JSONCodecProviderForDecodingSpecificValueWithContext")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factoryOf(
valueClass: KClass,
factory: (valueClass: KClass) -> JSONDecoderCodec?
): JSONCodecProvider =
FactoryCodecProvider(valueClass = valueClass, factory = factory)
@JvmName("JSONCodecProviderForEncodingSpecificValue")
@Suppress("FunctionName")
inline fun JSONCodecProvider.Companion.factoryOf(
noinline factory: (valueClass: KClass) -> JSONEncoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Value::class, factory = factory)
@JvmName("JSONCodecProviderForEncodingSpecificValue")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factoryOf(
valueClass: KClass,
factory: (valueClass: KClass) -> JSONEncoderCodec?
): JSONCodecProvider =
FactoryCodecProvider(valueClass = valueClass, factory = factory)
@JvmName("JSONCodecProviderForEncodingSpecificValueWithContext")
@Suppress("FunctionName")
inline fun JSONCodecProvider.Companion.factoryOf(
noinline factory: (valueClass: KClass) -> JSONEncoderCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Value::class, factory = factory)
@JvmName("JSONCodecProviderForEncodingSpecificValueWithContext")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factoryOf(
valueClass: KClass,
factory: (valueClass: KClass) -> JSONEncoderCodec?
): JSONCodecProvider =
FactoryCodecProvider(valueClass = valueClass, factory = factory)
@JvmName("JSONCodecProviderForCodingSpecificValue")
@Suppress("FunctionName")
inline fun JSONCodecProvider.Companion.factoryOf(
noinline factory: (valueClass: KClass) -> JSONCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Value::class, factory = factory)
@JvmName("JSONCodecProviderForCodingSpecificValue")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factoryOf(
valueClass: KClass,
factory: (valueClass: KClass) -> JSONCodec?
): JSONCodecProvider =
FactoryCodecProvider(valueClass = valueClass, factory = factory)
@JvmName("JSONCodecProviderForCodingSpecificValueWithContext")
@Suppress("FunctionName")
inline fun JSONCodecProvider.Companion.factoryOf(
noinline factory: (valueClass: KClass) -> JSONCodec?
): JSONCodecProvider =
JSONCodecProvider.factoryOf(valueClass = Value::class, factory = factory)
@JvmName("JSONCodecProviderForCodingSpecificValueWithContext")
@Suppress("FunctionName")
fun JSONCodecProvider.Companion.factoryOf(
valueClass: KClass,
factory: (valueClass: KClass) -> JSONCodec?
): JSONCodecProvider =
FactoryCodecProvider(valueClass = valueClass, factory = factory)