Review and correct exception messages.

This commit is contained in:
Ilya Gorbunov
2016-02-13 00:17:38 +03:00
parent 00e30e417f
commit 19fa2f51ea
19 changed files with 220 additions and 265 deletions
+108 -108
View File
@@ -802,7 +802,7 @@ public inline fun CharArray.findLast(predicate: (Char) -> Boolean): Char? {
*/ */
public fun <T> Array<out T>.first(): T { public fun <T> Array<out T>.first(): T {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -812,7 +812,7 @@ public fun <T> Array<out T>.first(): T {
*/ */
public fun ByteArray.first(): Byte { public fun ByteArray.first(): Byte {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -822,7 +822,7 @@ public fun ByteArray.first(): Byte {
*/ */
public fun ShortArray.first(): Short { public fun ShortArray.first(): Short {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -832,7 +832,7 @@ public fun ShortArray.first(): Short {
*/ */
public fun IntArray.first(): Int { public fun IntArray.first(): Int {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -842,7 +842,7 @@ public fun IntArray.first(): Int {
*/ */
public fun LongArray.first(): Long { public fun LongArray.first(): Long {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -852,7 +852,7 @@ public fun LongArray.first(): Long {
*/ */
public fun FloatArray.first(): Float { public fun FloatArray.first(): Float {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -862,7 +862,7 @@ public fun FloatArray.first(): Float {
*/ */
public fun DoubleArray.first(): Double { public fun DoubleArray.first(): Double {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -872,7 +872,7 @@ public fun DoubleArray.first(): Double {
*/ */
public fun BooleanArray.first(): Boolean { public fun BooleanArray.first(): Boolean {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -882,7 +882,7 @@ public fun BooleanArray.first(): Boolean {
*/ */
public fun CharArray.first(): Char { public fun CharArray.first(): Char {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[0] return this[0]
} }
@@ -892,7 +892,7 @@ public fun CharArray.first(): Char {
*/ */
public inline fun <T> Array<out T>.first(predicate: (T) -> Boolean): T { public inline fun <T> Array<out T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element 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 <T> Array<out T>.first(predicate: (T) -> Boolean): T {
*/ */
public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte {
for (element in this) if (predicate(element)) return element 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 { public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short {
for (element in this) if (predicate(element)) return element 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 { public inline fun IntArray.first(predicate: (Int) -> Boolean): Int {
for (element in this) if (predicate(element)) return element 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 { public inline fun LongArray.first(predicate: (Long) -> Boolean): Long {
for (element in this) if (predicate(element)) return element 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 { public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float {
for (element in this) if (predicate(element)) return element 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 { public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double {
for (element in this) if (predicate(element)) return element 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 { public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean {
for (element in this) if (predicate(element)) return element 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 { public inline fun CharArray.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element 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 <T> Array<out T>.last(): T { public fun <T> Array<out T>.last(): T {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1585,7 +1585,7 @@ public fun <T> Array<out T>.last(): T {
*/ */
public fun ByteArray.last(): Byte { public fun ByteArray.last(): Byte {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1595,7 +1595,7 @@ public fun ByteArray.last(): Byte {
*/ */
public fun ShortArray.last(): Short { public fun ShortArray.last(): Short {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1605,7 +1605,7 @@ public fun ShortArray.last(): Short {
*/ */
public fun IntArray.last(): Int { public fun IntArray.last(): Int {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1615,7 +1615,7 @@ public fun IntArray.last(): Int {
*/ */
public fun LongArray.last(): Long { public fun LongArray.last(): Long {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1625,7 +1625,7 @@ public fun LongArray.last(): Long {
*/ */
public fun FloatArray.last(): Float { public fun FloatArray.last(): Float {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1635,7 +1635,7 @@ public fun FloatArray.last(): Float {
*/ */
public fun DoubleArray.last(): Double { public fun DoubleArray.last(): Double {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1645,7 +1645,7 @@ public fun DoubleArray.last(): Double {
*/ */
public fun BooleanArray.last(): Boolean { public fun BooleanArray.last(): Boolean {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1655,7 +1655,7 @@ public fun BooleanArray.last(): Boolean {
*/ */
public fun CharArray.last(): Char { public fun CharArray.last(): Char {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Array is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -1668,7 +1668,7 @@ public inline fun <T> Array<out T>.last(predicate: (T) -> Boolean): T {
val element = this[index] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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] val element = this[index]
if (predicate(element)) return element 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 <T> Array<out T>.single(): T { public fun <T> Array<out T>.single(): T {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 <T> Array<out T>.single(): T {
*/ */
public fun ByteArray.single(): Byte { public fun ByteArray.single(): Byte {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun ShortArray.single(): Short {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun IntArray.single(): Int {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun LongArray.single(): Long {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun FloatArray.single(): Float {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun DoubleArray.single(): Double {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun BooleanArray.single(): Boolean {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 { public fun CharArray.single(): Char {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Array is empty.")
1 -> this[0] 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 <T> Array<out T>.single(predicate: (T) -> Boolean): T {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as T
} }
@@ -2169,12 +2169,12 @@ public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Byte
} }
@@ -2186,12 +2186,12 @@ public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Short
} }
@@ -2203,12 +2203,12 @@ public inline fun IntArray.single(predicate: (Int) -> Boolean): Int {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Int
} }
@@ -2220,12 +2220,12 @@ public inline fun LongArray.single(predicate: (Long) -> Boolean): Long {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Long
} }
@@ -2237,12 +2237,12 @@ public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Float
} }
@@ -2254,12 +2254,12 @@ public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Double
} }
@@ -2271,12 +2271,12 @@ public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as Boolean
} }
@@ -2288,12 +2288,12 @@ public inline fun CharArray.single(predicate: (Char) -> Boolean): Char {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 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. * Returns a list containing all elements except first [n] elements.
*/ */
public fun <T> Array<out T>.drop(n: Int): List<T> { public fun <T> Array<out T>.drop(n: Int): List<T> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2533,7 +2533,7 @@ public fun <T> Array<out T>.drop(n: Int): List<T> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun ByteArray.drop(n: Int): List<Byte> { public fun ByteArray.drop(n: Int): List<Byte> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2549,7 +2549,7 @@ public fun ByteArray.drop(n: Int): List<Byte> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun ShortArray.drop(n: Int): List<Short> { public fun ShortArray.drop(n: Int): List<Short> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2565,7 +2565,7 @@ public fun ShortArray.drop(n: Int): List<Short> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun IntArray.drop(n: Int): List<Int> { public fun IntArray.drop(n: Int): List<Int> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2581,7 +2581,7 @@ public fun IntArray.drop(n: Int): List<Int> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun LongArray.drop(n: Int): List<Long> { public fun LongArray.drop(n: Int): List<Long> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2597,7 +2597,7 @@ public fun LongArray.drop(n: Int): List<Long> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun FloatArray.drop(n: Int): List<Float> { public fun FloatArray.drop(n: Int): List<Float> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2613,7 +2613,7 @@ public fun FloatArray.drop(n: Int): List<Float> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun DoubleArray.drop(n: Int): List<Double> { public fun DoubleArray.drop(n: Int): List<Double> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2629,7 +2629,7 @@ public fun DoubleArray.drop(n: Int): List<Double> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun BooleanArray.drop(n: Int): List<Boolean> { public fun BooleanArray.drop(n: Int): List<Boolean> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2645,7 +2645,7 @@ public fun BooleanArray.drop(n: Int): List<Boolean> {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun CharArray.drop(n: Int): List<Char> { public fun CharArray.drop(n: Int): List<Char> {
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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -2661,7 +2661,7 @@ public fun CharArray.drop(n: Int): List<Char> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun <T> Array<out T>.dropLast(n: Int): List<T> { public fun <T> Array<out T>.dropLast(n: Int): List<T> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2669,7 +2669,7 @@ public fun <T> Array<out T>.dropLast(n: Int): List<T> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun ByteArray.dropLast(n: Int): List<Byte> { public fun ByteArray.dropLast(n: Int): List<Byte> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2677,7 +2677,7 @@ public fun ByteArray.dropLast(n: Int): List<Byte> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun ShortArray.dropLast(n: Int): List<Short> { public fun ShortArray.dropLast(n: Int): List<Short> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2685,7 +2685,7 @@ public fun ShortArray.dropLast(n: Int): List<Short> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun IntArray.dropLast(n: Int): List<Int> { public fun IntArray.dropLast(n: Int): List<Int> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2693,7 +2693,7 @@ public fun IntArray.dropLast(n: Int): List<Int> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun LongArray.dropLast(n: Int): List<Long> { public fun LongArray.dropLast(n: Int): List<Long> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2701,7 +2701,7 @@ public fun LongArray.dropLast(n: Int): List<Long> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun FloatArray.dropLast(n: Int): List<Float> { public fun FloatArray.dropLast(n: Int): List<Float> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2709,7 +2709,7 @@ public fun FloatArray.dropLast(n: Int): List<Float> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun DoubleArray.dropLast(n: Int): List<Double> { public fun DoubleArray.dropLast(n: Int): List<Double> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2717,7 +2717,7 @@ public fun DoubleArray.dropLast(n: Int): List<Double> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun BooleanArray.dropLast(n: Int): List<Boolean> { public fun BooleanArray.dropLast(n: Int): List<Boolean> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -2725,7 +2725,7 @@ public fun BooleanArray.dropLast(n: Int): List<Boolean> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun CharArray.dropLast(n: Int): List<Char> { public fun CharArray.dropLast(n: Int): List<Char> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -3981,7 +3981,7 @@ public fun CharArray.take(n: Int): List<Char> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun <T> Array<out T>.takeLast(n: Int): List<T> { public fun <T> Array<out T>.takeLast(n: Int): List<T> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -3995,7 +3995,7 @@ public fun <T> Array<out T>.takeLast(n: Int): List<T> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun ByteArray.takeLast(n: Int): List<Byte> { public fun ByteArray.takeLast(n: Int): List<Byte> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4009,7 +4009,7 @@ public fun ByteArray.takeLast(n: Int): List<Byte> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun ShortArray.takeLast(n: Int): List<Short> { public fun ShortArray.takeLast(n: Int): List<Short> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4023,7 +4023,7 @@ public fun ShortArray.takeLast(n: Int): List<Short> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun IntArray.takeLast(n: Int): List<Int> { public fun IntArray.takeLast(n: Int): List<Int> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4037,7 +4037,7 @@ public fun IntArray.takeLast(n: Int): List<Int> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun LongArray.takeLast(n: Int): List<Long> { public fun LongArray.takeLast(n: Int): List<Long> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4051,7 +4051,7 @@ public fun LongArray.takeLast(n: Int): List<Long> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun FloatArray.takeLast(n: Int): List<Float> { public fun FloatArray.takeLast(n: Int): List<Float> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4065,7 +4065,7 @@ public fun FloatArray.takeLast(n: Int): List<Float> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun DoubleArray.takeLast(n: Int): List<Double> { public fun DoubleArray.takeLast(n: Int): List<Double> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4079,7 +4079,7 @@ public fun DoubleArray.takeLast(n: Int): List<Double> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun BooleanArray.takeLast(n: Int): List<Boolean> { public fun BooleanArray.takeLast(n: Int): List<Boolean> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -4093,7 +4093,7 @@ public fun BooleanArray.takeLast(n: Int): List<Boolean> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun CharArray.takeLast(n: Int): List<Char> { public fun CharArray.takeLast(n: Int): List<Char> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -10351,7 +10351,7 @@ public inline fun CharArray.reduceIndexed(operation: (Int, Char, Char) -> Char):
*/ */
public inline fun <S, T: S> Array<out T>.reduceRight(operation: (T, S) -> S): S { public inline fun <S, T: S> Array<out T>.reduceRight(operation: (T, S) -> S): S {
var index = lastIndex 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--) var accumulator: S = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) accumulator = operation(get(index--), accumulator)
@@ -10364,7 +10364,7 @@ public inline fun <S, T: S> Array<out T>.reduceRight(operation: (T, S) -> S): S
*/ */
public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Short {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Float {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double): Double {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) 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 { public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) accumulator = operation(get(index--), accumulator)
+18 -32
View File
@@ -161,12 +161,7 @@ public inline fun <T> List<T>.findLast(predicate: (T) -> Boolean): T? {
*/ */
public fun <T> Iterable<T>.first(): T { public fun <T> Iterable<T>.first(): T {
when (this) { when (this) {
is List -> { is List -> return this.first()
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
else
return this[0]
}
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -182,7 +177,7 @@ public fun <T> Iterable<T>.first(): T {
*/ */
public fun <T> List<T>.first(): T { public fun <T> List<T>.first(): T {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("List is empty.")
return this[0] return this[0]
} }
@@ -192,7 +187,7 @@ public fun <T> List<T>.first(): T {
*/ */
public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T { public inline fun <T> Iterable<T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element 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 <T> List<T>.indexOfLast(predicate: (T) -> Boolean): Int {
*/ */
public fun <T> Iterable<T>.last(): T { public fun <T> Iterable<T>.last(): T {
when (this) { when (this) {
is List -> { is List -> return this.last()
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
else
return this[this.lastIndex]
}
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -347,7 +337,7 @@ public fun <T> Iterable<T>.last(): T {
*/ */
public fun <T> List<T>.last(): T { public fun <T> List<T>.last(): T {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("List is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -366,7 +356,7 @@ public inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T {
found = true 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 return last as T
} }
@@ -379,7 +369,7 @@ public inline fun <T> List<T>.last(predicate: (T) -> Boolean): T {
val element = this[index] val element = this[index]
if (predicate(element)) return element 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 <T> List<T>.lastOrNull(predicate: (T) -> Boolean): T? {
*/ */
public fun <T> Iterable<T>.single(): T { public fun <T> Iterable<T>.single(): T {
when (this) { when (this) {
is List -> return when (size) { is List -> return this.single()
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
}
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -482,9 +468,9 @@ public fun <T> Iterable<T>.single(): T {
*/ */
public fun <T> List<T>.single(): T { public fun <T> List<T>.single(): T {
return when (size) { return when (size) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("List is empty.")
1 -> this[0] 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 <T> Iterable<T>.single(predicate: (T) -> Boolean): T {
found = true 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 return single as T
} }
@@ -551,7 +537,7 @@ public inline fun <T> Iterable<T>.singleOrNull(predicate: (T) -> Boolean): T? {
* Returns a list containing all elements except first [n] elements. * Returns a list containing all elements except first [n] elements.
*/ */
public fun <T> Iterable<T>.drop(n: Int): List<T> { public fun <T> Iterable<T>.drop(n: Int): List<T> {
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 == 0) return toList()
val list: ArrayList<T> val list: ArrayList<T>
if (this is Collection<*>) { if (this is Collection<*>) {
@@ -580,7 +566,7 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
* Returns a list containing all elements except last [n] elements. * Returns a list containing all elements except last [n] elements.
*/ */
public fun <T> List<T>.dropLast(n: Int): List<T> { public fun <T> List<T>.dropLast(n: Int): List<T> {
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)) return take((size - n).coerceAtLeast(0))
} }
@@ -703,7 +689,7 @@ public fun <T> List<T>.slice(indices: Iterable<Int>): List<T> {
* Returns a list containing first [n] elements. * Returns a list containing first [n] elements.
*/ */
public fun <T> Iterable<T>.take(n: Int): List<T> { public fun <T> Iterable<T>.take(n: Int): List<T> {
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 (n == 0) return emptyList()
if (this is Collection<T> && n >= size) return toList() if (this is Collection<T> && n >= size) return toList()
var count = 0 var count = 0
@@ -720,7 +706,7 @@ public fun <T> Iterable<T>.take(n: Int): List<T> {
* Returns a list containing last [n] elements. * Returns a list containing last [n] elements.
*/ */
public fun <T> List<T>.takeLast(n: Int): List<T> { public fun <T> List<T>.takeLast(n: Int): List<T> {
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 (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()
@@ -1509,7 +1495,7 @@ public inline fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean {
*/ */
public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S { public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator() 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() var accumulator: S = iterator.next()
while (iterator.hasNext()) { while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next()) accumulator = operation(accumulator, iterator.next())
@@ -1525,7 +1511,7 @@ public inline fun <S, T: S> Iterable<T>.reduce(operation: (S, T) -> S): S {
*/ */
public inline fun <S, T: S> Iterable<T>.reduceIndexed(operation: (Int, S, T) -> S): S { public inline fun <S, T: S> Iterable<T>.reduceIndexed(operation: (Int, S, T) -> S): S {
val iterator = this.iterator() 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 index = 1
var accumulator: S = iterator.next() var accumulator: S = iterator.next()
while (iterator.hasNext()) { while (iterator.hasNext()) {
@@ -1539,7 +1525,7 @@ public inline fun <S, T: S> Iterable<T>.reduceIndexed(operation: (Int, S, T) ->
*/ */
public inline fun <S, T: S> List<T>.reduceRight(operation: (T, S) -> S): S { public inline fun <S, T: S> List<T>.reduceRight(operation: (T, S) -> S): S {
var index = lastIndex 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--) var accumulator: S = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) accumulator = operation(get(index--), accumulator)
+8 -8
View File
@@ -92,7 +92,7 @@ public fun <T> Sequence<T>.first(): T {
*/ */
public inline fun <T> Sequence<T>.first(predicate: (T) -> Boolean): T { public inline fun <T> Sequence<T>.first(predicate: (T) -> Boolean): T {
for (element in this) if (predicate(element)) return element 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 <T> Sequence<T>.last(predicate: (T) -> Boolean): T {
found = true 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 return last as T
} }
@@ -245,12 +245,12 @@ public inline fun <T> Sequence<T>.single(predicate: (T) -> Boolean): T {
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as T
} }
@@ -288,7 +288,7 @@ public inline fun <T> Sequence<T>.singleOrNull(predicate: (T) -> Boolean): T? {
* Returns a sequence containing all elements except first [n] elements. * Returns a sequence containing all elements except first [n] elements.
*/ */
public fun <T> Sequence<T>.drop(n: Int): Sequence<T> { public fun <T> Sequence<T>.drop(n: Int): Sequence<T> {
require(n >= 0, { "Requested element count $n is less than zero." }) require(n >= 0) { "Requested element count $n is less than zero." }
return when { return when {
n == 0 -> this n == 0 -> this
this is DropTakeSequence -> this.drop(n) this is DropTakeSequence -> this.drop(n)
@@ -375,7 +375,7 @@ public inline fun <T, C : MutableCollection<in T>> Sequence<T>.filterTo(destinat
* Returns a sequence containing first [n] elements. * Returns a sequence containing first [n] elements.
*/ */
public fun <T> Sequence<T>.take(n: Int): Sequence<T> { public fun <T> Sequence<T>.take(n: Int): Sequence<T> {
require(n >= 0, { "Requested element count $n is less than zero." }) require(n >= 0) { "Requested element count $n is less than zero." }
return when { return when {
n == 0 -> emptySequence() n == 0 -> emptySequence()
this is DropTakeSequence -> this.take(n) this is DropTakeSequence -> this.take(n)
@@ -931,7 +931,7 @@ public inline fun <T> Sequence<T>.none(predicate: (T) -> Boolean): Boolean {
*/ */
public inline fun <S, T: S> Sequence<T>.reduce(operation: (S, T) -> S): S { public inline fun <S, T: S> Sequence<T>.reduce(operation: (S, T) -> S): S {
val iterator = this.iterator() 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() var accumulator: S = iterator.next()
while (iterator.hasNext()) { while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next()) accumulator = operation(accumulator, iterator.next())
@@ -947,7 +947,7 @@ public inline fun <S, T: S> Sequence<T>.reduce(operation: (S, T) -> S): S {
*/ */
public inline fun <S, T: S> Sequence<T>.reduceIndexed(operation: (Int, S, T) -> S): S { public inline fun <S, T: S> Sequence<T>.reduceIndexed(operation: (Int, S, T) -> S): S {
val iterator = this.iterator() 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 index = 1
var accumulator: S = iterator.next() var accumulator: S = iterator.next()
while (iterator.hasNext()) { while (iterator.hasNext()) {
+17 -17
View File
@@ -59,7 +59,7 @@ public inline fun CharSequence.findLast(predicate: (Char) -> Boolean): Char? {
*/ */
public fun CharSequence.first(): Char { public fun CharSequence.first(): Char {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Char sequence is empty.")
return this[0] return this[0]
} }
@@ -69,7 +69,7 @@ public fun CharSequence.first(): Char {
*/ */
public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char { public inline fun CharSequence.first(predicate: (Char) -> Boolean): Char {
for (element in this) if (predicate(element)) return element 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 { public fun CharSequence.last(): Char {
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("Char sequence is empty.")
return this[lastIndex] return this[lastIndex]
} }
@@ -145,7 +145,7 @@ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
val element = this[index] val element = this[index]
if (predicate(element)) return element 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 { public fun CharSequence.single(): Char {
return when (length) { return when (length) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("Char sequence is empty.")
1 -> this[0] 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 var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 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. * Returns a subsequence of this char sequence with the first [n] characters removed.
*/ */
public fun CharSequence.drop(n: Int): CharSequence { 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) 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. * Returns a string with the first [n] characters removed.
*/ */
public fun String.drop(n: Int): String { 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)) 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. * Returns a subsequence of this char sequence with the last [n] characters removed.
*/ */
public fun CharSequence.dropLast(n: Int): CharSequence { 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)) 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. * Returns a string with the last [n] characters removed.
*/ */
public fun String.dropLast(n: Int): String { 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)) return take((length - n).coerceAtLeast(0))
} }
@@ -408,7 +408,7 @@ public inline fun String.slice(indices: Iterable<Int>): 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. * 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 { 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)) 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. * 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 { 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)) 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. * 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 { 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 val length = length
return subSequence(length - n.coerceAtMost(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. * 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 { 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 val length = length
return substring(length - n.coerceAtMost(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 { public inline fun CharSequence.reduceRight(operation: (Char, Char) -> Char): Char {
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
accumulator = operation(get(index--), accumulator) accumulator = operation(get(index--), accumulator)
@@ -26,7 +26,7 @@ internal object EmptyList : List<Nothing>, Serializable {
override fun contains(element: Nothing): Boolean = false override fun contains(element: Nothing): Boolean = false
override fun containsAll(elements: Collection<Nothing>): Boolean = elements.isEmpty() override fun containsAll(elements: Collection<Nothing>): 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 indexOf(element: Nothing): Int = -1
override fun lastIndexOf(element: Nothing): Int = -1 override fun lastIndexOf(element: Nothing): Int = -1
@@ -274,7 +274,7 @@ public fun <T> List<T>.binarySearch(fromIndex: Int = 0, toIndex: Int = size, com
*/ */
private fun rangeCheck(size: Int, fromIndex: Int, toIndex: Int) { private fun rangeCheck(size: Int, fromIndex: Int, toIndex: Int) {
when { 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.") fromIndex < 0 -> throw IndexOutOfBoundsException("fromIndex ($fromIndex) is less than zero.")
toIndex > size -> throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).") toIndex > size -> throw IndexOutOfBoundsException("toIndex ($toIndex) is greater than size ($size).")
} }
@@ -24,8 +24,8 @@ private open class ReversedListReadOnly<T>(protected open val delegate: List<T>)
override val size: Int get() = delegate.size override val size: Int get() = delegate.size
override fun get(index: Int): T = delegate[index.flipIndex()] 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.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.flipIndexForward(): Int = if (this in 0..size) size - this else throw IndexOutOfBoundsException("index $this should be in range [${0..size}].")
} }
private class ReversedList<T>(protected override val delegate: MutableList<T>) : ReversedListReadOnly<T>(delegate) { private class ReversedList<T>(protected override val delegate: MutableList<T>) : ReversedListReadOnly<T>(delegate) {
@@ -316,7 +316,7 @@ internal class TakeSequence<T> (
) : Sequence<T>, DropTakeSequence<T> { ) : Sequence<T>, DropTakeSequence<T> {
init { 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<T> = if (n >= count) emptySequence() else SubSequence(sequence, n, count) override fun drop(n: Int): Sequence<T> = if (n >= count) emptySequence() else SubSequence(sequence, n, count)
@@ -394,7 +394,7 @@ internal class DropSequence<T> (
private val count: Int private val count: Int
) : Sequence<T>, DropTakeSequence<T> { ) : Sequence<T>, DropTakeSequence<T> {
init { 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<T> = DropSequence(sequence, count + n) override fun drop(n: Int): Sequence<T> = DropSequence(sequence, count + n)
+1 -1
View File
@@ -32,7 +32,7 @@ public operator fun BufferedInputStream.iterator(): ByteIterator =
public override fun nextByte(): Byte { public override fun nextByte(): Byte {
prepareNext() prepareNext()
if (finished) if (finished)
throw NoSuchElementException("Input stream is over") throw NoSuchElementException("Input stream is over.")
val res = nextByte.toByte() val res = nextByte.toByte()
nextPrepared = false nextPrepared = false
return res return res
@@ -252,7 +252,7 @@ public class FileTreeWalk private constructor(
*/ */
public fun maxDepth(depth: Int): FileTreeWalk { public fun maxDepth(depth: Int): FileTreeWalk {
if (depth <= 0) 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) return FileTreeWalk(start, direction, onEnter, onLeave, onFail, depth)
} }
} }
@@ -24,7 +24,7 @@ public fun createTempDir(prefix: String = "tmp", suffix: String? = null, directo
if (dir.mkdir()) { if (dir.mkdir()) {
return dir return dir
} else { } 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. * @throws IllegalArgumentException if this and base paths have different roots.
*/ */
public fun File.toRelativeString(base: File): String 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. * 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 { public fun File.copyTo(target: File, overwrite: Boolean = false, bufferSize: Int = DEFAULT_BUFFER_SIZE): File {
if (!this.exists()) { 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()) { if (target.exists()) {
@@ -182,13 +182,13 @@ public fun File.copyTo(target: File, overwrite: Boolean = false, bufferSize: Int
if (stillExists) { if (stillExists) {
throw FileAlreadyExistsException(file = this, throw FileAlreadyExistsException(file = this,
other = target, other = target,
reason = "The destination file already exists") reason = "The destination file already exists.")
} }
} }
if (this.isDirectory) { if (this.isDirectory) {
if (!target.mkdirs()) 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 { } else {
target.parentFile?.mkdirs() target.parentFile?.mkdirs()
@@ -242,14 +242,14 @@ public fun File.copyRecursively(target: File,
{ file, exception -> throw exception } { file, exception -> throw exception }
): Boolean { ): Boolean {
if (!exists()) { 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 OnErrorAction.TERMINATE
} }
try { try {
// We cannot break for loop from inside a lambda, so we have to use an exception here // 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) }) { for (src in walkTopDown().onFail { f, e -> if (onError(f, e) == OnErrorAction.TERMINATE) throw TerminateException(f) }) {
if (!src.exists()) { 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) OnErrorAction.TERMINATE)
return false return false
} else { } else {
@@ -266,7 +266,7 @@ public fun File.copyRecursively(target: File,
if (stillExists) { if (stillExists) {
if (onError(dstFile, FileAlreadyExistsException(file = src, if (onError(dstFile, FileAlreadyExistsException(file = src,
other = dstFile, other = dstFile,
reason = "The destination file already exists")) == OnErrorAction.TERMINATE) reason = "The destination file already exists.")) == OnErrorAction.TERMINATE)
return false return false
continue continue
@@ -277,7 +277,7 @@ public fun File.copyRecursively(target: File,
dstFile.mkdirs() dstFile.mkdirs()
} else { } else {
if (src.copyTo(dstFile, overwrite).length() != src.length()) { 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 return false
} }
} }
+1 -1
View File
@@ -30,5 +30,5 @@ public operator fun <T: Comparable<T>> T.rangeTo(that: T): ClosedRange<T> = Comp
internal fun checkStepIsPositive(isPositive: Boolean, step: Number) { 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.")
} }
+1 -1
View File
@@ -29,7 +29,7 @@ public fun String.trimMargin(marginPrefix: String = "|"): String =
* Detects indent by [marginPrefix] as it does [trimMargin] and replace it with [newIndent]. * Detects indent by [marginPrefix] as it does [trimMargin] and replace it with [newIndent].
*/ */
public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: String = "|"): String { 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() val lines = lines()
return lines.reindent(length + newIndent.length * lines.size, getIndentFunction(newIndent), { line -> return lines.reindent(length + newIndent.length * lines.size, getIndentFunction(newIndent), { line ->
+4 -4
View File
@@ -380,7 +380,7 @@ public fun String.substringAfterLast(delimiter: String, missingDelimiterValue: S
*/ */
public fun CharSequence.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): CharSequence { public fun CharSequence.replaceRange(startIndex: Int, endIndex: Int, replacement: CharSequence): CharSequence {
if (endIndex < startIndex) 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() val sb = StringBuilder()
sb.append(this, 0, startIndex) sb.append(this, 0, startIndex)
sb.append(replacement) 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 { public fun CharSequence.removeRange(startIndex: Int, endIndex: Int): CharSequence {
if (endIndex < startIndex) 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) if (endIndex == startIndex)
return this.subSequence(0, length) 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. * @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<IntRange> { private fun CharSequence.rangesDelimitedBy(delimiters: CharArray, startIndex: Int = 0, ignoreCase: Boolean = false, limit: Int = 0): Sequence<IntRange> {
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 } }) 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. * that matches this string at that position.
*/ */
private fun CharSequence.rangesDelimitedBy(delimiters: Array<out String>, startIndex: Int = 0, ignoreCase: Boolean = false, limit: Int = 0): Sequence<IntRange> { private fun CharSequence.rangesDelimitedBy(delimiters: Array<out String>, startIndex: Int = 0, ignoreCase: Boolean = false, limit: Int = 0): Sequence<IntRange> {
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() val delimitersList = delimiters.asList()
return DelimitedRangesSequence(this, startIndex, limit, { startIndex -> findAnyOf(delimitersList, startIndex, ignoreCase = ignoreCase, last = false)?.let { it.first to it.second.length } }) return DelimitedRangesSequence(this, startIndex, limit, { startIndex -> findAnyOf(delimitersList, startIndex, ignoreCase = ignoreCase, last = false)?.let { it.first to it.second.length } })
@@ -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<String> public fun CharSequence.split(regex: Pattern, limit: Int = 0): List<String>
{ {
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() return regex.split(this, if (limit == 0) -1 else limit).asList()
} }
@@ -187,7 +187,7 @@ internal constructor(private val nativePattern: Pattern) {
* Zero by default means no limit is set. * Zero by default means no limit is set.
*/ */
public fun split(input: CharSequence, limit: Int = 0): List<String> { public fun split(input: CharSequence, limit: Int = 0): List<String> {
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() return nativePattern.split(input, if (limit == 0) -1 else limit).asList()
} }
@@ -9,7 +9,7 @@ import java.lang.IllegalStateException
* Throws an [IllegalArgumentException] if the [value] is false. * Throws an [IllegalArgumentException] if the [value] is false.
*/ */
@kotlin.internal.InlineOnly @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. * 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. * Throws an [IllegalArgumentException] if the [value] is null. Otherwise returns the not null value.
*/ */
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun <T:Any> requireNotNull(value: T?): T = requireNotNull(value) { "Required value was null" } public inline fun <T:Any> 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 * Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
@@ -50,7 +50,7 @@ public inline fun <T:Any> requireNotNull(value: T?, lazyMessage: () -> Any): T {
* Throws an [IllegalStateException] if the [value] is false. * Throws an [IllegalStateException] if the [value] is false.
*/ */
@kotlin.internal.InlineOnly @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. * 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. * returns the not null value.
*/ */
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun <T:Any> checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null" } public inline fun <T:Any> 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 * Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
@@ -480,10 +480,10 @@ fun aggregates(): List<GenericFunction> {
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { body { f ->
""" """
val iterator = this.iterator() 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 index = 1
var accumulator: S = iterator.next() var accumulator: S = iterator.next()
@@ -595,10 +595,10 @@ fun aggregates(): List<GenericFunction> {
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { body { f ->
""" """
val iterator = this.iterator() 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() var accumulator: S = iterator.next()
while (iterator.hasNext()) { while (iterator.hasNext()) {
@@ -627,10 +627,10 @@ fun aggregates(): List<GenericFunction> {
only(CharSequences, ArraysOfPrimitives) 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." } 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") returns("T")
body { body { f ->
""" """
var index = lastIndex 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--) var accumulator = get(index--)
while (index >= 0) { while (index >= 0) {
@@ -650,10 +650,10 @@ fun aggregates(): List<GenericFunction> {
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { body { f ->
""" """
var index = lastIndex 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--) var accumulator: S = get(index--)
while (index >= 0) { while (index >= 0) {
@@ -2,7 +2,6 @@ package templates
import templates.Family.* import templates.Family.*
fun elements(): List<GenericFunction> { fun elements(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>() val templates = arrayListOf<GenericFunction>()
@@ -14,7 +13,7 @@ fun elements(): List<GenericFunction> {
doc { f -> "Returns `true` if [element] is found in the ${f.collection}." } doc { f -> "Returns `true` if [element] is found in the ${f.collection}." }
typeParam("@kotlin.internal.OnlyInputTypes T") typeParam("@kotlin.internal.OnlyInputTypes T")
returns("Boolean") returns("Boolean")
body(Iterables) { body(Iterables) { f ->
""" """
if (this is Collection) if (this is Collection)
return contains(element) return contains(element)
@@ -327,12 +326,7 @@ fun elements(): List<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List -> { is List -> return this.first()
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
else
return this[0]
}
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -342,10 +336,10 @@ fun elements(): List<GenericFunction> {
} }
""" """
} }
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
""" """
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
return this[0] return this[0]
""" """
} }
@@ -401,10 +395,10 @@ fun elements(): List<GenericFunction> {
doc { f -> """Returns the first ${f.element} matching the given [predicate]. doc { f -> """Returns the first ${f.element} matching the given [predicate].
@throws [NoSuchElementException] if no such ${f.element} is found.""" } @throws [NoSuchElementException] if no such ${f.element} is found.""" }
returns("T") returns("T")
body { body { f ->
""" """
for (element in this) if (predicate(element)) return element 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<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List -> { is List -> return this.last()
if (isEmpty())
throw NoSuchElementException("Collection is empty.")
else
return this[this.lastIndex]
}
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -467,10 +456,10 @@ fun elements(): List<GenericFunction> {
return last return last
""" """
} }
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
""" """
if (isEmpty()) if (isEmpty())
throw NoSuchElementException("Collection is empty.") throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
return this[lastIndex] return this[lastIndex]
""" """
} }
@@ -541,18 +530,18 @@ fun elements(): List<GenericFunction> {
found = true 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 return last as T
""" """
} }
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects, Lists) { body(CharSequences, ArraysOfPrimitives, ArraysOfObjects, Lists) { f ->
""" """
for (index in this.indices.reversed()) { for (index in this.indices.reversed()) {
val element = this[index] val element = this[index]
if (predicate(element)) return element 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<GenericFunction> {
body { body {
""" """
when (this) { when (this) {
is List -> return when (size) { is List -> return this.single()
0 -> throw NoSuchElementException("Collection is empty.")
1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.")
}
else -> { else -> {
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) if (!iterator.hasNext())
@@ -633,21 +618,12 @@ fun elements(): List<GenericFunction> {
return single return single
""" """
} }
body(CharSequences) { body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
""" """
return when (length) { return when (${f.code.size}) {
0 -> throw NoSuchElementException("Collection is empty.") 0 -> throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
1 -> this[0] 1 -> this[0]
else -> throw IllegalArgumentException("Collection has more than one element.") else -> throw IllegalArgumentException("${f.doc.collection.capitalize()} 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.")
} }
""" """
} }
@@ -700,18 +676,18 @@ fun elements(): List<GenericFunction> {
include(CharSequences) 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}." } 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") returns("T")
body { body { f ->
""" """
var single: T? = null var single: T? = null
var found = false var found = false
for (element in this) { for (element in this) {
if (predicate(element)) { 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 single = element
found = true 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 return single as T
""" """
} }
@@ -23,7 +23,7 @@ fun filtering(): List<GenericFunction> {
returns("List<T>") returns("List<T>")
body { 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() if (n == 0) return toList()
val list: ArrayList<T> val list: ArrayList<T>
if (this is Collection<*>) { if (this is Collection<*>) {
@@ -54,7 +54,7 @@ fun filtering(): List<GenericFunction> {
returns(Sequences) { "Sequence<T>" } returns(Sequences) { "Sequence<T>" }
body(Sequences) { 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 { return when {
n == 0 -> this n == 0 -> this
this is DropTakeSequence -> this.drop(n) this is DropTakeSequence -> this.drop(n)
@@ -68,14 +68,14 @@ fun filtering(): List<GenericFunction> {
returns(Strings, CharSequences) { "SELF" } returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) { f -> 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)")} return ${subsequence(f, "n.coerceAtMost(length)")}
""" """
} }
body(ArraysOfObjects, ArraysOfPrimitives) { 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) if (n == 0)
return toList() return toList()
if (n >= size) if (n >= size)
@@ -96,7 +96,7 @@ fun filtering(): List<GenericFunction> {
returns("List<T>") returns("List<T>")
body { 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 (n == 0) return emptyList()
if (this is Collection<T> && n >= size) return toList() if (this is Collection<T> && n >= size) return toList()
var count = 0 var count = 0
@@ -115,7 +115,7 @@ fun filtering(): List<GenericFunction> {
returns(Strings, CharSequences) { "SELF" } returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) { f -> 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)")} return ${subsequence(f, "0", "n.coerceAtMost(length)")}
""" """
} }
@@ -124,7 +124,7 @@ fun filtering(): List<GenericFunction> {
returns(Sequences) { "Sequence<T>" } returns(Sequences) { "Sequence<T>" }
body(Sequences) { 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 { return when {
n == 0 -> emptySequence() n == 0 -> emptySequence()
this is DropTakeSequence -> this.take(n) this is DropTakeSequence -> this.take(n)
@@ -156,10 +156,10 @@ fun filtering(): List<GenericFunction> {
doc { "Returns a list containing all elements except last [n] elements." } doc { "Returns a list containing all elements except last [n] elements." }
returns("List<T>") returns("List<T>")
body { body { f ->
""" """
require(n >= 0, { "Requested element count $n is less than zero." }) require(n >= 0) { "Requested ${f.doc.element} count $n is less than zero." }
return take((size - n).coerceAtLeast(0)) return take((${f.code.size} - n).coerceAtLeast(0))
""" """
} }
@@ -167,13 +167,6 @@ fun filtering(): List<GenericFunction> {
doc(CharSequences) { "Returns a subsequence of this char sequence with the last [n] characters removed." } doc(CharSequences) { "Returns a subsequence of this char sequence with the last [n] characters removed." }
returns(Strings, CharSequences) { "SELF" } 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)") { templates add f("takeLast(n: Int)") {
@@ -187,7 +180,7 @@ fun filtering(): List<GenericFunction> {
returns(Strings, CharSequences) { "SELF" } returns(Strings, CharSequences) { "SELF" }
body(Strings, CharSequences) { f -> 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 val length = length
return ${subsequence(f, "length - n.coerceAtMost(length)")} return ${subsequence(f, "length - n.coerceAtMost(length)")}
""" """
@@ -195,7 +188,7 @@ fun filtering(): List<GenericFunction> {
body(Lists, ArraysOfObjects, ArraysOfPrimitives) { 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() if (n == 0) return emptyList()
val size = size val size = size
if (n >= size) return toList() if (n >= size) return toList()