refactoring: EntitiesCollections & add EntityI.className

This commit is contained in:
2019-06-24 15:47:18 +02:00
parent b3d9e7624b
commit 96b335875d
5 changed files with 26 additions and 13 deletions

View File

@@ -59,7 +59,9 @@ class Connection(
private fun compileArgs(values: List<Any?>): List<Any?> {
return values.map {
if (it is EntityI<*>) {
serializer.serialize(it)
val json = serializer.serialize(it)
serializer.collection.set<Any?, EntityI<Any?>>(it as EntityI<Any?>)
json
} else {
it
}

View File

@@ -2,21 +2,27 @@ package fr.postgresjson.entity
import kotlin.reflect.KClass
class EntityCollection {
val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
class EntitiesCollections {
private val collections: MutableMap<KClass<*>, EntityCollection<Any, EntityI<Any?>>> = mutableMapOf()
inline fun <I, reified R : EntityI<I?>> get(id: I): R? {
val collection = collections[R::class]
fun <I, R : EntityI<I?>> get(id: I, className: KClass<R>): R? {
val collection = collections[className]
val entity = collection?.get(id!!)
return entity as R?
}
inline fun <I, reified R : EntityI<I?>> set(entity: R) {
if (collections[R::class] == null) {
collections[R::class] = EntityCollection()
inline fun <I, reified R : EntityI<I?>> get(id: I): R? {
return get(id, R::class)
}
fun <I, R : EntityI<out I?>> set(entity: R): EntitiesCollections {
if (collections[entity.className] == null) {
collections[entity.className] = EntityCollection()
}
collections[R::class]!!.set(entity as EntityI<Any?>)
collections[entity.className]!!.set(entity as EntityI<Any?>)
return this
}
class EntityCollection<T, E : EntityI<T?>> {

View File

@@ -1,11 +1,15 @@
package fr.postgresjson.entity
import com.fasterxml.jackson.annotation.JsonIgnore
import org.joda.time.DateTime
import java.util.*
import kotlin.reflect.KClass
/* ID */
interface EntityI<T> {
var id: T?
val className: KClass<EntityI<T?>>
@JsonIgnore() get() = this::class as KClass<EntityI<T?>>
}
abstract class Entity<T>(override var id: T? = null) : EntityI<T?>

View File

@@ -2,7 +2,7 @@ package fr.postgresjson.repository
import fr.postgresjson.Serializer
import fr.postgresjson.connexion.Requester
import fr.postgresjson.entity.EntityCollection
import fr.postgresjson.entity.EntitiesCollections
import fr.postgresjson.entity.EntityI
import kotlin.reflect.KClass
@@ -17,7 +17,7 @@ abstract class Repository<T, E : EntityI<T?>>(override val entityName: KClass<E>
fun <T> findById(id: T): EntityI<T?>? {
val sql = requester.getQuery(entityName.toString())
return when (val e = EntityCollection().get(id)) {
return when (val e = EntitiesCollections().get(id)) {
null -> {
// TODO create Request
Serializer().deserialize<T, EntityI<T?>>("""{"plop", "plip"}""")