diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index cb10a8ecddb..d10f8b4d07c 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -453,70 +453,70 @@ public fun Stream.contains(element: T): Boolean { } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Array.elementAt(index: Int): T { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun BooleanArray.elementAt(index: Int): Boolean { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun ByteArray.elementAt(index: Int): Byte { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun CharArray.elementAt(index: Int): Char { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun DoubleArray.elementAt(index: Int): Double { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun FloatArray.elementAt(index: Int): Float { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun IntArray.elementAt(index: Int): Int { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun LongArray.elementAt(index: Int): Long { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun ShortArray.elementAt(index: Int): Short { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Iterable.elementAt(index: Int): T { if (this is List) @@ -525,14 +525,14 @@ public fun Iterable.elementAt(index: Int): T { } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun List.elementAt(index: Int): T { return get(index) } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Sequence.elementAt(index: Int): T { return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index") } @@ -541,88 +541,90 @@ public fun Sequence.elementAt(index: Int): T { deprecated("Migrate to using Sequence and respective functions") /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun Stream.elementAt(index: Int): T { return elementAtOrElse(index) { throw IndexOutOfBoundsException("Stream doesn't contain element at index $index") } } /** - * Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection. */ public fun String.elementAt(index: Int): Char { return get(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Array.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun BooleanArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun ByteArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Byte): Byte { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun CharArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun DoubleArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Double): Double { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun FloatArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Float): Float { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun IntArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Int): Int { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun LongArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Long): Long { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun ShortArray.elementAtOrElse(index: Int, defaultValue: (Int) -> Short): Short { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { if (this is List) return this.elementAtOrElse(index, defaultValue) + if (index < 0) + return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -634,16 +636,18 @@ public inline fun Iterable.elementAtOrElse(index: Int, defaultValue: (Int } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + if (index < 0) + return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -657,9 +661,11 @@ public inline fun Sequence.elementAtOrElse(index: Int, defaultValue: (Int deprecated("Migrate to using Sequence and respective functions") /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun Stream.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { + if (index < 0) + return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -671,81 +677,83 @@ public inline fun Stream.elementAtOrElse(index: Int, defaultValue: (Int) } /** - * Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection. */ public inline fun String.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Array.elementAtOrNull(index: Int): T? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun BooleanArray.elementAtOrNull(index: Int): Boolean? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun ByteArray.elementAtOrNull(index: Int): Byte? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun CharArray.elementAtOrNull(index: Int): Char? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun DoubleArray.elementAtOrNull(index: Int): Double? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun FloatArray.elementAtOrNull(index: Int): Float? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun IntArray.elementAtOrNull(index: Int): Int? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun LongArray.elementAtOrNull(index: Int): Long? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun ShortArray.elementAtOrNull(index: Int): Short? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Iterable.elementAtOrNull(index: Int): T? { if (this is List) return this.elementAtOrNull(index) + if (index < 0) + return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -757,16 +765,18 @@ public fun Iterable.elementAtOrNull(index: Int): T? { } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun List.elementAtOrNull(index: Int): T? { return if (index >= 0 && index <= lastIndex) get(index) else null } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Sequence.elementAtOrNull(index: Int): T? { + if (index < 0) + return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -780,9 +790,11 @@ public fun Sequence.elementAtOrNull(index: Int): T? { deprecated("Migrate to using Sequence and respective functions") /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun Stream.elementAtOrNull(index: Int): T? { + if (index < 0) + return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -794,7 +806,7 @@ public fun Stream.elementAtOrNull(index: Int): T? { } /** - * Returns element at the given [index] or `null` if the [index] is out of bounds of this collection. + * Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection. */ public fun String.elementAtOrNull(index: Int): Char? { return if (index >= 0 && index <= lastIndex) get(index) else null diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index ea6a4608283..bf15b8413c4 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -15,7 +15,7 @@ class IterableWrapper(collection: Iterable) : Iterable { } } -class IterableTest : IterableTests>(iterableOf("foo", "bar"), iterableOf()) +class IterableTest : OrderedIterableTests>(iterableOf("foo", "bar"), iterableOf()) class SetTest : IterableTests>(setOf("foo", "bar"), setOf()) class LinkedSetTest : IterableTests>(linkedSetOf("foo", "bar"), linkedSetOf()) class ListTest : OrderedIterableTests>(listOf("foo", "bar"), listOf()) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index e0eee2af93a..656311871f4 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -177,7 +177,7 @@ fun elements(): List { templates add f("elementAt(index: Int)") { val index = '$' + "index" - doc { "Returns element at the given [index] or throws an IndexOutOfBoundsException if the [index] is out of bounds of this collection." } + doc { "Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this collection." } returns("T") body { """ @@ -200,13 +200,15 @@ fun elements(): List { } templates add f("elementAtOrElse(index: Int, defaultValue: (Int) -> T)") { - doc { "Returns element at the given [index] or calls [defaultValue] and returns its result if the [index] is out of bounds of this collection." } + doc { "Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this collection." } returns("T") inline(true) body { """ if (this is List) return this.elementAtOrElse(index, defaultValue) + if (index < 0) + return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -219,6 +221,8 @@ fun elements(): List { } body(Sequences) { """ + if (index < 0) + return defaultValue(index) val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -238,12 +242,14 @@ fun elements(): List { templates add f("elementAtOrNull(index: Int)") { - doc { "Returns element at the given [index] or `null` if the [index] is out of bounds of this collection." } + doc { "Returns an element at the given [index] or `null` if the [index] is out of bounds of this collection." } returns("T?") body { """ if (this is List) return this.elementAtOrNull(index) + if (index < 0) + return null val iterator = iterator() var count = 0 while (iterator.hasNext()) { @@ -256,6 +262,8 @@ fun elements(): List { } body(Sequences) { """ + if (index < 0) + return null val iterator = iterator() var count = 0 while (iterator.hasNext()) {