Skip to content

Commit

Permalink
Add toMutableList method
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-rusu committed Mar 30, 2024
1 parent da9afb8 commit 9b21219
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private fun generateImmutableArrayFile(baseType: BaseType): FileSpec {
addFirst(baseType)
addLast(baseType)
addToList(baseType)
addToMutableList(baseType)
addIteratorOperator(baseType)
addAsSequence(baseType)
addForEach(baseType)
Expand Down Expand Up @@ -196,7 +197,7 @@ private fun TypeSpec.Builder.addToList(baseType: BaseType) {
val listType = List::class.asTypeName().parameterizedBy(baseType.type)

addFunction(
kdoc = "Returns a [List] containing all elements.",
kdoc = "Returns a [List] containing all the elements.",
name = "toList",
returns = listType,
) {
Expand All @@ -209,6 +210,24 @@ private fun TypeSpec.Builder.addToList(baseType: BaseType) {
}
}

private fun TypeSpec.Builder.addToMutableList(baseType: BaseType) {
val mutableListClassName = ClassName("kotlin.collections", "MutableList")
val listType = mutableListClassName.parameterizedBy(baseType.type)

addFunction(
kdoc = "Returns a [MutableList] containing all the elements.",
name = "toMutableList",
returns = listType,
) {
if (baseType == BaseType.GENERIC) {
suppress("UNCHECKED_CAST")
addStatement("return values.toMutableList() as %T", listType)
} else {
addStatement("return values.toMutableList()")
}
}
}

private fun TypeSpec.Builder.addIteratorOperator(baseType: BaseType) {
val iteratorType = Iterator::class.asClassName().parameterizedBy(baseType.type)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.Suppress
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -91,11 +92,17 @@ public value class ImmutableArray<T> @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
@Suppress("UNCHECKED_CAST")
public fun toList(): List<T> = values.toList() as List<T>

/**
* Returns a [MutableList] containing all the elements.
*/
@Suppress("UNCHECKED_CAST")
public fun toMutableList(): MutableList<T> = values.toMutableList() as MutableList<T>

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -88,10 +89,15 @@ public value class ImmutableBooleanArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Boolean> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Boolean> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -89,10 +90,15 @@ public value class ImmutableByteArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Byte> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Byte> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -89,10 +90,15 @@ public value class ImmutableCharArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Char> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Char> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -89,10 +90,15 @@ public value class ImmutableDoubleArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Double> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Double> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -89,10 +90,15 @@ public value class ImmutableFloatArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Float> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Float> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -88,10 +89,15 @@ public value class ImmutableIntArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Int> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Int> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -89,10 +90,15 @@ public value class ImmutableLongArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Long> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Long> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.String
import kotlin.Unit
import kotlin.collections.Iterator
import kotlin.collections.List
import kotlin.collections.MutableList
import kotlin.jvm.JvmInline
import kotlin.ranges.IntRange
import kotlin.sequences.Sequence
Expand Down Expand Up @@ -89,10 +90,15 @@ public value class ImmutableShortArray @PublishedApi internal constructor(
}

/**
* Returns a [List] containing all elements.
* Returns a [List] containing all the elements.
*/
public fun toList(): List<Short> = values.toList()

/**
* Returns a [MutableList] containing all the elements.
*/
public fun toMutableList(): MutableList<Short> = values.toMutableList()

/**
* Returns an iterator allowing iteration over the elements of the array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ class ImmutableArrayTest {
}
}

@Test
fun `toMutableList validation`() {
with(ImmutableArray(3) { "element $it" }) {
expectThat(this.toMutableList()).isEqualTo(mutableListOf("element 0", "element 1", "element 2"))
}
}

@Test
fun `iterator validation`() {
with(ImmutableArray(0) { "element $it" }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ class ImmutableIntArrayTest {
}
}

@Test
fun `toMutableList validation`() {
with(ImmutableIntArray(3) { it }) {
expectThat(this.toMutableList()).isEqualTo(mutableListOf(0, 1, 2))
}
}

@Test
fun `iterator validation`() {
with(ImmutableIntArray(0) { it }) {
Expand Down

0 comments on commit 9b21219

Please sign in to comment.