From 1b118986f53a4d6b39b95ca48ef5522f53192888 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 2 Oct 2019 13:48:04 +0300 Subject: [PATCH] Value extensions to access primitives --- .../kotlin/hep/dataforge/meta/Meta.kt | 2 - .../kotlin/hep/dataforge/names/Name.kt | 4 +- .../kotlin/hep/dataforge/values/Value.kt | 38 +++++++------------ .../hep/dataforge/values/valueEstensions.kt | 37 ++++++++++++++++++ 4 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueEstensions.kt 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 45a27250..6b255a63 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/Meta.kt @@ -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() diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt index 0531a818..41680eb0 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/names/Name.kt @@ -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)) 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 cd68e040..2fce145f 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/Value.kt @@ -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 { } } - override val value: Any? get() = list + override val value: List 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 { } -/** - * 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.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) }) /** diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueEstensions.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueEstensions.kt new file mode 100644 index 00000000..41a5bda3 --- /dev/null +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueEstensions.kt @@ -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 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 } \ No newline at end of file