From 19fa2f51ea893d2763846343fe66bd6ac1739ec7 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 13 Feb 2016 00:17:38 +0300 Subject: [PATCH] Review and correct exception messages. --- libraries/stdlib/src/generated/_Arrays.kt | 216 +++++++++--------- .../stdlib/src/generated/_Collections.kt | 50 ++-- libraries/stdlib/src/generated/_Sequences.kt | 16 +- libraries/stdlib/src/generated/_Strings.kt | 34 +-- .../src/kotlin/collections/Collections.kt | 4 +- .../src/kotlin/collections/ReversedViews.kt | 4 +- .../src/kotlin/collections/Sequences.kt | 4 +- libraries/stdlib/src/kotlin/io/IOStreams.kt | 2 +- .../src/kotlin/io/files/FileTreeWalk.kt | 2 +- libraries/stdlib/src/kotlin/io/files/Utils.kt | 18 +- libraries/stdlib/src/kotlin/ranges/Ranges.kt | 2 +- libraries/stdlib/src/kotlin/text/Indent.kt | 2 +- libraries/stdlib/src/kotlin/text/Strings.kt | 8 +- .../stdlib/src/kotlin/text/StringsJVM.kt | 2 +- .../stdlib/src/kotlin/text/regex/Regex.kt | 2 +- .../stdlib/src/kotlin/util/Preconditions.kt | 8 +- .../src/templates/Aggregates.kt | 16 +- .../src/templates/Elements.kt | 64 ++---- .../src/templates/Filtering.kt | 31 +-- 19 files changed, 220 insertions(+), 265 deletions(-) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index a7b091514fc..be082fb19b2 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -802,7 +802,7 @@ public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? { */ public fun Array.first(): T { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -812,7 +812,7 @@ public fun Array.first(): T { */ public fun ByteArray.first(): Byte { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -822,7 +822,7 @@ public fun ByteArray.first(): Byte { */ public fun ShortArray.first(): Short { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -832,7 +832,7 @@ public fun ShortArray.first(): Short { */ public fun IntArray.first(): Int { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -842,7 +842,7 @@ public fun IntArray.first(): Int { */ public fun LongArray.first(): Long { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -852,7 +852,7 @@ public fun LongArray.first(): Long { */ public fun FloatArray.first(): Float { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -862,7 +862,7 @@ public fun FloatArray.first(): Float { */ public fun DoubleArray.first(): Double { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -872,7 +872,7 @@ public fun DoubleArray.first(): Double { */ public fun BooleanArray.first(): Boolean { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -882,7 +882,7 @@ public fun BooleanArray.first(): Boolean { */ public fun CharArray.first(): Char { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[0] } @@ -892,7 +892,7 @@ public fun CharArray.first(): Char { */ public inline fun Array.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -901,7 +901,7 @@ public inline fun Array.first(predicate: (T) -> Boolean): T { */ public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -910,7 +910,7 @@ public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { */ public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -919,7 +919,7 @@ public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { */ public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -928,7 +928,7 @@ public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { */ public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -937,7 +937,7 @@ public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { */ public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -946,7 +946,7 @@ public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { */ public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -955,7 +955,7 @@ public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { */ public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -964,7 +964,7 @@ public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { */ public inline fun CharArray.first(predicate: (Char) -> Boolean): Char { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1575,7 +1575,7 @@ public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int { */ public fun Array.last(): T { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1585,7 +1585,7 @@ public fun Array.last(): T { */ public fun ByteArray.last(): Byte { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1595,7 +1595,7 @@ public fun ByteArray.last(): Byte { */ public fun ShortArray.last(): Short { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1605,7 +1605,7 @@ public fun ShortArray.last(): Short { */ public fun IntArray.last(): Int { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1615,7 +1615,7 @@ public fun IntArray.last(): Int { */ public fun LongArray.last(): Long { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1625,7 +1625,7 @@ public fun LongArray.last(): Long { */ public fun FloatArray.last(): Float { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1635,7 +1635,7 @@ public fun FloatArray.last(): Float { */ public fun DoubleArray.last(): Double { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1645,7 +1645,7 @@ public fun DoubleArray.last(): Double { */ public fun BooleanArray.last(): Boolean { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1655,7 +1655,7 @@ public fun BooleanArray.last(): Boolean { */ public fun CharArray.last(): Char { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Array is empty.") return this[lastIndex] } @@ -1668,7 +1668,7 @@ public inline fun Array.last(predicate: (T) -> Boolean): T { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1680,7 +1680,7 @@ public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1692,7 +1692,7 @@ public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1704,7 +1704,7 @@ public inline fun IntArray.last(predicate: (Int) -> Boolean): Int { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1716,7 +1716,7 @@ public inline fun LongArray.last(predicate: (Long) -> Boolean): Long { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1728,7 +1728,7 @@ public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1740,7 +1740,7 @@ public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1752,7 +1752,7 @@ public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -1764,7 +1764,7 @@ public inline fun CharArray.last(predicate: (Char) -> Boolean): Char { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Array contains no element matching the predicate.") } /** @@ -2050,9 +2050,9 @@ public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? { */ public fun Array.single(): T { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2061,9 +2061,9 @@ public fun Array.single(): T { */ public fun ByteArray.single(): Byte { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2072,9 +2072,9 @@ public fun ByteArray.single(): Byte { */ public fun ShortArray.single(): Short { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2083,9 +2083,9 @@ public fun ShortArray.single(): Short { */ public fun IntArray.single(): Int { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2094,9 +2094,9 @@ public fun IntArray.single(): Int { */ public fun LongArray.single(): Long { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2105,9 +2105,9 @@ public fun LongArray.single(): Long { */ public fun FloatArray.single(): Float { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2116,9 +2116,9 @@ public fun FloatArray.single(): Float { */ public fun DoubleArray.single(): Double { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2127,9 +2127,9 @@ public fun DoubleArray.single(): Double { */ public fun BooleanArray.single(): Boolean { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2138,9 +2138,9 @@ public fun BooleanArray.single(): Boolean { */ public fun CharArray.single(): Char { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Array is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Array has more than one element.") } } @@ -2152,12 +2152,12 @@ public inline fun Array.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as T } @@ -2169,12 +2169,12 @@ public inline fun ByteArray.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Byte } @@ -2186,12 +2186,12 @@ public inline fun ShortArray.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Short } @@ -2203,12 +2203,12 @@ public inline fun IntArray.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Int } @@ -2220,12 +2220,12 @@ public inline fun LongArray.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Long } @@ -2237,12 +2237,12 @@ public inline fun FloatArray.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Float } @@ -2254,12 +2254,12 @@ public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + if (found) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Double } @@ -2271,12 +2271,12 @@ public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + if (found) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Boolean } @@ -2288,12 +2288,12 @@ public inline fun CharArray.single(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) throw IllegalArgumentException("Array contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") return single as Char } @@ -2517,7 +2517,7 @@ public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { * Returns a list containing all elements except first [n] elements. */ public fun Array.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2533,7 +2533,7 @@ public fun Array.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun ByteArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2549,7 +2549,7 @@ public fun ByteArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun ShortArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2565,7 +2565,7 @@ public fun ShortArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun IntArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2581,7 +2581,7 @@ public fun IntArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun LongArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2597,7 +2597,7 @@ public fun LongArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun FloatArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2613,7 +2613,7 @@ public fun FloatArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun DoubleArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2629,7 +2629,7 @@ public fun DoubleArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun BooleanArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2645,7 +2645,7 @@ public fun BooleanArray.drop(n: Int): List { * Returns a list containing all elements except first [n] elements. */ public fun CharArray.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -2661,7 +2661,7 @@ public fun CharArray.drop(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun Array.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2669,7 +2669,7 @@ public fun Array.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun ByteArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2677,7 +2677,7 @@ public fun ByteArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun ShortArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2685,7 +2685,7 @@ public fun ShortArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun IntArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2693,7 +2693,7 @@ public fun IntArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun LongArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2701,7 +2701,7 @@ public fun LongArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun FloatArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2709,7 +2709,7 @@ public fun FloatArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun DoubleArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2717,7 +2717,7 @@ public fun DoubleArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun BooleanArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -2725,7 +2725,7 @@ public fun BooleanArray.dropLast(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun CharArray.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -3981,7 +3981,7 @@ public fun CharArray.take(n: Int): List { * Returns a list containing last [n] elements. */ public fun Array.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -3995,7 +3995,7 @@ public fun Array.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun ByteArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4009,7 +4009,7 @@ public fun ByteArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun ShortArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4023,7 +4023,7 @@ public fun ShortArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun IntArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4037,7 +4037,7 @@ public fun IntArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun LongArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4051,7 +4051,7 @@ public fun LongArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun FloatArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4065,7 +4065,7 @@ public fun FloatArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun DoubleArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4079,7 +4079,7 @@ public fun DoubleArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun BooleanArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -4093,7 +4093,7 @@ public fun BooleanArray.takeLast(n: Int): List { * Returns a list containing last [n] elements. */ public fun CharArray.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -10351,7 +10351,7 @@ public inline fun CharArray.reduceIndexed(operation: (Int, Char, Char) -> Char): */ public inline fun Array.reduceRight(operation: (T, S) -> S): S { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator: S = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10364,7 +10364,7 @@ public inline fun Array.reduceRight(operation: (T, S) -> S): S */ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10377,7 +10377,7 @@ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { */ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Short { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10390,7 +10390,7 @@ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Sh */ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10403,7 +10403,7 @@ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int { */ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10416,7 +10416,7 @@ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long { */ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Float { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10429,7 +10429,7 @@ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Fl */ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double): Double { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10442,7 +10442,7 @@ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double) */ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -10455,7 +10455,7 @@ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Bool */ public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty array can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index 3c0c9bc0391..e5129bcc22c 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -161,12 +161,7 @@ public inline fun List.findLast(predicate: (T) -> Boolean): T? { */ public fun Iterable.first(): T { when (this) { - is List -> { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - else - return this[0] - } + is List -> return this.first() else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -182,7 +177,7 @@ public fun Iterable.first(): T { */ public fun List.first(): T { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("List is empty.") return this[0] } @@ -192,7 +187,7 @@ public fun List.first(): T { */ public inline fun Iterable.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Collection contains no element matching the predicate.") } /** @@ -323,12 +318,7 @@ public inline fun List.indexOfLast(predicate: (T) -> Boolean): Int { */ public fun Iterable.last(): T { when (this) { - is List -> { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - else - return this[this.lastIndex] - } + is List -> return this.last() else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -347,7 +337,7 @@ public fun Iterable.last(): T { */ public fun List.last(): T { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("List is empty.") return this[lastIndex] } @@ -366,7 +356,7 @@ public inline fun Iterable.last(predicate: (T) -> Boolean): T { found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") return last as T } @@ -379,7 +369,7 @@ public inline fun List.last(predicate: (T) -> Boolean): T { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("List contains no element matching the predicate.") } /** @@ -460,11 +450,7 @@ public inline fun List.lastOrNull(predicate: (T) -> Boolean): T? { */ public fun Iterable.single(): T { when (this) { - is List -> return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } + is List -> return this.single() else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -482,9 +468,9 @@ public fun Iterable.single(): T { */ public fun List.single(): T { return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("List is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("List has more than one element.") } } @@ -501,7 +487,7 @@ public inline fun Iterable.single(predicate: (T) -> Boolean): T { found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") return single as T } @@ -551,7 +537,7 @@ public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { * Returns a list containing all elements except first [n] elements. */ public fun Iterable.drop(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() val list: ArrayList if (this is Collection<*>) { @@ -580,7 +566,7 @@ public fun Iterable.drop(n: Int): List { * Returns a list containing all elements except last [n] elements. */ public fun List.dropLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return take((size - n).coerceAtLeast(0)) } @@ -703,7 +689,7 @@ public fun List.slice(indices: Iterable): List { * Returns a list containing first [n] elements. */ public fun Iterable.take(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() if (this is Collection && n >= size) return toList() var count = 0 @@ -720,7 +706,7 @@ public fun Iterable.take(n: Int): List { * Returns a list containing last [n] elements. */ public fun List.takeLast(n: Int): List { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList() @@ -1509,7 +1495,7 @@ public inline fun Iterable.none(predicate: (T) -> Boolean): Boolean { */ public inline fun Iterable.reduce(operation: (S, T) -> S): S { val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") var accumulator: S = iterator.next() while (iterator.hasNext()) { accumulator = operation(accumulator, iterator.next()) @@ -1525,7 +1511,7 @@ public inline fun Iterable.reduce(operation: (S, T) -> S): S { */ public inline fun Iterable.reduceIndexed(operation: (Int, S, T) -> S): S { val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.") var index = 1 var accumulator: S = iterator.next() while (iterator.hasNext()) { @@ -1539,7 +1525,7 @@ public inline fun Iterable.reduceIndexed(operation: (Int, S, T) -> */ public inline fun List.reduceRight(operation: (T, S) -> S): S { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty list can't be reduced.") var accumulator: S = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 5a56d284fde..b8d958e931c 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -92,7 +92,7 @@ public fun Sequence.first(): T { */ public inline fun Sequence.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Sequence contains no element matching the predicate.") } /** @@ -180,7 +180,7 @@ public inline fun Sequence.last(predicate: (T) -> Boolean): T { found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") return last as T } @@ -245,12 +245,12 @@ public inline fun Sequence.single(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) throw IllegalArgumentException("Sequence contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") return single as T } @@ -288,7 +288,7 @@ public inline fun Sequence.singleOrNull(predicate: (T) -> Boolean): T? { * Returns a sequence containing all elements except first [n] elements. */ public fun Sequence.drop(n: Int): Sequence { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return when { n == 0 -> this this is DropTakeSequence -> this.drop(n) @@ -375,7 +375,7 @@ public inline fun > Sequence.filterTo(destinat * Returns a sequence containing first [n] elements. */ public fun Sequence.take(n: Int): Sequence { - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return when { n == 0 -> emptySequence() this is DropTakeSequence -> this.take(n) @@ -931,7 +931,7 @@ public inline fun Sequence.none(predicate: (T) -> Boolean): Boolean { */ public inline fun Sequence.reduce(operation: (S, T) -> S): S { val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") var accumulator: S = iterator.next() while (iterator.hasNext()) { accumulator = operation(accumulator, iterator.next()) @@ -947,7 +947,7 @@ public inline fun Sequence.reduce(operation: (S, T) -> S): S { */ public inline fun Sequence.reduceIndexed(operation: (Int, S, T) -> S): S { val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty sequence can't be reduced.") var index = 1 var accumulator: S = iterator.next() while (iterator.hasNext()) { diff --git a/libraries/stdlib/src/generated/_Strings.kt b/libraries/stdlib/src/generated/_Strings.kt index b4c0f1f1e77..d1919f449d1 100644 --- a/libraries/stdlib/src/generated/_Strings.kt +++ b/libraries/stdlib/src/generated/_Strings.kt @@ -59,7 +59,7 @@ public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? { */ public fun CharSequence.first(): Char { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Char sequence is empty.") return this[0] } @@ -69,7 +69,7 @@ public fun CharSequence.first(): Char { */ public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char { for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("Char sequence contains no character matching the predicate.") } /** @@ -132,7 +132,7 @@ public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int { */ public fun CharSequence.last(): Char { if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("Char sequence is empty.") return this[lastIndex] } @@ -145,7 +145,7 @@ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("Char sequence contains no character matching the predicate.") } /** @@ -171,9 +171,9 @@ public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? { */ public fun CharSequence.single(): Char { return when (length) { - 0 -> throw NoSuchElementException("Collection is empty.") + 0 -> throw NoSuchElementException("Char sequence is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("Char sequence has more than one element.") } } @@ -185,12 +185,12 @@ public inline fun CharSequence.single(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) throw IllegalArgumentException("Char sequence contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("Char sequence contains no character matching the predicate.") return single as Char } @@ -222,7 +222,7 @@ public inline fun CharSequence.singleOrNull(predicate: (Char) -> Boolean): Char? * Returns a subsequence of this char sequence with the first [n] characters removed. */ public fun CharSequence.drop(n: Int): CharSequence { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return subSequence(n.coerceAtMost(length), length) } @@ -230,7 +230,7 @@ public fun CharSequence.drop(n: Int): CharSequence { * Returns a string with the first [n] characters removed. */ public fun String.drop(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return substring(n.coerceAtMost(length)) } @@ -238,7 +238,7 @@ public fun String.drop(n: Int): String { * Returns a subsequence of this char sequence with the last [n] characters removed. */ public fun CharSequence.dropLast(n: Int): CharSequence { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return take((length - n).coerceAtLeast(0)) } @@ -246,7 +246,7 @@ public fun CharSequence.dropLast(n: Int): CharSequence { * Returns a string with the last [n] characters removed. */ public fun String.dropLast(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return take((length - n).coerceAtLeast(0)) } @@ -408,7 +408,7 @@ public inline fun String.slice(indices: Iterable): String { * Returns a subsequence of this char sequence containing the first [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. */ public fun CharSequence.take(n: Int): CharSequence { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return subSequence(0, n.coerceAtMost(length)) } @@ -416,7 +416,7 @@ public fun CharSequence.take(n: Int): CharSequence { * Returns a string containing the first [n] characters from this string, or the entire string if this string is shorter. */ public fun String.take(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return substring(0, n.coerceAtMost(length)) } @@ -424,7 +424,7 @@ public fun String.take(n: Int): String { * Returns a subsequence of this char sequence containing the last [n] characters from this char sequence, or the entire char sequence if this char sequence is shorter. */ public fun CharSequence.takeLast(n: Int): CharSequence { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } val length = length return subSequence(length - n.coerceAtMost(length), length) } @@ -433,7 +433,7 @@ public fun CharSequence.takeLast(n: Int): CharSequence { * Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter. */ public fun String.takeLast(n: Int): String { - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } val length = length return substring(length - n.coerceAtMost(length)) } @@ -1014,7 +1014,7 @@ public inline fun CharSequence.reduceIndexed(operation: (Int, Char, Char) -> Cha */ public inline fun CharSequence.reduceRight(operation: (Char, Char) -> Char): Char { var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty char sequence can't be reduced.") var accumulator = get(index--) while (index >= 0) { accumulator = operation(get(index--), accumulator) diff --git a/libraries/stdlib/src/kotlin/collections/Collections.kt b/libraries/stdlib/src/kotlin/collections/Collections.kt index 483af5587d0..5898eb8400f 100644 --- a/libraries/stdlib/src/kotlin/collections/Collections.kt +++ b/libraries/stdlib/src/kotlin/collections/Collections.kt @@ -26,7 +26,7 @@ internal object EmptyList : List, Serializable { override fun contains(element: Nothing): Boolean = false override fun containsAll(elements: Collection): Boolean = elements.isEmpty() - override fun get(index: Int): Nothing = throw IndexOutOfBoundsException("Index $index is out of bound of empty list.") + override fun get(index: Int): Nothing = throw IndexOutOfBoundsException("Empty list doesn't contain element at index $index.") override fun indexOf(element: Nothing): Int = -1 override fun lastIndexOf(element: Nothing): Int = -1 @@ -274,7 +274,7 @@ public fun List.binarySearch(fromIndex: Int = 0, toIndex: Int = size, com */ private fun rangeCheck(size: Int, fromIndex: Int, toIndex: Int) { when { - fromIndex > toIndex -> throw IllegalArgumentException("fromIndex ($fromIndex) is greater than toIndex ($toIndex)") + fromIndex > toIndex -> throw IllegalArgumentException("fromIndex ($fromIndex) is greater than toIndex ($toIndex).") fromIndex < 0 -> throw IndexOutOfBoundsException("fromIndex ($fromIndex) is less than zero.") toIndex > size -> throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).") } diff --git a/libraries/stdlib/src/kotlin/collections/ReversedViews.kt b/libraries/stdlib/src/kotlin/collections/ReversedViews.kt index 8a3f6579fc2..95ac5dd0940 100644 --- a/libraries/stdlib/src/kotlin/collections/ReversedViews.kt +++ b/libraries/stdlib/src/kotlin/collections/ReversedViews.kt @@ -24,8 +24,8 @@ private open class ReversedListReadOnly(protected open val delegate: List) override val size: Int get() = delegate.size override fun get(index: Int): T = delegate[index.flipIndex()] - protected fun Int.flipIndex(): Int = if (this in 0..size - 1) size - this - 1 else throw IndexOutOfBoundsException("index $this should be in range [${0..size - 1}]") - protected fun Int.flipIndexForward(): Int = if (this in 0..size) size - this else throw IndexOutOfBoundsException("index $this should be in range [${0..size}]") + protected fun Int.flipIndex(): Int = if (this in 0..size - 1) size - this - 1 else throw IndexOutOfBoundsException("index $this should be in range [${0..size - 1}].") + protected fun Int.flipIndexForward(): Int = if (this in 0..size) size - this else throw IndexOutOfBoundsException("index $this should be in range [${0..size}].") } private class ReversedList(protected override val delegate: MutableList) : ReversedListReadOnly(delegate) { diff --git a/libraries/stdlib/src/kotlin/collections/Sequences.kt b/libraries/stdlib/src/kotlin/collections/Sequences.kt index 372ae26d7db..d0c3ecdfa2e 100644 --- a/libraries/stdlib/src/kotlin/collections/Sequences.kt +++ b/libraries/stdlib/src/kotlin/collections/Sequences.kt @@ -316,7 +316,7 @@ internal class TakeSequence ( ) : Sequence, DropTakeSequence { init { - require (count >= 0) { throw IllegalArgumentException("count should be non-negative, but is $count") } + require (count >= 0) { throw IllegalArgumentException("count must be non-negative, but was $count.") } } override fun drop(n: Int): Sequence = if (n >= count) emptySequence() else SubSequence(sequence, n, count) @@ -394,7 +394,7 @@ internal class DropSequence ( private val count: Int ) : Sequence, DropTakeSequence { init { - require (count >= 0) { throw IllegalArgumentException("count should be non-negative, but is $count") } + require (count >= 0) { throw IllegalArgumentException("count must be non-negative, but was $count.") } } override fun drop(n: Int): Sequence = DropSequence(sequence, count + n) diff --git a/libraries/stdlib/src/kotlin/io/IOStreams.kt b/libraries/stdlib/src/kotlin/io/IOStreams.kt index 3976ffa6824..4fcee1c1dd8 100644 --- a/libraries/stdlib/src/kotlin/io/IOStreams.kt +++ b/libraries/stdlib/src/kotlin/io/IOStreams.kt @@ -32,7 +32,7 @@ public operator fun BufferedInputStream.iterator(): ByteIterator = public override fun nextByte(): Byte { prepareNext() if (finished) - throw NoSuchElementException("Input stream is over") + throw NoSuchElementException("Input stream is over.") val res = nextByte.toByte() nextPrepared = false return res diff --git a/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt b/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt index a489f53f9eb..1887fbf1974 100644 --- a/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt +++ b/libraries/stdlib/src/kotlin/io/files/FileTreeWalk.kt @@ -252,7 +252,7 @@ public class FileTreeWalk private constructor( */ public fun maxDepth(depth: Int): FileTreeWalk { if (depth <= 0) - throw IllegalArgumentException("Use positive depth value") + throw IllegalArgumentException("depth must be positive, but was $depth.") return FileTreeWalk(start, direction, onEnter, onLeave, onFail, depth) } } diff --git a/libraries/stdlib/src/kotlin/io/files/Utils.kt b/libraries/stdlib/src/kotlin/io/files/Utils.kt index c55abe63b30..bf3a11204f3 100644 --- a/libraries/stdlib/src/kotlin/io/files/Utils.kt +++ b/libraries/stdlib/src/kotlin/io/files/Utils.kt @@ -24,7 +24,7 @@ public fun createTempDir(prefix: String = "tmp", suffix: String? = null, directo if (dir.mkdir()) { return dir } else { - throw IOException("Unable to create temporary directory $dir") + throw IOException("Unable to create temporary directory $dir.") } } @@ -73,7 +73,7 @@ public val File.nameWithoutExtension: String * @throws IllegalArgumentException if this and base paths have different roots. */ public fun File.toRelativeString(base: File): String - = toRelativeStringOrNull(base) ?: throw IllegalArgumentException("this and base files have different roots: $this and $base") + = toRelativeStringOrNull(base) ?: throw IllegalArgumentException("this and base files have different roots: $this and $base.") /** * Calculates the relative path for this file from [base] file. @@ -173,7 +173,7 @@ private fun File.toRelativeStringOrNull(base: File): String? { */ public fun File.copyTo(target: File, overwrite: Boolean = false, bufferSize: Int = DEFAULT_BUFFER_SIZE): File { if (!this.exists()) { - throw NoSuchFileException(file = this, reason = "The source file doesn't exist") + throw NoSuchFileException(file = this, reason = "The source file doesn't exist.") } if (target.exists()) { @@ -182,13 +182,13 @@ public fun File.copyTo(target: File, overwrite: Boolean = false, bufferSize: Int if (stillExists) { throw FileAlreadyExistsException(file = this, other = target, - reason = "The destination file already exists") + reason = "The destination file already exists.") } } if (this.isDirectory) { if (!target.mkdirs()) - throw FileSystemException(file = this, other = target, reason = "Failed to create target directory") + throw FileSystemException(file = this, other = target, reason = "Failed to create target directory.") } else { target.parentFile?.mkdirs() @@ -242,14 +242,14 @@ public fun File.copyRecursively(target: File, { file, exception -> throw exception } ): Boolean { if (!exists()) { - return onError(this, NoSuchFileException(file = this, reason = "The source file doesn't exist")) != + return onError(this, NoSuchFileException(file = this, reason = "The source file doesn't exist.")) != OnErrorAction.TERMINATE } try { // We cannot break for loop from inside a lambda, so we have to use an exception here for (src in walkTopDown().onFail { f, e -> if (onError(f, e) == OnErrorAction.TERMINATE) throw TerminateException(f) }) { if (!src.exists()) { - if (onError(src, NoSuchFileException(file = src, reason = "The source file doesn't exist")) == + if (onError(src, NoSuchFileException(file = src, reason = "The source file doesn't exist.")) == OnErrorAction.TERMINATE) return false } else { @@ -266,7 +266,7 @@ public fun File.copyRecursively(target: File, if (stillExists) { if (onError(dstFile, FileAlreadyExistsException(file = src, other = dstFile, - reason = "The destination file already exists")) == OnErrorAction.TERMINATE) + reason = "The destination file already exists.")) == OnErrorAction.TERMINATE) return false continue @@ -277,7 +277,7 @@ public fun File.copyRecursively(target: File, dstFile.mkdirs() } else { if (src.copyTo(dstFile, overwrite).length() != src.length()) { - if (onError(src, IOException("src.length() != dst.length()")) == OnErrorAction.TERMINATE) + if (onError(src, IOException("Source file wasn't copied completely, length of destination file differs.")) == OnErrorAction.TERMINATE) return false } } diff --git a/libraries/stdlib/src/kotlin/ranges/Ranges.kt b/libraries/stdlib/src/kotlin/ranges/Ranges.kt index 082e37ec18c..a63f84bc52d 100644 --- a/libraries/stdlib/src/kotlin/ranges/Ranges.kt +++ b/libraries/stdlib/src/kotlin/ranges/Ranges.kt @@ -30,5 +30,5 @@ public operator fun > T.rangeTo(that: T): ClosedRange = Comp internal fun checkStepIsPositive(isPositive: Boolean, step: Number) { - if (!isPositive) throw IllegalArgumentException("Step must be positive, was: $step") + if (!isPositive) throw IllegalArgumentException("Step must be positive, was: $step.") } diff --git a/libraries/stdlib/src/kotlin/text/Indent.kt b/libraries/stdlib/src/kotlin/text/Indent.kt index 5fd94d29b28..2ec9f6fdb81 100644 --- a/libraries/stdlib/src/kotlin/text/Indent.kt +++ b/libraries/stdlib/src/kotlin/text/Indent.kt @@ -29,7 +29,7 @@ public fun String.trimMargin(marginPrefix: String = "|"): String = * Detects indent by [marginPrefix] as it does [trimMargin] and replace it with [newIndent]. */ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: String = "|"): String { - require(marginPrefix.isNotBlank()) { "marginPrefix must be non blank string." } + require(marginPrefix.isNotBlank()) { "marginPrefix must be non-blank string." } val lines = lines() return lines.reindent(length + newIndent.length * lines.size, getIndentFunction(newIndent), { line -> diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index a31035ceaa0..9db6d4577c6 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -380,7 +380,7 @@ public fun String.substringAfterLast(delimiter: String, missingDelimiterValue: S */ public fun CharSequence.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): CharSequence { if (endIndex < startIndex) - throw IndexOutOfBoundsException("End index ($endIndex) is less than start index ($startIndex)") + throw IndexOutOfBoundsException("End index ($endIndex) is less than start index ($startIndex).") val sb = StringBuilder() sb.append(this, 0, startIndex) sb.append(replacement) @@ -425,7 +425,7 @@ public inline fun String.replaceRange(range: IntRange, replacement: CharSequence */ public fun CharSequence.removeRange(startIndex: Int, endIndex: Int): CharSequence { if (endIndex < startIndex) - throw IndexOutOfBoundsException("End index ($endIndex) is less than start index ($startIndex)") + throw IndexOutOfBoundsException("End index ($endIndex) is less than start index ($startIndex).") if (endIndex == startIndex) return this.subSequence(0, length) @@ -1068,7 +1068,7 @@ private class DelimitedRangesSequence(private val input: CharSequence, private v * @param limit The maximum number of substrings to return. Zero by default means no limit is set. */ private fun CharSequence.rangesDelimitedBy(delimiters: CharArray, startIndex: Int = 0, ignoreCase: Boolean = false, limit: Int = 0): Sequence { - require(limit >= 0, { "Limit must be non-negative, but was $limit" }) + require(limit >= 0, { "Limit must be non-negative, but was $limit." }) return DelimitedRangesSequence(this, startIndex, limit, { startIndex -> findAnyOf(delimiters, startIndex, ignoreCase = ignoreCase, last = false)?.let { it.first to 1 } }) } @@ -1089,7 +1089,7 @@ private fun CharSequence.rangesDelimitedBy(delimiters: CharArray, startIndex: In * that matches this string at that position. */ private fun CharSequence.rangesDelimitedBy(delimiters: Array, startIndex: Int = 0, ignoreCase: Boolean = false, limit: Int = 0): Sequence { - require(limit >= 0, { "Limit must be non-negative, but was $limit" } ) + require(limit >= 0, { "Limit must be non-negative, but was $limit." } ) val delimitersList = delimiters.asList() return DelimitedRangesSequence(this, startIndex, limit, { startIndex -> findAnyOf(delimitersList, startIndex, ignoreCase = ignoreCase, last = false)?.let { it.first to it.second.length } }) diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index bbbb9bc783b..8c5d1b395ae 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -151,7 +151,7 @@ public inline fun String.Companion.format(locale: Locale, format: String, vararg */ public fun CharSequence.split(regex: Pattern, limit: Int = 0): List { - require(limit >= 0, { "Limit must be non-negative, but was $limit" } ) + require(limit >= 0, { "Limit must be non-negative, but was $limit." } ) return regex.split(this, if (limit == 0) -1 else limit).asList() } diff --git a/libraries/stdlib/src/kotlin/text/regex/Regex.kt b/libraries/stdlib/src/kotlin/text/regex/Regex.kt index 44e638affa6..101ee4dc673 100644 --- a/libraries/stdlib/src/kotlin/text/regex/Regex.kt +++ b/libraries/stdlib/src/kotlin/text/regex/Regex.kt @@ -187,7 +187,7 @@ internal constructor(private val nativePattern: Pattern) { * Zero by default means no limit is set. */ public fun split(input: CharSequence, limit: Int = 0): List { - require(limit >= 0, { "Limit must be non-negative, but was $limit" } ) + require(limit >= 0, { "Limit must be non-negative, but was $limit." } ) return nativePattern.split(input, if (limit == 0) -1 else limit).asList() } diff --git a/libraries/stdlib/src/kotlin/util/Preconditions.kt b/libraries/stdlib/src/kotlin/util/Preconditions.kt index a50e10e8643..9861d396fb6 100644 --- a/libraries/stdlib/src/kotlin/util/Preconditions.kt +++ b/libraries/stdlib/src/kotlin/util/Preconditions.kt @@ -9,7 +9,7 @@ import java.lang.IllegalStateException * Throws an [IllegalArgumentException] if the [value] is false. */ @kotlin.internal.InlineOnly -public inline fun require(value: Boolean): Unit = require(value) { "Failed requirement" } +public inline fun require(value: Boolean): Unit = require(value) { "Failed requirement." } /** * Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is false. @@ -28,7 +28,7 @@ public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit { * Throws an [IllegalArgumentException] if the [value] is null. Otherwise returns the not null value. */ @kotlin.internal.InlineOnly -public inline fun requireNotNull(value: T?): T = requireNotNull(value) { "Required value was null" } +public inline fun requireNotNull(value: T?): T = requireNotNull(value) { "Required value was null." } /** * Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is null. Otherwise @@ -50,7 +50,7 @@ public inline fun requireNotNull(value: T?, lazyMessage: () -> Any): T { * Throws an [IllegalStateException] if the [value] is false. */ @kotlin.internal.InlineOnly -public inline fun check(value: Boolean): Unit = check(value) { "Check failed" } +public inline fun check(value: Boolean): Unit = check(value) { "Check failed." } /** * Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is false. @@ -70,7 +70,7 @@ public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit { * returns the not null value. */ @kotlin.internal.InlineOnly -public inline fun checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null" } +public inline fun checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null." } /** * Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is null. Otherwise diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index c93af808dd9..7a5bbd805d9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -480,10 +480,10 @@ fun aggregates(): List { typeParam("S") typeParam("T: S") returns("S") - body { + body { f -> """ val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") var index = 1 var accumulator: S = iterator.next() @@ -595,10 +595,10 @@ fun aggregates(): List { typeParam("S") typeParam("T: S") returns("S") - body { + body { f -> """ val iterator = this.iterator() - if (!iterator.hasNext()) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") var accumulator: S = iterator.next() while (iterator.hasNext()) { @@ -627,10 +627,10 @@ fun aggregates(): List { only(CharSequences, ArraysOfPrimitives) doc { f -> "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." } returns("T") - body { + body { f -> """ var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") var accumulator = get(index--) while (index >= 0) { @@ -650,10 +650,10 @@ fun aggregates(): List { typeParam("S") typeParam("T: S") returns("S") - body { + body { f -> """ var index = lastIndex - if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced.") + if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") var accumulator: S = get(index--) while (index >= 0) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 215b1e69506..2884fe1cce5 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -2,7 +2,6 @@ package templates import templates.Family.* - fun elements(): List { val templates = arrayListOf() @@ -14,7 +13,7 @@ fun elements(): List { doc { f -> "Returns `true` if [element] is found in the ${f.collection}." } typeParam("@kotlin.internal.OnlyInputTypes T") returns("Boolean") - body(Iterables) { + body(Iterables) { f -> """ if (this is Collection) return contains(element) @@ -327,12 +326,7 @@ fun elements(): List { body { """ when (this) { - is List -> { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - else - return this[0] - } + is List -> return this.first() else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -342,10 +336,10 @@ fun elements(): List { } """ } - body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f -> """ if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.") return this[0] """ } @@ -401,10 +395,10 @@ fun elements(): List { doc { f -> """Returns the first ${f.element} matching the given [predicate]. @throws [NoSuchElementException] if no such ${f.element} is found.""" } returns("T") - body { + body { f -> """ for (element in this) if (predicate(element)) return element - throw NoSuchElementException("No element matching predicate was found.") + throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.") """ } } @@ -438,12 +432,7 @@ fun elements(): List { body { """ when (this) { - is List -> { - if (isEmpty()) - throw NoSuchElementException("Collection is empty.") - else - return this[this.lastIndex] - } + is List -> return this.last() else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -467,10 +456,10 @@ fun elements(): List { return last """ } - body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f -> """ if (isEmpty()) - throw NoSuchElementException("Collection is empty.") + throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.") return this[lastIndex] """ } @@ -541,18 +530,18 @@ fun elements(): List { found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + if (!found) throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.") return last as T """ } - body(CharSequences, ArraysOfPrimitives, ArraysOfObjects, Lists) { + body(CharSequences, ArraysOfPrimitives, ArraysOfObjects, Lists) { f -> """ for (index in this.indices.reversed()) { val element = this[index] if (predicate(element)) return element } - throw NoSuchElementException("Collection doesn't contain any element matching the predicate.") + throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.") """ } } @@ -605,11 +594,7 @@ fun elements(): List { body { """ when (this) { - is List -> return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } + is List -> return this.single() else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -633,21 +618,12 @@ fun elements(): List { return single """ } - body(CharSequences) { + body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f -> """ - return when (length) { - 0 -> throw NoSuchElementException("Collection is empty.") + return when (${f.code.size}) { + 0 -> throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.") 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") - } - """ - } - body(Lists, ArraysOfObjects, ArraysOfPrimitives) { - """ - return when (size) { - 0 -> throw NoSuchElementException("Collection is empty.") - 1 -> this[0] - else -> throw IllegalArgumentException("Collection has more than one element.") + else -> throw IllegalArgumentException("${f.doc.collection.capitalize()} has more than one element.") } """ } @@ -700,18 +676,18 @@ fun elements(): List { include(CharSequences) doc { f -> "Returns the single ${f.element} matching the given [predicate], or throws exception if there is no or more than one matching ${f.element}." } returns("T") - body { + body { f -> """ var single: T? = null var found = false for (element in this) { if (predicate(element)) { - if (found) throw IllegalArgumentException("Collection contains more than one matching element.") + if (found) throw IllegalArgumentException("${f.doc.collection.capitalize()} contains more than one matching element.") single = element found = true } } - if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate.") + if (!found) throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.") return single as T """ } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index d69f70867b8..626a74c735d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -23,7 +23,7 @@ fun filtering(): List { returns("List") body { """ - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() val list: ArrayList if (this is Collection<*>) { @@ -54,7 +54,7 @@ fun filtering(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return when { n == 0 -> this this is DropTakeSequence -> this.drop(n) @@ -68,14 +68,14 @@ fun filtering(): List { returns(Strings, CharSequences) { "SELF" } body(Strings, CharSequences) { f -> """ - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return ${subsequence(f, "n.coerceAtMost(length)")} """ } body(ArraysOfObjects, ArraysOfPrimitives) { """ - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return toList() if (n >= size) @@ -96,7 +96,7 @@ fun filtering(): List { returns("List") body { """ - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() if (this is Collection && n >= size) return toList() var count = 0 @@ -115,7 +115,7 @@ fun filtering(): List { returns(Strings, CharSequences) { "SELF" } body(Strings, CharSequences) { f -> """ - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } return ${subsequence(f, "0", "n.coerceAtMost(length)")} """ } @@ -124,7 +124,7 @@ fun filtering(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } return when { n == 0 -> emptySequence() this is DropTakeSequence -> this.take(n) @@ -156,10 +156,10 @@ fun filtering(): List { doc { "Returns a list containing all elements except last [n] elements." } returns("List") - body { + body { f -> """ - require(n >= 0, { "Requested element count $n is less than zero." }) - return take((size - n).coerceAtLeast(0)) + require(n >= 0) { "Requested ${f.doc.element} count $n is less than zero." } + return take((${f.code.size} - n).coerceAtLeast(0)) """ } @@ -167,13 +167,6 @@ fun filtering(): List { doc(CharSequences) { "Returns a subsequence of this char sequence with the last [n] characters removed." } returns(Strings, CharSequences) { "SELF" } - body(Strings, CharSequences) { - """ - require(n >= 0, { "Requested character count $n is less than zero." }) - return take((length - n).coerceAtLeast(0)) - """ - } - } templates add f("takeLast(n: Int)") { @@ -187,7 +180,7 @@ fun filtering(): List { returns(Strings, CharSequences) { "SELF" } body(Strings, CharSequences) { f -> """ - require(n >= 0, { "Requested character count $n is less than zero." }) + require(n >= 0) { "Requested character count $n is less than zero." } val length = length return ${subsequence(f, "length - n.coerceAtMost(length)")} """ @@ -195,7 +188,7 @@ fun filtering(): List { body(Lists, ArraysOfObjects, ArraysOfPrimitives) { """ - require(n >= 0, { "Requested element count $n is less than zero." }) + require(n >= 0) { "Requested element count $n is less than zero." } if (n == 0) return emptyList() val size = size if (n >= size) return toList()