Add EntityCollection
This commit is contained in:
@@ -6,7 +6,7 @@ interface EntityI<T> {
|
||||
var id: T
|
||||
}
|
||||
abstract class Entity<T>(override var id: T) : EntityI<T>
|
||||
abstract class UuidEntity(override var id: UUID = UUID.randomUUID()) : Entity<UUID>(id) {}
|
||||
abstract class UuidEntity(override var id: UUID = UUID.randomUUID()) : Entity<UUID>(id)
|
||||
abstract class IdEntity(override var id: Int) : Entity<Int>(id)
|
||||
|
||||
interface EntityVersioning<T> {
|
||||
|
||||
13
src/main/kotlin/fr/postgresjson/entity/EntityCollection.kt
Normal file
13
src/main/kotlin/fr/postgresjson/entity/EntityCollection.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package fr.postgresjson.entity
|
||||
|
||||
class EntityCollection<T, E : EntityI<T>> {
|
||||
var collection: MutableMap<T, E> = mutableMapOf()
|
||||
|
||||
fun get(id: T): E? {
|
||||
return collection[id]
|
||||
}
|
||||
|
||||
fun set(entity: E) {
|
||||
collection.set(entity.id, entity)
|
||||
}
|
||||
}
|
||||
37
src/main/kotlin/fr/postgresjson/repository/Repository.kt
Normal file
37
src/main/kotlin/fr/postgresjson/repository/Repository.kt
Normal file
@@ -0,0 +1,37 @@
|
||||
package fr.postgresjson.repository
|
||||
|
||||
import fr.postgresjson.entity.EntityCollection
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.serializer.Serializer
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
interface RepositoryI<T, E : EntityI<T>>
|
||||
|
||||
abstract class Repository<T, E : EntityI<T>> : RepositoryI<T, E> {
|
||||
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any>>> = mutableMapOf()
|
||||
|
||||
private inline fun <I, reified R : EntityI<I>> get(id: I): R? {
|
||||
val collection = collections[R::class]
|
||||
val entity = collection?.get(id!!)
|
||||
return entity as R?
|
||||
}
|
||||
|
||||
private inline fun <I, reified R : EntityI<I>> set(entity: R) {
|
||||
if (collections[R::class] == null) {
|
||||
collections[R::class] = EntityCollection()
|
||||
}
|
||||
|
||||
collections[R::class]!!.set(entity as EntityI<Any>)
|
||||
}
|
||||
|
||||
fun <T> findById(id: T): EntityI<T>? {
|
||||
return when (val e = get(id)) {
|
||||
null -> {
|
||||
// TODO create Request
|
||||
Serializer().deserialize<T, EntityI<T>>("""{"plop", "plip"}""")
|
||||
}
|
||||
else -> e
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,24 @@ package fr.postgresjson.serializer
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import com.fasterxml.jackson.module.kotlin.readValue
|
||||
import fr.postgresjson.entity.Entity
|
||||
import fr.postgresjson.entity.EntityCollection
|
||||
import fr.postgresjson.entity.EntityI
|
||||
|
||||
class Serializer(val mapper: ObjectMapper = jacksonObjectMapper()) {
|
||||
fun serialize(source: Any): String {
|
||||
fun <T> serialize(source: EntityI<T>): String {
|
||||
return mapper.writeValueAsString(source)
|
||||
}
|
||||
|
||||
inline fun <reified T>deserialize(json: String): T {
|
||||
return mapper.readValue(json)
|
||||
inline fun <T, reified E : EntityI<T>> deserialize(json: String): E {
|
||||
val unserialized = mapper.readValue<E>(json)
|
||||
return EntityCollection<T, E>().get(unserialized.id) ?: unserialized
|
||||
}
|
||||
|
||||
inline fun <reified T>deserialize(json: String, target: T): T {
|
||||
return mapper.readerForUpdating(target).readValue(json)
|
||||
fun <T, E : EntityI<T>> deserialize(json: String, target: E): E {
|
||||
val unserialized = mapper.readerForUpdating(target).readValue<E>(json)
|
||||
return EntityCollection<T, E>().get(unserialized.id) ?: unserialized
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Entity<T>.serialize() = Serializer().serialize(this)
|
||||
inline fun <reified T> T.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
fun <T> EntityI<T>.serialize() = Serializer().serialize(this)
|
||||
fun <T, E : EntityI<T>> E.deserialize(json: String) = Serializer().deserialize(json, this)
|
||||
Reference in New Issue
Block a user