-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
71 additions
and
30 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...urion-avro/src/main/kotlin/com/sksamuel/centurion/avro/io/CachedReflectionSerdeFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.sksamuel.centurion.avro.io | ||
|
||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* A [CachedReflectionSerdeFactory] will create a [Serde] once for a given type via delegation | ||
* to a [ReflectionSerdeFactory] and return that cached [Serde] upon future invocations. | ||
* | ||
* This instance is thread safe. | ||
*/ | ||
object CachedReflectionSerdeFactory { | ||
|
||
private val cache = ConcurrentHashMap<KClass<*>, Serde<*>>() | ||
|
||
/** | ||
* Creates or returns a [Serde] for the given [kclass]. | ||
*/ | ||
fun <T : Any> create( | ||
kclass: KClass<T>, | ||
options: SerdeOptions = SerdeOptions() | ||
): Serde<T> { | ||
return cache.getOrPut(kclass) { ReflectionSerdeFactory.create(kclass, options) } as Serde<T> | ||
} | ||
|
||
/** | ||
* Creates or returns a [Serde] from the given type parameter [T]. | ||
*/ | ||
inline fun <reified T : Any> create(options: SerdeOptions = SerdeOptions()): Serde<T> { | ||
return create(T::class, options) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
centurion-avro/src/main/kotlin/com/sksamuel/centurion/avro/io/ReflectionSerdeFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.sksamuel.centurion.avro.io | ||
|
||
import com.sksamuel.centurion.avro.decoders.SpecificRecordDecoder | ||
import com.sksamuel.centurion.avro.encoders.SpecificRecordEncoder | ||
import com.sksamuel.centurion.avro.generation.ReflectionSchemaBuilder | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* A [ReflectionSerdeFactory] will create a [Serde] for any given type using reflection based builders. | ||
* | ||
* This instance is thread safe. | ||
*/ | ||
object ReflectionSerdeFactory { | ||
|
||
/** | ||
* Creates a [Serde] reflectively from the given [kclass] using a [ReflectionSchemaBuilder]. | ||
*/ | ||
fun <T : Any> create( | ||
kclass: KClass<T>, | ||
options: SerdeOptions = SerdeOptions() | ||
): Serde<T> { | ||
val schema = ReflectionSchemaBuilder(true).schema(kclass) | ||
val encoder = SpecificRecordEncoder(kclass) | ||
val decoder: SpecificRecordDecoder<T> = SpecificRecordDecoder(kclass) | ||
return Serde(schema, encoder, decoder, options) | ||
} | ||
|
||
/** | ||
* Creates a [Serde] reflectively from the given type parameter [T] using a [ReflectionSchemaBuilder]. | ||
*/ | ||
inline fun <reified T : Any> create(options: SerdeOptions = SerdeOptions()): Serde<T> { | ||
return create(T::class, options) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters