Array converters for values

This commit is contained in:
Alexander Nozik 2019-04-30 21:15:21 +03:00
parent da2c59fdfe
commit 3cf62bb6b2

View File

@ -55,14 +55,14 @@ interface Value {
is Value -> value is Value -> value
true -> True true -> True
false -> False false -> False
is Number -> NumberValue(value) is Number -> value.asValue()
is Iterable<*> -> ListValue(value.map { of(it) }) is Iterable<*> -> ListValue(value.map { of(it) })
is DoubleArray -> ListValue(value.map { NumberValue(it) }) is DoubleArray -> value.asValue()
is IntArray -> ListValue(value.map { NumberValue(it) }) is IntArray -> value.asValue()
is FloatArray -> ListValue(value.map { NumberValue(it) }) is FloatArray -> value.asValue()
is ShortArray -> ListValue(value.map { NumberValue(it) }) is ShortArray -> value.asValue()
is LongArray -> ListValue(value.map { NumberValue(it) }) is LongArray -> value.asValue()
is ByteArray -> ListValue(value.map { NumberValue(it) }) is ByteArray -> value.asValue()
is Array<*> -> ListValue(value.map { of(it) }) is Array<*> -> ListValue(value.map { of(it) })
is Enum<*> -> EnumValue(value) is Enum<*> -> EnumValue(value)
is CharSequence -> StringValue(value.toString()) is CharSequence -> StringValue(value.toString())
@ -182,11 +182,7 @@ class ListValue(override val list: List<Value>) : Value {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other !is Value) return false if (other !is Value) return false
val otherList = other.list return list == other.list
if (list.size != otherList.size) return false
return (0 until list.size).all { list[it] == otherList[it] }
} }
override fun hashCode(): Int { override fun hashCode(): Int {
@ -208,7 +204,20 @@ fun Boolean.asValue(): Value = if (this) True else False
fun String.asValue(): Value = StringValue(this) fun String.asValue(): Value = StringValue(this)
fun Collection<Value>.asValue(): Value = ListValue(this.toList()) fun Iterable<Value>.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)})
/** /**