-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Iterable toImmutableArray extensions
- Loading branch information
1 parent
020a9f7
commit 2ddc172
Showing
4 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
buildSrc/src/main/kotlin/com/danrusu/pods4k/immutableArrays/CollectionExtensionsGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
immutable-arrays/src/main/kotlin/com/danrusu/pods4k/immutableArrays/Collections.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] } | ||
} |
46 changes: 46 additions & 0 deletions
46
immutable-arrays/src/test/kotlin/com/danrusu/pods4k/immutableArrays/CollectionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |