Meta equality for all

This commit is contained in:
Alexander Nozik 2019-08-07 20:41:27 +03:00
parent 1f0a317cd8
commit 72954a8370
7 changed files with 36 additions and 23 deletions

View File

@ -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) {

View File

@ -42,7 +42,7 @@ class MetaFormatTest {
if (meta[it] != result[it]) error("${meta[it]} != ${result[it]}")
}
assertEquals(meta, result)
assertEquals<Meta>(meta, result)
}
}

View File

@ -7,7 +7,7 @@ import hep.dataforge.names.NameToken
*
*
*/
class Laminate(layers: List<Meta>) : Meta {
class Laminate(layers: List<Meta>) : MetaBase() {
val layers: List<Meta> = layers.flatMap {
if (it is Laminate) {

View File

@ -15,10 +15,11 @@ import hep.dataforge.values.boolean
* * a [NodeItem] (node)
*/
sealed class MetaItem<out M : Meta> {
data class ValueItem(val value: Value) : MetaItem<Nothing>(){
data class ValueItem(val value: Value) : MetaItem<Nothing>() {
override fun toString(): String = value.toString()
}
data class NodeItem<M : Meta>(val node: M) : MetaItem<M>(){
data class NodeItem<M : Meta>(val node: M) : MetaItem<M>() {
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 <M : MetaNode<M>> MetaNode<M>?.get(key: NameToken): MetaItem<M>? =
}
/**
* Equals and hash code implementation for meta node
* Equals, hashcode and to string for any meta
*/
abstract class AbstractMetaNode<M : MetaNode<M>> : MetaNode<M> {
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<M : MetaNode<M>> : MetaNode<M>, MetaBase()
/**
* The meta implementation which is guaranteed to be immutable.
*
@ -210,7 +220,7 @@ fun MetaItem<*>.seal(): MetaItem<SealedMeta> = when (this) {
is NodeItem -> NodeItem(node.seal())
}
object EmptyMeta : Meta {
object EmptyMeta : MetaBase() {
override val items: Map<NameToken, MetaItem<*>> = emptyMap()
}

View File

@ -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<E : Enum<*>>(override val value: E) : Value {
@ -188,11 +188,14 @@ class ListValue(override val list: List<Value>) : 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
}

View File

@ -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)

View File

@ -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<String>
private fun isArray(@Suppress("UNUSED_PARAMETER") obj: dynamic): Boolean =