Fix MetaItem equality

This commit is contained in:
Alexander Nozik 2020-06-05 21:08:41 +03:00
parent fc0454d339
commit 086fcd441b
4 changed files with 34 additions and 13 deletions

View File

@ -7,7 +7,7 @@ plugins {
id("org.jetbrains.dokka") version "0.10.1" id("org.jetbrains.dokka") version "0.10.1"
} }
val dataforgeVersion by extra("0.1.8-dev-2") val dataforgeVersion by extra("0.1.8-dev-4")
val bintrayRepo by extra("dataforge") val bintrayRepo by extra("dataforge")
val githubProject by extra("dataforge-core") val githubProject by extra("dataforge-core")

View File

@ -16,8 +16,12 @@ import kotlinx.serialization.*
@Serializable @Serializable
sealed class MetaItem<out M : Meta> { sealed class MetaItem<out M : Meta> {
abstract override fun equals(other: Any?): Boolean
abstract override fun hashCode(): Int
@Serializable @Serializable
data class ValueItem(val value: Value) : MetaItem<Nothing>() { class ValueItem(val value: Value) : MetaItem<Nothing>() {
override fun toString(): String = value.toString() override fun toString(): String = value.toString()
@Serializer(ValueItem::class) @Serializer(ValueItem::class)
@ -30,10 +34,18 @@ sealed class MetaItem<out M : Meta> {
ValueSerializer.serialize(encoder, value.value) ValueSerializer.serialize(encoder, value.value)
} }
} }
override fun equals(other: Any?): Boolean {
return this.value == (other as? ValueItem)?.value
}
override fun hashCode(): Int {
return value.hashCode()
}
} }
@Serializable @Serializable
data class NodeItem<M : Meta>(@Serializable(MetaSerializer::class) val node: M) : MetaItem<M>() { class NodeItem<M : Meta>(@Serializable(MetaSerializer::class) val node: M) : MetaItem<M>() {
//Fixing serializer for node could cause class cast problems, but it should not since Meta descendants are not serializeable //Fixing serializer for node could cause class cast problems, but it should not since Meta descendants are not serializeable
override fun toString(): String = node.toString() override fun toString(): String = node.toString()
@ -47,6 +59,10 @@ sealed class MetaItem<out M : Meta> {
MetaSerializer.serialize(encoder, value.node) MetaSerializer.serialize(encoder, value.node)
} }
} }
override fun equals(other: Any?): Boolean = node == (other as? NodeItem<*>)?.node
override fun hashCode(): Int = node.hashCode()
} }
companion object { companion object {

View File

@ -4,7 +4,7 @@ package hep.dataforge.meta.descriptors
* An object which provides its descriptor * An object which provides its descriptor
*/ */
interface Described { interface Described {
val descriptor: NodeDescriptor? val descriptor: ItemDescriptor?
companion object { companion object {
const val DESCRIPTOR_NODE = "@descriptor" const val DESCRIPTOR_NODE = "@descriptor"

View File

@ -9,13 +9,18 @@ import hep.dataforge.values.*
/** /**
* A converter of generic object to and from [MetaItem] * A converter of generic object to and from [MetaItem]
*/ */
interface MetaCaster<T : Any> { interface MetaConverter<T : Any> {
fun itemToObject(item: MetaItem<*>): T fun itemToObject(item: MetaItem<*>): T
fun objectToMetaItem(obj: T): MetaItem<*> fun objectToMetaItem(obj: T): MetaItem<*>
companion object { companion object {
val meta = object : MetaCaster<Meta> { val item = object :MetaConverter<MetaItem<*>>{
override fun itemToObject(item: MetaItem<*>): MetaItem<*> = item
override fun objectToMetaItem(obj: MetaItem<*>): MetaItem<*> = obj
}
val meta = object : MetaConverter<Meta> {
override fun itemToObject(item: MetaItem<*>): Meta = when (item) { override fun itemToObject(item: MetaItem<*>): Meta = when (item) {
is MetaItem.NodeItem -> item.node is MetaItem.NodeItem -> item.node
is MetaItem.ValueItem -> item.value.toMeta() is MetaItem.ValueItem -> item.value.toMeta()
@ -24,7 +29,7 @@ interface MetaCaster<T : Any> {
override fun objectToMetaItem(obj: Meta): MetaItem<*> = MetaItem.NodeItem(obj) override fun objectToMetaItem(obj: Meta): MetaItem<*> = MetaItem.NodeItem(obj)
} }
val value = object : MetaCaster<Value> { val value = object : MetaConverter<Value> {
override fun itemToObject(item: MetaItem<*>): Value = when (item) { override fun itemToObject(item: MetaItem<*>): Value = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value is MetaItem.ValueItem -> item.value
@ -33,7 +38,7 @@ interface MetaCaster<T : Any> {
override fun objectToMetaItem(obj: Value): MetaItem<*> = MetaItem.ValueItem(obj) override fun objectToMetaItem(obj: Value): MetaItem<*> = MetaItem.ValueItem(obj)
} }
val string = object : MetaCaster<String> { val string = object : MetaConverter<String> {
override fun itemToObject(item: MetaItem<*>): String = when (item) { override fun itemToObject(item: MetaItem<*>): String = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value is MetaItem.ValueItem -> item.value
@ -42,7 +47,7 @@ interface MetaCaster<T : Any> {
override fun objectToMetaItem(obj: String): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) override fun objectToMetaItem(obj: String): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
} }
val boolean = object : MetaCaster<Boolean> { val boolean = object : MetaConverter<Boolean> {
override fun itemToObject(item: MetaItem<*>): Boolean = when (item) { override fun itemToObject(item: MetaItem<*>): Boolean = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value is MetaItem.ValueItem -> item.value
@ -51,7 +56,7 @@ interface MetaCaster<T : Any> {
override fun objectToMetaItem(obj: Boolean): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) override fun objectToMetaItem(obj: Boolean): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
} }
val double = object : MetaCaster<Double> { val double = object : MetaConverter<Double> {
override fun itemToObject(item: MetaItem<*>): Double = when (item) { override fun itemToObject(item: MetaItem<*>): Double = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value is MetaItem.ValueItem -> item.value
@ -60,7 +65,7 @@ interface MetaCaster<T : Any> {
override fun objectToMetaItem(obj: Double): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) override fun objectToMetaItem(obj: Double): MetaItem<*> = MetaItem.ValueItem(obj.asValue())
} }
val int = object : MetaCaster<Int> { val int = object : MetaConverter<Int> {
override fun itemToObject(item: MetaItem<*>): Int = when (item) { override fun itemToObject(item: MetaItem<*>): Int = when (item) {
is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value")
is MetaItem.ValueItem -> item.value is MetaItem.ValueItem -> item.value
@ -71,5 +76,5 @@ interface MetaCaster<T : Any> {
} }
} }
fun <T : Any> MetaCaster<T>.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta)) fun <T : Any> MetaConverter<T>.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta))
fun <T : Any> MetaCaster<T>.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value)) fun <T : Any> MetaConverter<T>.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value))