Skip to content

Commit

Permalink
Add Iterable toImmutableArray extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-rusu committed Apr 9, 2024
1 parent 020a9f7 commit 2ddc172
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/com/danrusu/pods4k/CodeGenerator.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.danrusu.pods4k

import com.danrusu.pods4k.immutableArrays.ArrayExtensionsGenerator
import com.danrusu.pods4k.immutableArrays.CollectionExtensionsGenerator
import com.danrusu.pods4k.immutableArrays.ImmutableArrayCodeGenerator
import com.danrusu.pods4k.immutableArrays.ImmutableArrayFactoryFunctionsGenerator
import org.gradle.api.Plugin
Expand All @@ -18,6 +19,7 @@ public open class CodeGenerator : Plugin<Project> {
ImmutableArrayCodeGenerator.generate(destinationPath = immutableArrayPath)
ImmutableArrayFactoryFunctionsGenerator.generate(destinationPath = immutableArrayPath)
ArrayExtensionsGenerator.generate(destinationPath = immutableArrayPath)
CollectionExtensionsGenerator.generate(destinationPath = immutableArrayPath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.danrusu.pods4k.immutableArrays

import com.danrusu.pods4k.utils.addFunction
import com.danrusu.pods4k.utils.createFile
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.asTypeName
import java.io.File

internal object CollectionExtensionsGenerator {
fun generate(destinationPath: String) {
val fileSpec = createFile(Config.packageName, "Collections") {
addIterableToImmutableArray()
}
fileSpec.writeTo(File(destinationPath, ""))
}
}

private fun FileSpec.Builder.addIterableToImmutableArray() {
for (baseType in BaseType.values()) {
addFunction(
kdoc = "Returns an [${baseType.generatedClassName}] with the contents of [this] collection.",
receiver = Iterable::class.asTypeName().parameterizedBy(baseType.type),
name = "to${baseType.generatedClassName}",
returns = baseType.getGeneratedTypeName(),
) {
if (baseType == BaseType.GENERIC) {
addTypeVariable(baseType.type as TypeVariableName)
}
addCode(
"""
if (this is Collection<${baseType.type}>) {
val iterator = this.iterator()
return ${baseType.generatedClassName}(size) { iterator.next() }
}
val values = this.toList()
return ${baseType.generatedClassName}(values.size) { values[it] }
""".trimIndent()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Auto-generated file. DO NOT EDIT!
package com.danrusu.pods4k.immutableArrays

import kotlin.Boolean
import kotlin.Byte
import kotlin.Char
import kotlin.Double
import kotlin.Float
import kotlin.Int
import kotlin.Long
import kotlin.Short
import kotlin.collections.Iterable

/**
* Returns an [ImmutableArray] with the contents of [this] collection.
*/
public fun <T> Iterable<T>.toImmutableArray(): ImmutableArray<T> {
if (this is Collection<T>) {
val iterator = this.iterator()
return ImmutableArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableBooleanArray] with the contents of [this] collection.
*/
public fun Iterable<Boolean>.toImmutableBooleanArray(): ImmutableBooleanArray {
if (this is Collection<kotlin.Boolean>) {
val iterator = this.iterator()
return ImmutableBooleanArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableBooleanArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableByteArray] with the contents of [this] collection.
*/
public fun Iterable<Byte>.toImmutableByteArray(): ImmutableByteArray {
if (this is Collection<kotlin.Byte>) {
val iterator = this.iterator()
return ImmutableByteArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableByteArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableCharArray] with the contents of [this] collection.
*/
public fun Iterable<Char>.toImmutableCharArray(): ImmutableCharArray {
if (this is Collection<kotlin.Char>) {
val iterator = this.iterator()
return ImmutableCharArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableCharArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableShortArray] with the contents of [this] collection.
*/
public fun Iterable<Short>.toImmutableShortArray(): ImmutableShortArray {
if (this is Collection<kotlin.Short>) {
val iterator = this.iterator()
return ImmutableShortArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableShortArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableIntArray] with the contents of [this] collection.
*/
public fun Iterable<Int>.toImmutableIntArray(): ImmutableIntArray {
if (this is Collection<kotlin.Int>) {
val iterator = this.iterator()
return ImmutableIntArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableIntArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableLongArray] with the contents of [this] collection.
*/
public fun Iterable<Long>.toImmutableLongArray(): ImmutableLongArray {
if (this is Collection<kotlin.Long>) {
val iterator = this.iterator()
return ImmutableLongArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableLongArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableFloatArray] with the contents of [this] collection.
*/
public fun Iterable<Float>.toImmutableFloatArray(): ImmutableFloatArray {
if (this is Collection<kotlin.Float>) {
val iterator = this.iterator()
return ImmutableFloatArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableFloatArray(values.size) { values[it] }
}

/**
* Returns an [ImmutableDoubleArray] with the contents of [this] collection.
*/
public fun Iterable<Double>.toImmutableDoubleArray(): ImmutableDoubleArray {
if (this is Collection<kotlin.Double>) {
val iterator = this.iterator()
return ImmutableDoubleArray(size) { iterator.next() }
}
val values = this.toList()
return ImmutableDoubleArray(values.size) { values[it] }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.danrusu.pods4k.immutableArrays

import org.junit.jupiter.api.Test
import strikt.api.expectThat
import strikt.assertions.isA
import strikt.assertions.isEqualTo
import strikt.assertions.isTrue

class CollectionsTest {
@Test
fun `toImmutableArray validation`() {
with(listOf<String>()) {
val immutableArray = this.toImmutableArray()
expectThat(immutableArray).isA<ImmutableArray<String>>()
expectThat(immutableArray.isEmpty()).isTrue()
}
with(listOf(1, 3)) {
val immutableArray = this.toImmutableArray()
expectThat(immutableArray).isA<ImmutableArray<Int>>()
expectThat(immutableArray.size).isEqualTo(2)
expectThat(immutableArray[0]).isEqualTo(1)
expectThat(immutableArray[1]).isEqualTo(3)

val immutableIntArray = this.toImmutableIntArray()
expectThat(immutableIntArray).isA<ImmutableIntArray>()
expectThat(immutableIntArray.size).isEqualTo(2)
expectThat(immutableIntArray[0]).isEqualTo(1)
expectThat(immutableIntArray[1]).isEqualTo(3)
}
with(IntRange(start = 3, endInclusive = 5)) {
val immutableArray = this.toImmutableArray()
expectThat(immutableArray).isA<ImmutableArray<Int>>()
expectThat(immutableArray.size).isEqualTo(3)
expectThat(immutableArray[0]).isEqualTo(3)
expectThat(immutableArray[1]).isEqualTo(4)
expectThat(immutableArray[2]).isEqualTo(5)

val immutableIntArray = this.toImmutableIntArray()
expectThat(immutableIntArray).isA<ImmutableIntArray>()
expectThat(immutableIntArray.size).isEqualTo(3)
expectThat(immutableIntArray[0]).isEqualTo(3)
expectThat(immutableIntArray[1]).isEqualTo(4)
expectThat(immutableIntArray[2]).isEqualTo(5)
}
}
}

0 comments on commit 2ddc172

Please sign in to comment.