Add Connection and refactoring EntityCollection

This commit is contained in:
2019-06-03 17:29:05 +02:00
parent 65fb032ad9
commit c5d4bfb07a
7 changed files with 177 additions and 37 deletions

View File

@@ -3,11 +3,11 @@ package fr.postgresjson.entity
import java.util.*
interface EntityI<T> {
var id: 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 IdEntity(override var id: Int) : Entity<Int>(id)
abstract class Entity<T>(override var id: T? = null) : EntityI<T?>
abstract class UuidEntity(override var id: UUID? = UUID.randomUUID()) : Entity<UUID?>(id)
abstract class IdEntity(override var id: Int? = null) : Entity<Int?>(id)
interface EntityVersioning<T> {
var version: T

View File

@@ -1,13 +1,36 @@
package fr.postgresjson.entity
class EntityCollection<T, E : EntityI<T>> {
var collection: MutableMap<T, E> = mutableMapOf()
import kotlin.reflect.KClass
fun get(id: T): E? {
return collection[id]
class EntityCollection {
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]
val entity = collection?.get(id!!)
return entity as R?
}
fun set(entity: E) {
collection.set(entity.id, entity)
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?>)
}
class EntityCollection<T, E : EntityI<T?>> {
private var collection: MutableMap<T, E> = mutableMapOf()
fun get(id: T): E? {
return collection[id]
}
fun set(entity: E) {
val id = entity.id
if (id !== null) {
collection[id] = entity
}
}
}
}