diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index 644e78e6de3..63c9893a82e 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -1928,99 +1928,63 @@ public inline fun String.single(predicate: (Char) -> Boolean): Char { * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun Array.singleOrNull(): T? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun BooleanArray.singleOrNull(): Boolean? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun ByteArray.singleOrNull(): Byte? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun CharArray.singleOrNull(): Char? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun DoubleArray.singleOrNull(): Double? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun FloatArray.singleOrNull(): Float? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun IntArray.singleOrNull(): Int? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun LongArray.singleOrNull(): Long? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun ShortArray.singleOrNull(): Short? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** @@ -2028,18 +1992,14 @@ public fun ShortArray.singleOrNull(): Short? { */ public fun Iterable.singleOrNull(): T? { when (this) { - is List<*> -> return when (size) { - 0 -> null - 1 -> this[0] as T? - else -> throw IllegalArgumentException("Collection has more than one element") - } + is List<*> -> return if (size == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) - throw IllegalArgumentException("Collection has more than one element") + return null return single } } @@ -2049,11 +2009,7 @@ public fun Iterable.singleOrNull(): T? { * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun List.singleOrNull(): T? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** @@ -2061,18 +2017,14 @@ public fun List.singleOrNull(): T? { */ public fun Stream.singleOrNull(): T? { when (this) { - is List<*> -> return when (size) { - 0 -> null - 1 -> this[0] as T? - else -> throw IllegalArgumentException("Collection has more than one element") - } + is List<*> -> return if (size == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) - throw IllegalArgumentException("Collection has more than one element") + return null return single } } @@ -2082,11 +2034,7 @@ public fun Stream.singleOrNull(): T? { * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun String.singleOrNull(): Char? { - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null } /** @@ -2097,7 +2045,7 @@ public inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2114,7 +2062,7 @@ public inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Bo var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2131,7 +2079,7 @@ public inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2148,7 +2096,7 @@ public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2165,7 +2113,7 @@ public inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Doub var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2182,7 +2130,7 @@ public inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2199,7 +2147,7 @@ public inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2216,7 +2164,7 @@ public inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2233,7 +2181,7 @@ public inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2250,7 +2198,7 @@ public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2267,7 +2215,7 @@ public inline fun Stream.singleOrNull(predicate: (T) -> Boolean): T? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } @@ -2284,7 +2232,7 @@ public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true } diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 1ecee2492c6..910796d2975 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -146,11 +146,11 @@ abstract class IterableTests>(val data: T, val empty: T) { Test fun singleOrNull() { - fails { data.singleOrNull() } - fails { empty.singleOrNull() } + expect(null) { data.singleOrNull() } + expect(null) { empty.singleOrNull() } expect("foo") { data.singleOrNull { it.startsWith("f") } } expect("bar") { data.singleOrNull { it.startsWith("b") } } - fails { + expect(null) { data.singleOrNull { it.length == 3 } } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 3daddb3d372..007c92b6b27 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -372,18 +372,14 @@ fun elements(): List { body { """ when (this) { - is List<*> -> return when (size) { - 0 -> null - 1 -> this[0] as T? - else -> throw IllegalArgumentException("Collection has more than one element") - } + is List<*> -> return if (size == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) - throw IllegalArgumentException("Collection has more than one element") + return null return single } } @@ -391,11 +387,7 @@ fun elements(): List { } body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { """ - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element") - } + return if (size == 1) this[0] else null """ } } @@ -431,7 +423,7 @@ fun elements(): List { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element") + if (found) return null single = element found = true }