Value extensions to access primitives

This commit is contained in:
Alexander Nozik 2019-10-02 13:48:04 +03:00
parent 337401f6e0
commit 1b118986f5
4 changed files with 54 additions and 27 deletions

View File

@ -263,6 +263,4 @@ interface Metoid {
val meta: Meta
}
fun Value.toMeta() = buildMeta { Meta.VALUE_KEY to this }
fun Meta.isEmpty() = this === EmptyMeta || this.items.isEmpty()

View File

@ -139,7 +139,7 @@ fun String.toName(): Name {
* Convert the [String] to a [Name] by simply wrapping it in a single name token without parsing.
* The input string could contain dots and braces, but they are just escaped, not parsed.
*/
fun String.asName(): Name = if(isBlank()) EmptyName else NameToken(this).asName()
fun String.asName(): Name = if (isBlank()) EmptyName else NameToken(this).asName()
operator fun NameToken.plus(other: Name): Name = Name(listOf(this) + other.tokens)
@ -147,6 +147,8 @@ operator fun Name.plus(other: Name): Name = Name(this.tokens + other.tokens)
operator fun Name.plus(other: String): Name = this + other.toName()
operator fun Name.plus(other: NameToken): Name = Name(tokens + other)
fun Name.appendLeft(other: String): Name = NameToken(other) + this
fun NameToken.asName() = Name(listOf(this))

View File

@ -45,6 +45,8 @@ interface Value {
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
companion object {
const val TYPE = "value"
@ -86,14 +88,9 @@ object Null : Value {
override fun toString(): String = value.toString()
override fun equals(other: Any?): Boolean = other === Null
override fun hashCode(): Int = 0
}
/**
* Check if value is null
*/
fun Value.isNull(): Boolean = this == Null
/**
* Singleton true value
*/
@ -106,7 +103,7 @@ object True : Value {
override fun toString(): String = value.toString()
override fun equals(other: Any?): Boolean = other === True
override fun hashCode(): Int = 1
}
/**
@ -121,10 +118,9 @@ object False : Value {
override fun toString(): String = True.value.toString()
override fun equals(other: Any?): Boolean = other === False
override fun hashCode(): Int = -1
}
val Value.boolean get() = this == True || this.list.firstOrNull() == True || (type == ValueType.STRING && string.toBoolean())
class NumberValue(override val number: Number) : Value {
override val value: Any? get() = number
override val type: ValueType get() = ValueType.NUMBER
@ -183,18 +179,18 @@ class ListValue(override val list: List<Value>) : Value {
}
}
override val value: Any? get() = list
override val value: List<Value> get() = list
override val type: ValueType get() = list.first().type
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){
if (other is DoubleArrayValue) {
return DoubleArray(list.size) { list[it].number.toDouble() }.contentEquals(other.value)
}
return list == other.list
}
@ -206,12 +202,6 @@ class ListValue(override val list: List<Value>) : Value {
}
/**
* Check if value is list
*/
fun Value.isList(): Boolean = this.list.size > 1
fun Number.asValue(): Value = NumberValue(this)
fun Boolean.asValue(): Value = if (this) True else False
@ -220,15 +210,15 @@ fun String.asValue(): Value = StringValue(this)
fun Iterable<Value>.asValue(): Value = ListValue(this.toList())
fun IntArray.asValue(): Value = ListValue(map{NumberValue(it)})
fun IntArray.asValue(): Value = ListValue(map { NumberValue(it) })
fun LongArray.asValue(): Value = ListValue(map{NumberValue(it)})
fun LongArray.asValue(): Value = ListValue(map { NumberValue(it) })
fun ShortArray.asValue(): Value = ListValue(map{NumberValue(it)})
fun ShortArray.asValue(): Value = ListValue(map { NumberValue(it) })
fun FloatArray.asValue(): Value = ListValue(map{NumberValue(it)})
fun FloatArray.asValue(): Value = ListValue(map { NumberValue(it) })
fun ByteArray.asValue(): Value = ListValue(map{NumberValue(it)})
fun ByteArray.asValue(): Value = ListValue(map { NumberValue(it) })
/**

View File

@ -0,0 +1,37 @@
package hep.dataforge.values
import hep.dataforge.meta.Meta
import hep.dataforge.meta.buildMeta
/**
* Check if value is null
*/
fun Value.isNull(): Boolean = this == Null
/**
* Check if value is list
*/
fun Value.isList(): Boolean = this.list.size > 1
val Value.boolean
get() = this == True
|| this.list.firstOrNull() == True
|| (type == ValueType.STRING && string.toBoolean())
val Value.int get() = number.toInt()
val Value.double get() = number.toDouble()
val Value.float get() = number.toFloat()
val Value.long get() = number.toLong()
val Value.stringList: List<String> get() = list.map { it.string }
val Value.doubleArray: DoubleArray
get() = if (this is DoubleArrayValue) {
value
} else {
DoubleArray(list.size) { list[it].double }
}
fun Value.toMeta() = buildMeta { Meta.VALUE_KEY to this }