From 72954a837004e8b56708a1d3d390834fb0403e1d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 7 Aug 2019 20:41:27 +0300 Subject: [PATCH] Meta equality for all --- .../kotlin/hep/dataforge/io/JsonMetaFormat.kt | 3 +- .../kotlin/hep/dataforge/io/MetaFormatTest.kt | 2 +- .../kotlin/hep/dataforge/meta/Laminate.kt | 2 +- .../kotlin/hep/dataforge/meta/Meta.kt | 34 ++++++++++++------- .../kotlin/hep/dataforge/values/Value.kt | 7 ++-- .../hep/dataforge/values/exoticValues.kt | 9 +++-- .../kotlin/hep/dataforge/meta/DynamicMeta.kt | 2 +- 7 files changed, 36 insertions(+), 23 deletions(-) diff --git a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt index de2cf925..5dabf499 100644 --- a/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt +++ b/dataforge-io/src/commonMain/kotlin/hep/dataforge/io/JsonMetaFormat.kt @@ -4,6 +4,7 @@ import hep.dataforge.descriptors.ItemDescriptor import hep.dataforge.descriptors.NodeDescriptor import hep.dataforge.descriptors.ValueDescriptor import hep.dataforge.meta.Meta +import hep.dataforge.meta.MetaBase import hep.dataforge.meta.MetaItem import hep.dataforge.names.NameToken import hep.dataforge.names.toName @@ -79,7 +80,7 @@ fun Meta.toJson(descriptor: NodeDescriptor? = null): JsonObject { fun JsonObject.toMeta(descriptor: NodeDescriptor? = null) = JsonMeta(this, descriptor) -class JsonMeta(val json: JsonObject, val descriptor: NodeDescriptor? = null) : Meta { +class JsonMeta(val json: JsonObject, val descriptor: NodeDescriptor? = null) : MetaBase() { private fun JsonPrimitive.toValue(descriptor: ValueDescriptor?): Value { return when (this) { diff --git a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt index ab4ab677..8348f7b0 100644 --- a/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt +++ b/dataforge-io/src/commonTest/kotlin/hep/dataforge/io/MetaFormatTest.kt @@ -42,7 +42,7 @@ class MetaFormatTest { if (meta[it] != result[it]) error("${meta[it]} != ${result[it]}") } - assertEquals(meta, result) + assertEquals(meta, result) } } \ No newline at end of file diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt index 6f7c41c8..b76fd46f 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Laminate.kt @@ -7,7 +7,7 @@ import hep.dataforge.names.NameToken * * */ -class Laminate(layers: List) : Meta { +class Laminate(layers: List) : MetaBase() { val layers: List = layers.flatMap { if (it is Laminate) { diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt index e89eb950..1f918da3 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -15,10 +15,11 @@ import hep.dataforge.values.boolean * * a [NodeItem] (node) */ sealed class MetaItem { - data class ValueItem(val value: Value) : MetaItem(){ + data class ValueItem(val value: Value) : MetaItem() { override fun toString(): String = value.toString() } - data class NodeItem(val node: M) : MetaItem(){ + + data class NodeItem(val node: M) : MetaItem() { override fun toString(): String = node.toString() } } @@ -46,6 +47,12 @@ interface Meta : MetaRepr { override fun toMeta(): Meta = this + override fun equals(other: Any?): Boolean + + override fun hashCode(): Int + + override fun toString(): String + companion object { const val TYPE = "meta" /** @@ -174,23 +181,26 @@ operator fun > MetaNode?.get(key: NameToken): MetaItem? = } /** - * Equals and hash code implementation for meta node + * Equals, hashcode and to string for any meta */ -abstract class AbstractMetaNode> : MetaNode { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is Meta) return false +abstract class MetaBase: Meta{ - return this.items == other.items//this.items.keys == other.items.keys && items.keys.all { this[it] == other[it] } + override fun equals(other: Any?): Boolean = if(other is Meta) { + items == other.items + } else { + false } - override fun hashCode(): Int { - return items.hashCode() - } + override fun hashCode(): Int = items.hashCode() override fun toString(): String = items.toString() } +/** + * Equals and hash code implementation for meta node + */ +abstract class AbstractMetaNode> : MetaNode, MetaBase() + /** * The meta implementation which is guaranteed to be immutable. * @@ -210,7 +220,7 @@ fun MetaItem<*>.seal(): MetaItem = when (this) { is NodeItem -> NodeItem(node.seal()) } -object EmptyMeta : Meta { +object EmptyMeta : MetaBase() { override val items: Map> = emptyMap() } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt index 7b42f639..5da65a10 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -159,7 +159,7 @@ class StringValue(override val string: String) : Value { override fun hashCode(): Int = string.hashCode() - override fun toString(): String = value.toString() + override fun toString(): String = "\"${value.toString()}\"" } class EnumValue>(override val value: E) : Value { @@ -188,11 +188,14 @@ class ListValue(override val list: List) : Value { override val number: Number get() = list.first().number override val string: String get() = list.first().string - override fun toString(): String = list.joinToString (prefix = "[ ", postfix = " ]") + override fun toString(): String = list.joinToString (prefix = "[", postfix = "]") override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Value) return false + if( other is DoubleArrayValue){ + + } return list == other.list } diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt index 8ebc1c6b..f6122dbb 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/exoticValues.kt @@ -31,10 +31,9 @@ class DoubleArrayValue(override val value: DoubleArray) : Value { if (this === other) return true if (other !is Value) return false - return if (other is DoubleArrayValue) { - value.contentEquals(other.value) - } else { - list == other.list + return when (other) { + is DoubleArrayValue -> value.contentEquals(other.value) + else -> list == other.list } } @@ -42,7 +41,7 @@ class DoubleArrayValue(override val value: DoubleArray) : Value { return value.contentHashCode() } - override fun toString(): String = value.toString() + override fun toString(): String = list.joinToString (prefix = "[", postfix = "]") } fun DoubleArray.asValue(): DoubleArrayValue = DoubleArrayValue(this) diff --git a/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/DynamicMeta.kt b/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/DynamicMeta.kt index 57375125..694e2441 100644 --- a/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/DynamicMeta.kt +++ b/dataforge-meta/src/jsMain/kotlin/hep/dataforge/meta/DynamicMeta.kt @@ -29,7 +29,7 @@ fun Meta.toDynamic(): dynamic { return res } -class DynamicMeta(val obj: dynamic) : Meta { +class DynamicMeta(val obj: dynamic) : MetaBase() { private fun keys() = js("Object.keys(this.obj)") as Array private fun isArray(@Suppress("UNUSED_PARAMETER") obj: dynamic): Boolean =