From 3cf62bb6b2f25445da7aef911cbdfa521152cfe5 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 30 Apr 2019 21:15:21 +0300 Subject: [PATCH] Array converters for values --- .../kotlin/hep/dataforge/values/Value.kt | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) 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 89d1519c..20f86779 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -55,14 +55,14 @@ interface Value { is Value -> value true -> True false -> False - is Number -> NumberValue(value) + is Number -> value.asValue() is Iterable<*> -> ListValue(value.map { of(it) }) - is DoubleArray -> ListValue(value.map { NumberValue(it) }) - is IntArray -> ListValue(value.map { NumberValue(it) }) - is FloatArray -> ListValue(value.map { NumberValue(it) }) - is ShortArray -> ListValue(value.map { NumberValue(it) }) - is LongArray -> ListValue(value.map { NumberValue(it) }) - is ByteArray -> ListValue(value.map { NumberValue(it) }) + is DoubleArray -> value.asValue() + is IntArray -> value.asValue() + is FloatArray -> value.asValue() + is ShortArray -> value.asValue() + is LongArray -> value.asValue() + is ByteArray -> value.asValue() is Array<*> -> ListValue(value.map { of(it) }) is Enum<*> -> EnumValue(value) is CharSequence -> StringValue(value.toString()) @@ -182,11 +182,7 @@ class ListValue(override val list: List) : Value { override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Value) return false - val otherList = other.list - if (list.size != otherList.size) return false - - - return (0 until list.size).all { list[it] == otherList[it] } + return list == other.list } override fun hashCode(): Int { @@ -208,7 +204,20 @@ fun Boolean.asValue(): Value = if (this) True else False fun String.asValue(): Value = StringValue(this) -fun Collection.asValue(): Value = ListValue(this.toList()) +fun Iterable.asValue(): Value = ListValue(this.toList()) + +//TODO maybe optimized storage performance +fun DoubleArray.asValue(): Value = ListValue(map{NumberValue(it)}) + +fun IntArray.asValue(): Value = ListValue(map{NumberValue(it)}) + +fun LongArray.asValue(): Value = ListValue(map{NumberValue(it)}) + +fun ShortArray.asValue(): Value = ListValue(map{NumberValue(it)}) + +fun FloatArray.asValue(): Value = ListValue(map{NumberValue(it)}) + +fun ByteArray.asValue(): Value = ListValue(map{NumberValue(it)}) /**