@@ -18,7 +18,6 @@ package com.amazon.ionelement.api
1818
1919import com.amazon.ion.Decimal
2020import com.amazon.ion.Timestamp
21- import com.amazon.ionelement.impl.*
2221import com.amazon.ionelement.impl.BigIntIntElementImpl
2322import com.amazon.ionelement.impl.BlobElementImpl
2423import com.amazon.ionelement.impl.BoolElementImpl
@@ -34,12 +33,9 @@ import com.amazon.ionelement.impl.StructElementImpl
3433import com.amazon.ionelement.impl.StructFieldImpl
3534import com.amazon.ionelement.impl.SymbolElementImpl
3635import com.amazon.ionelement.impl.TimestampElementImpl
36+ import com.amazon.ionelement.impl.collections.*
3737import java.math.BigInteger
3838import java.util.function.Consumer
39- import kotlinx.collections.immutable.PersistentList
40- import kotlinx.collections.immutable.persistentListOf
41- import kotlinx.collections.immutable.toPersistentList
42- import kotlinx.collections.immutable.toPersistentMap
4339
4440internal typealias Annotations = List <String >
4541
@@ -127,8 +123,8 @@ public fun ionString(
127123 metas : MetaContainer = emptyMetaContainer()
128124): StringElement = StringElementImpl (
129125 value = s,
130- annotations = annotations.toEmptyOrPersistentList (),
131- metas = metas.toPersistentMap ()
126+ annotations = annotations.toImmutableList (),
127+ metas = metas.toImmutableMap ()
132128)
133129/* * Creates a [StringElement] that represents an Ion `symbol`. */
134130public fun ionString (
@@ -144,8 +140,8 @@ public fun ionSymbol(
144140 metas : MetaContainer = emptyMetaContainer()
145141): SymbolElement = SymbolElementImpl (
146142 value = s,
147- annotations = annotations.toEmptyOrPersistentList (),
148- metas = metas.toPersistentMap ()
143+ annotations = annotations.toImmutableList (),
144+ metas = metas.toImmutableMap ()
149145)
150146
151147/* * Creates a [SymbolElement] that represents an Ion `symbol`. */
@@ -162,8 +158,8 @@ public fun ionTimestamp(
162158 metas : MetaContainer = emptyMetaContainer()
163159): TimestampElement = TimestampElementImpl (
164160 timestampValue = Timestamp .valueOf(s),
165- annotations = annotations.toEmptyOrPersistentList (),
166- metas = metas.toPersistentMap ()
161+ annotations = annotations.toImmutableList (),
162+ metas = metas.toImmutableMap ()
167163)
168164
169165/* * Creates a [TimestampElement] that represents an Ion `timestamp`. */
@@ -180,8 +176,8 @@ public fun ionTimestamp(
180176 metas : MetaContainer = emptyMetaContainer()
181177): TimestampElement = TimestampElementImpl (
182178 timestampValue = timestamp,
183- annotations = annotations.toEmptyOrPersistentList (),
184- metas = metas.toPersistentMap ()
179+ annotations = annotations.toImmutableList (),
180+ metas = metas.toImmutableMap ()
185181)
186182
187183/* * Creates a [TimestampElement] that represents an Ion `timestamp`. */
@@ -199,8 +195,8 @@ public fun ionInt(
199195): IntElement =
200196 LongIntElementImpl (
201197 longValue = l,
202- annotations = annotations.toEmptyOrPersistentList (),
203- metas = metas.toPersistentMap ()
198+ annotations = annotations.toImmutableList (),
199+ metas = metas.toImmutableMap ()
204200 )
205201
206202/* * Creates an [IntElement] that represents an Ion `int`. */
@@ -217,8 +213,8 @@ public fun ionInt(
217213 metas : MetaContainer = emptyMetaContainer()
218214): IntElement = BigIntIntElementImpl (
219215 bigIntegerValue = bigInt,
220- annotations = annotations.toEmptyOrPersistentList (),
221- metas = metas.toPersistentMap ()
216+ annotations = annotations.toImmutableList (),
217+ metas = metas.toImmutableMap ()
222218)
223219
224220/* * Creates an [IntElement] that represents an Ion `BitInteger`. */
@@ -235,8 +231,8 @@ public fun ionBool(
235231 metas : MetaContainer = emptyMetaContainer()
236232): BoolElement = BoolElementImpl (
237233 booleanValue = b,
238- annotations = annotations.toEmptyOrPersistentList (),
239- metas = metas.toPersistentMap ()
234+ annotations = annotations.toImmutableList (),
235+ metas = metas.toImmutableMap ()
240236)
241237
242238/* * Creates a [BoolElement] that represents an Ion `bool`. */
@@ -253,8 +249,8 @@ public fun ionFloat(
253249 metas : MetaContainer = emptyMetaContainer()
254250): FloatElement = FloatElementImpl (
255251 doubleValue = d,
256- annotations = annotations.toEmptyOrPersistentList (),
257- metas = metas.toPersistentMap ()
252+ annotations = annotations.toImmutableList (),
253+ metas = metas.toImmutableMap ()
258254)
259255
260256/* * Creates a [FloatElement] that represents an Ion `float`. */
@@ -271,8 +267,8 @@ public fun ionDecimal(
271267 metas : MetaContainer = emptyMetaContainer()
272268): DecimalElement = DecimalElementImpl (
273269 decimalValue = bigDecimal,
274- annotations = annotations.toEmptyOrPersistentList (),
275- metas = metas.toPersistentMap ()
270+ annotations = annotations.toImmutableList (),
271+ metas = metas.toImmutableMap ()
276272)
277273
278274/* * Creates a [DecimalElement] that represents an Ion `decimall`. */
@@ -293,8 +289,8 @@ public fun ionBlob(
293289 metas : MetaContainer = emptyMetaContainer()
294290): BlobElement = BlobElementImpl (
295291 bytes = bytes.clone(),
296- annotations = annotations.toEmptyOrPersistentList (),
297- metas = metas.toPersistentMap ()
292+ annotations = annotations.toImmutableList (),
293+ metas = metas.toImmutableMap ()
298294)
299295
300296/* *
@@ -322,8 +318,8 @@ public fun ionClob(
322318 metas : MetaContainer = emptyMetaContainer()
323319): ClobElement = ClobElementImpl (
324320 bytes = bytes.clone(),
325- annotations = annotations.toEmptyOrPersistentList (),
326- metas = metas.toPersistentMap ()
321+ annotations = annotations.toImmutableList (),
322+ metas = metas.toImmutableMap ()
327323)
328324/* *
329325 * Creates a [ClobElement] that represents an Ion `clob`.
@@ -346,9 +342,9 @@ public fun ionListOf(
346342 metas : MetaContainer = emptyMetaContainer()
347343): ListElement =
348344 ListElementImpl (
349- values = iterable.mapToEmptyOrPersistentList { it.asAnyElement() },
350- annotations = annotations.toEmptyOrPersistentList (),
351- metas = metas.toPersistentMap ()
345+ values = iterable.map { it.asAnyElement() }.toImmutableListUnsafe() ,
346+ annotations = annotations.toImmutableList (),
347+ metas = metas.toImmutableMap ()
352348 )
353349
354350/* * Creates a [ListElement] that represents an Ion `list`. */
@@ -395,9 +391,9 @@ public fun ionSexpOf(
395391 metas : MetaContainer = emptyMetaContainer()
396392): SexpElement =
397393 SexpElementImpl (
398- values = iterable.mapToEmptyOrPersistentList { it.asAnyElement() },
399- annotations = annotations.toEmptyOrPersistentList (),
400- metas = metas.toPersistentMap ()
394+ values = iterable.map { it.asAnyElement() }.toImmutableListUnsafe() ,
395+ annotations = annotations.toImmutableList (),
396+ metas = metas.toImmutableMap ()
401397 )
402398
403399/* * Creates an [SexpElement] that represents an Ion `sexp`. */
@@ -437,9 +433,9 @@ public fun ionStructOf(
437433 metas : MetaContainer = emptyMetaContainer()
438434): StructElement =
439435 StructElementImpl (
440- allFields = fields.toEmptyOrPersistentList (),
441- annotations = annotations.toEmptyOrPersistentList (),
442- metas = metas.toPersistentMap ()
436+ allFields = fields.toImmutableList (),
437+ annotations = annotations.toImmutableList (),
438+ metas = metas.toImmutableMap ()
443439 )
444440
445441/* * Creates a [StructElement] that represents an Ion `struct` with the specified fields. */
@@ -457,7 +453,7 @@ public fun ionStructOf(
457453): StructElement =
458454 ionStructOf(
459455 fields = fields.asIterable(),
460- annotations = annotations.toEmptyOrPersistentList (),
456+ annotations = annotations.toImmutableList (),
461457 metas = metas
462458 )
463459
@@ -523,55 +519,14 @@ public fun buildStruct(
523519 return ionStructOf(fields, annotations, metas)
524520}
525521
526- /* *
527- * Converts an `Iterable` to a `PersistentList`.
528- *
529- * ### Why is this needed?
530- * `kotlinx.collections.immutable` <= 0.3.7 has a bug that causes it to unnecessarily allocate empty `PersistentList`s
531- * instead of using a singleton instance. The fix is in
532- * [kotlinx.collections.immutable#176](https://github.com/Kotlin/kotlinx.collections.immutable/pull/176),
533- * but we cannot use it because the version of `kotlinx.collections.immutable` that will have (or has) the fix
534- * requires Kotlin stdlib >=1.9.2, and `ion-element-kotlin` supports consumers using Kotlin >= 1.3.0.
535- */
536- internal fun <E > Iterable<E>.toEmptyOrPersistentList (): PersistentList <E > {
537- val isEmpty = if (this is Collection <* >) {
538- this .isEmpty()
539- } else {
540- ! this .iterator().hasNext()
541- }
542- return if (isEmpty) EMPTY_PERSISTENT_LIST else toPersistentList()
543- }
544-
545- /* *
546- * Converts an `Iterable` to a `PersistentList`, transforming each element using [block].
547- *
548- * ### Why is this needed?
549- * `kotlinx.collections.immutable` <= 0.3.7 has a bug that causes it to unnecessarily allocate empty `PersistentList`s
550- * instead of using a singleton instance. The fix is in
551- * [kotlinx.collections.immutable#176](https://github.com/Kotlin/kotlinx.collections.immutable/pull/176),
552- * but we cannot use it because the version of `kotlinx.collections.immutable` that will have (or has) the fix
553- * requires Kotlin stdlib >=1.9.2, and `ion-element-kotlin` supports consumers using Kotlin >= 1.3.0.
554- */
555- internal inline fun <T , R > Iterable<T>.mapToEmptyOrPersistentList (block : (T ) -> R ): PersistentList <R > {
556- val isEmpty = if (this is Collection <* >) {
557- this .isEmpty()
558- } else {
559- ! this .iterator().hasNext()
560- }
561- return if (isEmpty) EMPTY_PERSISTENT_LIST else mapTo(persistentListOf<R >().builder(), block).build()
562- }
563-
564- // Memoized empty PersistentList for the memoized container types and null values
565- internal val EMPTY_PERSISTENT_LIST : PersistentList <Nothing > = persistentListOf()
566-
567522// Memoized empty instances of our container types.
568- private val EMPTY_LIST = ListElementImpl (EMPTY_PERSISTENT_LIST , EMPTY_PERSISTENT_LIST , EMPTY_METAS )
569- private val EMPTY_SEXP = SexpElementImpl (EMPTY_PERSISTENT_LIST , EMPTY_PERSISTENT_LIST , EMPTY_METAS )
570- private val EMPTY_STRUCT = StructElementImpl (EMPTY_PERSISTENT_LIST , EMPTY_PERSISTENT_LIST , EMPTY_METAS )
571- private val EMPTY_BLOB = BlobElementImpl (ByteArray (0 ), EMPTY_PERSISTENT_LIST , EMPTY_METAS )
572- private val EMPTY_CLOB = ClobElementImpl (ByteArray (0 ), EMPTY_PERSISTENT_LIST , EMPTY_METAS )
523+ private val EMPTY_LIST = ListElementImpl (EMPTY_IMMUTABLE_LIST , EMPTY_IMMUTABLE_LIST , EMPTY_METAS )
524+ private val EMPTY_SEXP = SexpElementImpl (EMPTY_IMMUTABLE_LIST , EMPTY_IMMUTABLE_LIST , EMPTY_METAS )
525+ private val EMPTY_STRUCT = StructElementImpl (EMPTY_IMMUTABLE_LIST , EMPTY_IMMUTABLE_LIST , EMPTY_METAS )
526+ private val EMPTY_BLOB = BlobElementImpl (ByteArray (0 ), EMPTY_IMMUTABLE_LIST , EMPTY_METAS )
527+ private val EMPTY_CLOB = ClobElementImpl (ByteArray (0 ), EMPTY_IMMUTABLE_LIST , EMPTY_METAS )
573528
574529// Memoized instances of all of our null values.
575530private val ALL_NULLS = ElementType .values().map {
576- it to NullElementImpl (it, EMPTY_PERSISTENT_LIST , EMPTY_METAS ) as IonElement
531+ it to NullElementImpl (it, EMPTY_IMMUTABLE_LIST , EMPTY_METAS ) as IonElement
577532}.toMap()
0 commit comments