From df159ac5d3ff855eafb8922bbd6ca4184ccc5bc6 Mon Sep 17 00:00:00 2001 From: Matthew Pope Date: Wed, 13 Mar 2024 12:50:47 -0700 Subject: [PATCH] Ensure that all empty PersistentLists are the same instance. --- src/com/amazon/ionelement/api/Ion.kt | 77 ++++++++++++++----- .../ionelement/impl/BigIntIntElementImpl.kt | 3 +- .../amazon/ionelement/impl/BlobElementImpl.kt | 3 +- .../amazon/ionelement/impl/BoolElementImpl.kt | 3 +- .../amazon/ionelement/impl/ClobElementImpl.kt | 3 +- .../ionelement/impl/DecimalElementImpl.kt | 3 +- .../ionelement/impl/FloatElementImpl.kt | 3 +- .../amazon/ionelement/impl/ListElementImpl.kt | 3 +- .../ionelement/impl/LongIntElementImpl.kt | 3 +- .../amazon/ionelement/impl/NullElementImpl.kt | 3 +- .../amazon/ionelement/impl/SexpElementImpl.kt | 3 +- .../ionelement/impl/StringElementImpl.kt | 3 +- .../ionelement/impl/StructElementImpl.kt | 7 +- .../ionelement/impl/SymbolElementImpl.kt | 3 +- .../ionelement/impl/TimestampElementImpl.kt | 3 +- 15 files changed, 74 insertions(+), 49 deletions(-) diff --git a/src/com/amazon/ionelement/api/Ion.kt b/src/com/amazon/ionelement/api/Ion.kt index bd3d286..d9a0a90 100644 --- a/src/com/amazon/ionelement/api/Ion.kt +++ b/src/com/amazon/ionelement/api/Ion.kt @@ -35,6 +35,7 @@ import com.amazon.ionelement.impl.SymbolElementImpl import com.amazon.ionelement.impl.TimestampElementImpl import java.math.BigInteger import kotlinx.collections.immutable.PersistentList +import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap @@ -124,7 +125,7 @@ public fun ionString( metas: MetaContainer = emptyMetaContainer() ): StringElement = StringElementImpl( value = s, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) /** Creates a [StringElement] that represents an Ion `symbol`. */ @@ -141,7 +142,7 @@ public fun ionSymbol( metas: MetaContainer = emptyMetaContainer() ): SymbolElement = SymbolElementImpl( value = s, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -159,7 +160,7 @@ public fun ionTimestamp( metas: MetaContainer = emptyMetaContainer() ): TimestampElement = TimestampElementImpl( timestampValue = Timestamp.valueOf(s), - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -177,7 +178,7 @@ public fun ionTimestamp( metas: MetaContainer = emptyMetaContainer() ): TimestampElement = TimestampElementImpl( timestampValue = timestamp, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -196,7 +197,7 @@ public fun ionInt( ): IntElement = LongIntElementImpl( longValue = l, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -214,7 +215,7 @@ public fun ionInt( metas: MetaContainer = emptyMetaContainer() ): IntElement = BigIntIntElementImpl( bigIntegerValue = bigInt, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -232,7 +233,7 @@ public fun ionBool( metas: MetaContainer = emptyMetaContainer() ): BoolElement = BoolElementImpl( booleanValue = b, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -250,7 +251,7 @@ public fun ionFloat( metas: MetaContainer = emptyMetaContainer() ): FloatElement = FloatElementImpl( doubleValue = d, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -268,7 +269,7 @@ public fun ionDecimal( metas: MetaContainer = emptyMetaContainer() ): DecimalElement = DecimalElementImpl( decimalValue = bigDecimal, - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -290,7 +291,7 @@ public fun ionBlob( metas: MetaContainer = emptyMetaContainer() ): BlobElement = BlobElementImpl( bytes = bytes.clone(), - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -319,7 +320,7 @@ public fun ionClob( metas: MetaContainer = emptyMetaContainer() ): ClobElement = ClobElementImpl( bytes = bytes.clone(), - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) /** @@ -343,8 +344,8 @@ public fun ionListOf( metas: MetaContainer = emptyMetaContainer() ): ListElement = ListElementImpl( - values = iterable.map { it.asAnyElement() }.toPersistentList(), - annotations = annotations.toPersistentList(), + values = iterable.mapToEmptyOrPersistentList { it.asAnyElement() }, + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -392,8 +393,8 @@ public fun ionSexpOf( metas: MetaContainer = emptyMetaContainer() ): SexpElement = SexpElementImpl( - values = iterable.map { it.asAnyElement() }.toPersistentList(), - annotations = annotations.toPersistentList(), + values = iterable.mapToEmptyOrPersistentList { it.asAnyElement() }, + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -434,8 +435,8 @@ public fun ionStructOf( metas: MetaContainer = emptyMetaContainer() ): StructElement = StructElementImpl( - allFields = fields.toPersistentList(), - annotations = annotations.toPersistentList(), + allFields = fields.toEmptyOrPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas.toPersistentMap() ) @@ -454,7 +455,7 @@ public fun ionStructOf( ): StructElement = ionStructOf( fields = fields.asIterable(), - annotations = annotations.toPersistentList(), + annotations = annotations.toEmptyOrPersistentList(), metas = metas ) @@ -482,8 +483,46 @@ public fun buildStruct( return ionStructOf(fields, annotations, metas) } +/** + * Converts an `Iterable` to a `PersistentList`. + * + * ### Why is this needed? + * `kotlinx.collections.immutable` <= 0.3.7 has a bug that causes it to unnecessarily allocate empty `PersistentList`s + * instead of using a singleton instance. The fix is in + * [kotlinx.collections.immutable#176](https://github.com/Kotlin/kotlinx.collections.immutable/pull/176), + * but we cannot use it because the version of `kotlinx.collections.immutable` that will have (or has) the fix + * requires Kotlin stdlib >=1.9.2, and `ion-element-kotlin` supports consumers using Kotlin >= 1.3.0. + */ +internal fun Iterable.toEmptyOrPersistentList(): PersistentList { + val isEmpty = if (this is Collection<*>) { + this.isEmpty() + } else { + !this.iterator().hasNext() + } + return if (isEmpty) EMPTY_PERSISTENT_LIST else toPersistentList() +} + +/** + * Converts an `Iterable` to a `PersistentList`, transforming each element using [block]. + * + * ### Why is this needed? + * `kotlinx.collections.immutable` <= 0.3.7 has a bug that causes it to unnecessarily allocate empty `PersistentList`s + * instead of using a singleton instance. The fix is in + * [kotlinx.collections.immutable#176](https://github.com/Kotlin/kotlinx.collections.immutable/pull/176), + * but we cannot use it because the version of `kotlinx.collections.immutable` that will have (or has) the fix + * requires Kotlin stdlib >=1.9.2, and `ion-element-kotlin` supports consumers using Kotlin >= 1.3.0. + */ +internal inline fun Iterable.mapToEmptyOrPersistentList(block: (T) -> R): PersistentList { + val isEmpty = if (this is Collection<*>) { + this.isEmpty() + } else { + !this.iterator().hasNext() + } + return if (isEmpty) EMPTY_PERSISTENT_LIST else mapTo(persistentListOf().builder(), block).build() +} + // Memoized empty PersistentList for the memoized container types and null values -private val EMPTY_PERSISTENT_LIST: PersistentList = emptyList().toPersistentList() +internal val EMPTY_PERSISTENT_LIST: PersistentList = persistentListOf() // Memoized empty instances of our container types. private val EMPTY_LIST = ListElementImpl(EMPTY_PERSISTENT_LIST, EMPTY_PERSISTENT_LIST, EMPTY_METAS) diff --git a/src/com/amazon/ionelement/impl/BigIntIntElementImpl.kt b/src/com/amazon/ionelement/impl/BigIntIntElementImpl.kt index 2004d52..c6ba732 100644 --- a/src/com/amazon/ionelement/impl/BigIntIntElementImpl.kt +++ b/src/com/amazon/ionelement/impl/BigIntIntElementImpl.kt @@ -21,7 +21,6 @@ import com.amazon.ionelement.api.PersistentMetaContainer import com.amazon.ionelement.api.constraintError import java.math.BigInteger import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class BigIntIntElementImpl( @@ -42,7 +41,7 @@ internal class BigIntIntElementImpl( } override fun copy(annotations: List, metas: MetaContainer): BigIntIntElementImpl = - BigIntIntElementImpl(bigIntegerValue, annotations.toPersistentList(), metas.toPersistentMap()) + BigIntIntElementImpl(bigIntegerValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): BigIntIntElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): BigIntIntElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/BlobElementImpl.kt b/src/com/amazon/ionelement/impl/BlobElementImpl.kt index 224a19e..1b6af61 100644 --- a/src/com/amazon/ionelement/impl/BlobElementImpl.kt +++ b/src/com/amazon/ionelement/impl/BlobElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class BlobElementImpl( @@ -33,7 +32,7 @@ internal class BlobElementImpl( override fun writeContentTo(writer: IonWriter) = writer.writeBlob(bytes) override fun copy(annotations: List, metas: MetaContainer): BlobElementImpl = - BlobElementImpl(bytes, annotations.toPersistentList(), metas.toPersistentMap()) + BlobElementImpl(bytes, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): BlobElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): BlobElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/BoolElementImpl.kt b/src/com/amazon/ionelement/impl/BoolElementImpl.kt index afd688a..52e3af1 100644 --- a/src/com/amazon/ionelement/impl/BoolElementImpl.kt +++ b/src/com/amazon/ionelement/impl/BoolElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class BoolElementImpl( @@ -30,7 +29,7 @@ internal class BoolElementImpl( override val type: ElementType get() = ElementType.BOOL override fun copy(annotations: List, metas: MetaContainer): BoolElementImpl = - BoolElementImpl(booleanValue, annotations.toPersistentList(), metas.toPersistentMap()) + BoolElementImpl(booleanValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): BoolElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): BoolElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/ClobElementImpl.kt b/src/com/amazon/ionelement/impl/ClobElementImpl.kt index e65898c..768d883 100644 --- a/src/com/amazon/ionelement/impl/ClobElementImpl.kt +++ b/src/com/amazon/ionelement/impl/ClobElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class ClobElementImpl( @@ -32,7 +31,7 @@ internal class ClobElementImpl( override fun writeContentTo(writer: IonWriter) = writer.writeClob(bytes) override fun copy(annotations: List, metas: MetaContainer): ClobElementImpl = - ClobElementImpl(bytes, annotations.toPersistentList(), metas.toPersistentMap()) + ClobElementImpl(bytes, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): ClobElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): ClobElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/DecimalElementImpl.kt b/src/com/amazon/ionelement/impl/DecimalElementImpl.kt index c8640a2..bd7e3b0 100644 --- a/src/com/amazon/ionelement/impl/DecimalElementImpl.kt +++ b/src/com/amazon/ionelement/impl/DecimalElementImpl.kt @@ -20,7 +20,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class DecimalElementImpl( @@ -31,7 +30,7 @@ internal class DecimalElementImpl( override val type get() = ElementType.DECIMAL override fun copy(annotations: List, metas: MetaContainer): DecimalElementImpl = - DecimalElementImpl(decimalValue, annotations.toPersistentList(), metas.toPersistentMap()) + DecimalElementImpl(decimalValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): DecimalElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): DecimalElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/FloatElementImpl.kt b/src/com/amazon/ionelement/impl/FloatElementImpl.kt index af03711..f880d7f 100644 --- a/src/com/amazon/ionelement/impl/FloatElementImpl.kt +++ b/src/com/amazon/ionelement/impl/FloatElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class FloatElementImpl( @@ -30,7 +29,7 @@ internal class FloatElementImpl( override val type: ElementType get() = ElementType.FLOAT override fun copy(annotations: List, metas: MetaContainer): FloatElementImpl = - FloatElementImpl(doubleValue, annotations.toPersistentList(), metas.toPersistentMap()) + FloatElementImpl(doubleValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): FloatElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): FloatElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/ListElementImpl.kt b/src/com/amazon/ionelement/impl/ListElementImpl.kt index 259fabf..c066f4f 100644 --- a/src/com/amazon/ionelement/impl/ListElementImpl.kt +++ b/src/com/amazon/ionelement/impl/ListElementImpl.kt @@ -18,7 +18,6 @@ package com.amazon.ionelement.impl import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class ListElementImpl( @@ -31,7 +30,7 @@ internal class ListElementImpl( override val listValues: List get() = values override fun copy(annotations: List, metas: MetaContainer): ListElementImpl = - ListElementImpl(values, annotations.toPersistentList(), metas.toPersistentMap()) + ListElementImpl(values, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): ListElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): ListElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/LongIntElementImpl.kt b/src/com/amazon/ionelement/impl/LongIntElementImpl.kt index b4599f4..07dbc02 100644 --- a/src/com/amazon/ionelement/impl/LongIntElementImpl.kt +++ b/src/com/amazon/ionelement/impl/LongIntElementImpl.kt @@ -20,7 +20,6 @@ import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import java.math.BigInteger import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class LongIntElementImpl( @@ -34,7 +33,7 @@ internal class LongIntElementImpl( override val bigIntegerValue: BigInteger get() = BigInteger.valueOf(longValue) override fun copy(annotations: List, metas: MetaContainer): LongIntElementImpl = - LongIntElementImpl(longValue, annotations.toPersistentList(), metas.toPersistentMap()) + LongIntElementImpl(longValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): LongIntElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): LongIntElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/NullElementImpl.kt b/src/com/amazon/ionelement/impl/NullElementImpl.kt index d2e717d..7e80dfc 100644 --- a/src/com/amazon/ionelement/impl/NullElementImpl.kt +++ b/src/com/amazon/ionelement/impl/NullElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class NullElementImpl( @@ -31,7 +30,7 @@ internal class NullElementImpl( override val isNull: Boolean get() = true override fun copy(annotations: List, metas: MetaContainer): AnyElement = - NullElementImpl(type, annotations.toPersistentList(), metas.toPersistentMap()) + NullElementImpl(type, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): AnyElement = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): AnyElement = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/SexpElementImpl.kt b/src/com/amazon/ionelement/impl/SexpElementImpl.kt index ee3a8e0..c0c04ca 100644 --- a/src/com/amazon/ionelement/impl/SexpElementImpl.kt +++ b/src/com/amazon/ionelement/impl/SexpElementImpl.kt @@ -18,7 +18,6 @@ package com.amazon.ionelement.impl import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class SexpElementImpl( @@ -31,7 +30,7 @@ internal class SexpElementImpl( override val sexpValues: List get() = seqValues override fun copy(annotations: List, metas: MetaContainer): SexpElementImpl = - SexpElementImpl(values, annotations.toPersistentList(), metas.toPersistentMap()) + SexpElementImpl(values, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): SexpElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): SexpElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/StringElementImpl.kt b/src/com/amazon/ionelement/impl/StringElementImpl.kt index 07e0b3f..8ab7715 100644 --- a/src/com/amazon/ionelement/impl/StringElementImpl.kt +++ b/src/com/amazon/ionelement/impl/StringElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class StringElementImpl( @@ -32,7 +31,7 @@ internal class StringElementImpl( override val stringValue: String get() = textValue override fun copy(annotations: List, metas: MetaContainer): StringElementImpl = - StringElementImpl(textValue, annotations.toPersistentList(), metas.toPersistentMap()) + StringElementImpl(textValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): StringElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): StringElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/StructElementImpl.kt b/src/com/amazon/ionelement/impl/StructElementImpl.kt index 9387922..254f0aa 100644 --- a/src/com/amazon/ionelement/impl/StructElementImpl.kt +++ b/src/com/amazon/ionelement/impl/StructElementImpl.kt @@ -24,7 +24,6 @@ import java.util.function.Consumer import kotlinx.collections.immutable.PersistentCollection import kotlinx.collections.immutable.PersistentList import kotlinx.collections.immutable.PersistentMap -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class StructElementImpl( @@ -42,7 +41,7 @@ internal class StructElementImpl( override val values: Collection get() { if (valuesBackingField == null) { - valuesBackingField = fields.map { it.value }.toPersistentList() + valuesBackingField = fields.mapToEmptyOrPersistentList { it.value } } return valuesBackingField!! } @@ -61,7 +60,7 @@ internal class StructElementImpl( fieldsByNameBackingField = fields .groupBy { it.name } - .map { structFieldGroup -> structFieldGroup.key to structFieldGroup.value.map { it.value }.toPersistentList() } + .map { structFieldGroup -> structFieldGroup.key to structFieldGroup.value.mapToEmptyOrPersistentList { it.value } } .toMap().toPersistentMap() } return fieldsByNameBackingField!! @@ -99,7 +98,7 @@ internal class StructElementImpl( override fun containsField(fieldName: String): Boolean = fieldsByName.containsKey(fieldName) override fun copy(annotations: List, metas: MetaContainer): StructElementImpl = - StructElementImpl(allFields, annotations.toPersistentList(), metas.toPersistentMap()) + StructElementImpl(allFields, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): StructElementImpl = _withAnnotations(*additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/SymbolElementImpl.kt b/src/com/amazon/ionelement/impl/SymbolElementImpl.kt index 57edc0d..c2f8718 100644 --- a/src/com/amazon/ionelement/impl/SymbolElementImpl.kt +++ b/src/com/amazon/ionelement/impl/SymbolElementImpl.kt @@ -19,7 +19,6 @@ import com.amazon.ion.IonWriter import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class SymbolElementImpl( @@ -32,7 +31,7 @@ internal class SymbolElementImpl( override val symbolValue: String get() = textValue override fun copy(annotations: List, metas: MetaContainer): SymbolElementImpl = - SymbolElementImpl(textValue, annotations.toPersistentList(), metas.toPersistentMap()) + SymbolElementImpl(textValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): SymbolElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): SymbolElementImpl = _withAnnotations(additionalAnnotations) diff --git a/src/com/amazon/ionelement/impl/TimestampElementImpl.kt b/src/com/amazon/ionelement/impl/TimestampElementImpl.kt index bfc7fb3..c1e84b2 100644 --- a/src/com/amazon/ionelement/impl/TimestampElementImpl.kt +++ b/src/com/amazon/ionelement/impl/TimestampElementImpl.kt @@ -20,7 +20,6 @@ import com.amazon.ion.Timestamp import com.amazon.ionelement.api.* import com.amazon.ionelement.api.PersistentMetaContainer import kotlinx.collections.immutable.PersistentList -import kotlinx.collections.immutable.toPersistentList import kotlinx.collections.immutable.toPersistentMap internal class TimestampElementImpl( @@ -31,7 +30,7 @@ internal class TimestampElementImpl( override val type: ElementType get() = ElementType.TIMESTAMP override fun copy(annotations: List, metas: MetaContainer): TimestampElementImpl = - TimestampElementImpl(timestampValue, annotations.toPersistentList(), metas.toPersistentMap()) + TimestampElementImpl(timestampValue, annotations.toEmptyOrPersistentList(), metas.toPersistentMap()) override fun withAnnotations(vararg additionalAnnotations: String): TimestampElementImpl = _withAnnotations(*additionalAnnotations) override fun withAnnotations(additionalAnnotations: Iterable): TimestampElementImpl = _withAnnotations(additionalAnnotations)