diff --git a/libraries/stdlib/src/generated/_Aggregates.kt b/libraries/stdlib/src/generated/_Aggregates.kt index 4ffbbef591a..a90b5a1deec 100644 --- a/libraries/stdlib/src/generated/_Aggregates.kt +++ b/libraries/stdlib/src/generated/_Aggregates.kt @@ -323,70 +323,70 @@ public inline fun String.any(predicate: (Char) -> Boolean): Boolean { * Returns the number of elements */ public fun Array.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun BooleanArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun ByteArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun CharArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun DoubleArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun FloatArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun IntArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun LongArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun ShortArray.count(): Int { - return size + return size() } /** * Returns the number of elements */ public fun Collection.count(): Int { - return size + return size() } /** @@ -402,7 +402,7 @@ public fun Iterable.count(): Int { * Returns the number of elements */ public fun Map.count(): Int { - return size + return size() } /** @@ -418,7 +418,7 @@ public fun Stream.count(): Int { * Returns the number of elements */ public fun String.count(): Int { - return size + return length() } /** @@ -650,7 +650,7 @@ public inline fun String.fold(initial: R, operation: (R, Char) -> R): R { * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun Array.foldRight(initial: R, operation: (T, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -662,7 +662,7 @@ public inline fun Array.foldRight(initial: R, operation: (T, R) -> * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -674,7 +674,7 @@ public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, R) * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun ByteArray.foldRight(initial: R, operation: (Byte, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -686,7 +686,7 @@ public inline fun ByteArray.foldRight(initial: R, operation: (Byte, R) -> R) * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun CharArray.foldRight(initial: R, operation: (Char, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -698,7 +698,7 @@ public inline fun CharArray.foldRight(initial: R, operation: (Char, R) -> R) * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun DoubleArray.foldRight(initial: R, operation: (Double, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -710,7 +710,7 @@ public inline fun DoubleArray.foldRight(initial: R, operation: (Double, R) - * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun FloatArray.foldRight(initial: R, operation: (Float, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -722,7 +722,7 @@ public inline fun FloatArray.foldRight(initial: R, operation: (Float, R) -> * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun IntArray.foldRight(initial: R, operation: (Int, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -734,7 +734,7 @@ public inline fun IntArray.foldRight(initial: R, operation: (Int, R) -> R): * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun LongArray.foldRight(initial: R, operation: (Long, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -746,7 +746,7 @@ public inline fun LongArray.foldRight(initial: R, operation: (Long, R) -> R) * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun ShortArray.foldRight(initial: R, operation: (Short, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -758,7 +758,7 @@ public inline fun ShortArray.foldRight(initial: R, operation: (Short, R) -> * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun List.foldRight(initial: R, operation: (T, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -770,7 +770,7 @@ public inline fun List.foldRight(initial: R, operation: (T, R) -> R): * Accumulates value starting with *initial* value and applying *operation* from right to left to each element and current accumulator value */ public inline fun String.foldRight(initial: R, operation: (Char, R) -> R): R { - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -1499,7 +1499,7 @@ public fun String.min(): Char? { * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun , T : Any> Array.minBy(f: (T) -> R): T? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1517,7 +1517,7 @@ public inline fun , T : Any> Array.minBy(f: (T) -> R): * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > BooleanArray.minBy(f: (Boolean) -> R): Boolean? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1535,7 +1535,7 @@ public inline fun > BooleanArray.minBy(f: (Boolean) -> R): Boo * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > ByteArray.minBy(f: (Byte) -> R): Byte? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1553,7 +1553,7 @@ public inline fun > ByteArray.minBy(f: (Byte) -> R): Byte? { * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > CharArray.minBy(f: (Char) -> R): Char? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1571,7 +1571,7 @@ public inline fun > CharArray.minBy(f: (Char) -> R): Char? { * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > DoubleArray.minBy(f: (Double) -> R): Double? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1589,7 +1589,7 @@ public inline fun > DoubleArray.minBy(f: (Double) -> R): Doubl * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > FloatArray.minBy(f: (Float) -> R): Float? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1607,7 +1607,7 @@ public inline fun > FloatArray.minBy(f: (Float) -> R): Float? * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > IntArray.minBy(f: (Int) -> R): Int? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1625,7 +1625,7 @@ public inline fun > IntArray.minBy(f: (Int) -> R): Int? { * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > LongArray.minBy(f: (Long) -> R): Long? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -1643,7 +1643,7 @@ public inline fun > LongArray.minBy(f: (Long) -> R): Long? { * Returns the first element yielding the smallest value of the given function or null if there are no elements */ public inline fun > ShortArray.minBy(f: (Short) -> R): Short? { - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) for (i in 1..lastIndex) { @@ -2101,7 +2101,7 @@ public inline fun String.reduce(operation: (Char, Char) -> Char): Char { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun Array.reduceRight(operation: (T, T) -> T): T { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2114,7 +2114,7 @@ public inline fun Array.reduceRight(operation: (T, T) -> T): T { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2127,7 +2127,7 @@ public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Bool * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2140,7 +2140,7 @@ public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2153,7 +2153,7 @@ public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double): Double { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2166,7 +2166,7 @@ public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double) * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Float { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2179,7 +2179,7 @@ public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Fl * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2192,7 +2192,7 @@ public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2205,7 +2205,7 @@ public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Short { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2218,7 +2218,7 @@ public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Sh * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun List.reduceRight(operation: (T, T) -> T): T { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { @@ -2231,7 +2231,7 @@ public inline fun List.reduceRight(operation: (T, T) -> T): T { * Accumulates value starting with last element and applying *operation* from right to left to each element and current accumulator value */ public inline fun String.reduceRight(operation: (Char, Char) -> Char): Char { - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) while (index >= 0) { diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index b5681a6658d..2cc2676847a 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -11,63 +11,63 @@ import java.util.* * Returns true if the array is empty */ public fun Array.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun BooleanArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun ByteArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun CharArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun DoubleArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun FloatArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun IntArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun LongArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** * Returns true if the array is empty */ public fun ShortArray.isEmpty(): Boolean { - return size == 0 + return size() == 0 } /** diff --git a/libraries/stdlib/src/generated/_Elements.kt b/libraries/stdlib/src/generated/_Elements.kt index 26f9ce09cd9..964f22daa65 100644 --- a/libraries/stdlib/src/generated/_Elements.kt +++ b/libraries/stdlib/src/generated/_Elements.kt @@ -549,7 +549,7 @@ public fun String.elementAt(index: Int): Char { * Returns first element */ public fun Array.first(): T { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -558,7 +558,7 @@ public fun Array.first(): T { * Returns first element */ public fun BooleanArray.first(): Boolean { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -567,7 +567,7 @@ public fun BooleanArray.first(): Boolean { * Returns first element */ public fun ByteArray.first(): Byte { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -576,7 +576,7 @@ public fun ByteArray.first(): Byte { * Returns first element */ public fun CharArray.first(): Char { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -585,7 +585,7 @@ public fun CharArray.first(): Char { * Returns first element */ public fun DoubleArray.first(): Double { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -594,7 +594,7 @@ public fun DoubleArray.first(): Double { * Returns first element */ public fun FloatArray.first(): Float { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -603,7 +603,7 @@ public fun FloatArray.first(): Float { * Returns first element */ public fun IntArray.first(): Int { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -612,7 +612,7 @@ public fun IntArray.first(): Int { * Returns first element */ public fun LongArray.first(): Long { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -621,7 +621,7 @@ public fun LongArray.first(): Long { * Returns first element */ public fun ShortArray.first(): Short { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -632,7 +632,7 @@ public fun ShortArray.first(): Short { public fun Iterable.first(): T { when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[0] as T @@ -650,7 +650,7 @@ public fun Iterable.first(): T { * Returns first element */ public fun List.first(): T { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -661,7 +661,7 @@ public fun List.first(): T { public fun Stream.first(): T { when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[0] as T @@ -679,7 +679,7 @@ public fun Stream.first(): T { * Returns first element */ public fun String.first(): Char { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] } @@ -784,63 +784,63 @@ public inline fun String.first(predicate: (Char) -> Boolean): Char { * Returns first element, or null if collection is empty */ public fun Array.firstOrNull(): T? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun BooleanArray.firstOrNull(): Boolean? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun ByteArray.firstOrNull(): Byte? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun CharArray.firstOrNull(): Char? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun DoubleArray.firstOrNull(): Double? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun FloatArray.firstOrNull(): Float? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun IntArray.firstOrNull(): Int? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun LongArray.firstOrNull(): Long? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** * Returns first element, or null if collection is empty */ public fun ShortArray.firstOrNull(): Short? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** @@ -849,7 +849,7 @@ public fun ShortArray.firstOrNull(): Short? { public fun Iterable.firstOrNull(): T? { when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) return null else return this[0] as T @@ -867,7 +867,7 @@ public fun Iterable.firstOrNull(): T? { * Returns first element, or null if collection is empty */ public fun List.firstOrNull(): T? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** @@ -876,7 +876,7 @@ public fun List.firstOrNull(): T? { public fun Stream.firstOrNull(): T? { when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) return null else return this[0] as T @@ -894,7 +894,7 @@ public fun Stream.firstOrNull(): T? { * Returns first element, or null if collection is empty */ public fun String.firstOrNull(): Char? { - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] } /** @@ -1139,81 +1139,81 @@ public fun Stream.indexOf(element: T): Int { * Returns last element */ public fun Array.last(): T { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun BooleanArray.last(): Boolean { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun ByteArray.last(): Byte { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun CharArray.last(): Char { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun DoubleArray.last(): Double { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun FloatArray.last(): Float { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun IntArray.last(): Int { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun LongArray.last(): Long { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun ShortArray.last(): Short { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** @@ -1222,10 +1222,10 @@ public fun ShortArray.last(): Short { public fun Iterable.last(): T { when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") else - return this[size - 1] as T + return this[this.lastIndex] as T } else -> { val iterator = iterator() @@ -1243,41 +1243,31 @@ public fun Iterable.last(): T { * Returns last element */ public fun List.last(): T { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** * Returns last element */ public fun Stream.last(): T { - when (this) { - is List<*> -> { - if (size == 0) - throw NoSuchElementException("Collection is empty") - else - return this[size - 1] as T - } - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - throw NoSuchElementException("Collection is empty") - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty") + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last } /** * Returns last element */ public fun String.last(): Char { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] } /** @@ -1640,63 +1630,63 @@ public fun Stream.lastIndexOf(element: T): Int { * Returns last element, or null if collection is empty */ public fun Array.lastOrNull(): T? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun BooleanArray.lastOrNull(): Boolean? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun ByteArray.lastOrNull(): Byte? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun CharArray.lastOrNull(): Char? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun DoubleArray.lastOrNull(): Double? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun FloatArray.lastOrNull(): Float? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun IntArray.lastOrNull(): Int? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun LongArray.lastOrNull(): Long? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun ShortArray.lastOrNull(): Short? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** @@ -1704,7 +1694,7 @@ public fun ShortArray.lastOrNull(): Short? { */ public fun Iterable.lastOrNull(): T? { when (this) { - is List<*> -> return if (size > 0) this[size - 1] as T else null + is List<*> -> return if (isEmpty()) null else this[size() - 1] as T else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -1721,32 +1711,27 @@ public fun Iterable.lastOrNull(): T? { * Returns last element, or null if collection is empty */ public fun List.lastOrNull(): T? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[size() - 1] } /** * Returns last element, or null if collection is empty */ public fun Stream.lastOrNull(): T? { - when (this) { - is List<*> -> return if (size > 0) this[size - 1] as T else null - else -> { - val iterator = iterator() - if (!iterator.hasNext()) - return null - var last = iterator.next() - while (iterator.hasNext()) - last = iterator.next() - return last - } - } + val iterator = iterator() + if (!iterator.hasNext()) + return null + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last } /** * Returns last element, or null if collection is empty */ public fun String.lastOrNull(): Char? { - return if (size > 0) this[size - 1] else null + return if (isEmpty()) null else this[length() - 1] } /** @@ -1909,7 +1894,7 @@ public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { * Returns single element, or throws exception if there is no or more than one element */ public fun Array.single(): T { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1920,7 +1905,7 @@ public fun Array.single(): T { * Returns single element, or throws exception if there is no or more than one element */ public fun BooleanArray.single(): Boolean { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1931,7 +1916,7 @@ public fun BooleanArray.single(): Boolean { * Returns single element, or throws exception if there is no or more than one element */ public fun ByteArray.single(): Byte { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1942,7 +1927,7 @@ public fun ByteArray.single(): Byte { * Returns single element, or throws exception if there is no or more than one element */ public fun CharArray.single(): Char { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1953,7 +1938,7 @@ public fun CharArray.single(): Char { * Returns single element, or throws exception if there is no or more than one element */ public fun DoubleArray.single(): Double { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1964,7 +1949,7 @@ public fun DoubleArray.single(): Double { * Returns single element, or throws exception if there is no or more than one element */ public fun FloatArray.single(): Float { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1975,7 +1960,7 @@ public fun FloatArray.single(): Float { * Returns single element, or throws exception if there is no or more than one element */ public fun IntArray.single(): Int { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1986,7 +1971,7 @@ public fun IntArray.single(): Int { * Returns single element, or throws exception if there is no or more than one element */ public fun LongArray.single(): Long { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -1997,7 +1982,7 @@ public fun LongArray.single(): Long { * Returns single element, or throws exception if there is no or more than one element */ public fun ShortArray.single(): Short { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -2009,7 +1994,7 @@ public fun ShortArray.single(): Short { */ public fun Iterable.single(): T { when (this) { - is List<*> -> return when (size) { + is List<*> -> return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] as T else -> throw IllegalArgumentException("Collection has more than one element") @@ -2030,7 +2015,7 @@ public fun Iterable.single(): T { * Returns single element, or throws exception if there is no or more than one element */ public fun List.single(): T { - return when (size) { + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -2042,7 +2027,7 @@ public fun List.single(): T { */ public fun Stream.single(): T { when (this) { - is List<*> -> return when (size) { + is List<*> -> return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] as T else -> throw IllegalArgumentException("Collection has more than one element") @@ -2063,7 +2048,7 @@ public fun Stream.single(): T { * Returns single element, or throws exception if there is no or more than one element */ public fun String.single(): Char { - return when (size) { + return when (length()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -2278,63 +2263,63 @@ public inline fun String.single(predicate: (Char) -> Boolean): Char { * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun Array.singleOrNull(): T? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun BooleanArray.singleOrNull(): Boolean? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun ByteArray.singleOrNull(): Byte? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun CharArray.singleOrNull(): Char? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun DoubleArray.singleOrNull(): Double? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun FloatArray.singleOrNull(): Float? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun IntArray.singleOrNull(): Int? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun LongArray.singleOrNull(): Long? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun ShortArray.singleOrNull(): Short? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** @@ -2342,7 +2327,7 @@ public fun ShortArray.singleOrNull(): Short? { */ public fun Iterable.singleOrNull(): T? { when (this) { - is List<*> -> return if (size == 1) this[0] as T else null + is List<*> -> return if (size() == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -2359,7 +2344,7 @@ public fun Iterable.singleOrNull(): T? { * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun List.singleOrNull(): T? { - return if (size == 1) this[0] else null + return if (size() == 1) this[0] else null } /** @@ -2367,7 +2352,7 @@ public fun List.singleOrNull(): T? { */ public fun Stream.singleOrNull(): T? { when (this) { - is List<*> -> return if (size == 1) this[0] as T else null + is List<*> -> return if (size() == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -2384,7 +2369,7 @@ public fun Stream.singleOrNull(): T? { * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun String.singleOrNull(): Char? { - return if (size == 1) this[0] else null + return if (length() == 1) this[0] else null } /** diff --git a/libraries/stdlib/src/generated/_Filtering.kt b/libraries/stdlib/src/generated/_Filtering.kt index b7fd46ff924..e61164a49f6 100644 --- a/libraries/stdlib/src/generated/_Filtering.kt +++ b/libraries/stdlib/src/generated/_Filtering.kt @@ -11,10 +11,10 @@ import java.util.* * Returns a list containing all elements except first *n* elements */ public fun Array.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -25,10 +25,10 @@ public fun Array.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun BooleanArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -39,10 +39,10 @@ public fun BooleanArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun ByteArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -53,10 +53,10 @@ public fun ByteArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun CharArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -67,10 +67,10 @@ public fun CharArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun DoubleArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -81,10 +81,10 @@ public fun DoubleArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun FloatArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -95,10 +95,10 @@ public fun FloatArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun IntArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -109,10 +109,10 @@ public fun IntArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun LongArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -123,10 +123,10 @@ public fun LongArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun ShortArray.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -137,10 +137,10 @@ public fun ShortArray.drop(n: Int): List { * Returns a list containing all elements except first *n* elements */ public fun Collection.drop(n: Int): List { - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -170,7 +170,7 @@ public fun Stream.drop(n: Int): Stream { * Returns a list containing all elements except first *n* elements */ public fun String.drop(n: Int): String { - return substring(Math.min(n, size)) + return substring(Math.min(n, length())) } /** @@ -885,7 +885,7 @@ public fun String.slice(indices: Iterable): String { */ public fun Array.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -900,7 +900,7 @@ public fun Array.take(n: Int): List { */ public fun BooleanArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -915,7 +915,7 @@ public fun BooleanArray.take(n: Int): List { */ public fun ByteArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -930,7 +930,7 @@ public fun ByteArray.take(n: Int): List { */ public fun CharArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -945,7 +945,7 @@ public fun CharArray.take(n: Int): List { */ public fun DoubleArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -960,7 +960,7 @@ public fun DoubleArray.take(n: Int): List { */ public fun FloatArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -975,7 +975,7 @@ public fun FloatArray.take(n: Int): List { */ public fun IntArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -990,7 +990,7 @@ public fun IntArray.take(n: Int): List { */ public fun LongArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -1005,7 +1005,7 @@ public fun LongArray.take(n: Int): List { */ public fun ShortArray.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -1020,7 +1020,7 @@ public fun ShortArray.take(n: Int): List { */ public fun Collection.take(n: Int): List { var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) @@ -1055,7 +1055,7 @@ public fun Stream.take(n: Int): Stream { * Returns a list containing first *n* elements */ public fun String.take(n: Int): String { - return substring(0, Math.min(n, size)) + return substring(0, Math.min(n, length())) } /** diff --git a/libraries/stdlib/src/generated/_Sets.kt b/libraries/stdlib/src/generated/_Sets.kt index 961e19e8a9a..d0036b84892 100644 --- a/libraries/stdlib/src/generated/_Sets.kt +++ b/libraries/stdlib/src/generated/_Sets.kt @@ -261,7 +261,7 @@ public fun Iterable.subtract(other: Iterable): Set { * Returns a mutable set containing all distinct elements from the given collection. */ public fun Array.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -270,7 +270,7 @@ public fun Array.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun BooleanArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -279,7 +279,7 @@ public fun BooleanArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun ByteArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -288,7 +288,7 @@ public fun ByteArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun CharArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -297,7 +297,7 @@ public fun CharArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun DoubleArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -306,7 +306,7 @@ public fun DoubleArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun FloatArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -315,7 +315,7 @@ public fun FloatArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun IntArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -324,7 +324,7 @@ public fun IntArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun LongArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } @@ -333,7 +333,7 @@ public fun LongArray.toMutableSet(): MutableSet { * Returns a mutable set containing all distinct elements from the given collection. */ public fun ShortArray.toMutableSet(): MutableSet { - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set } diff --git a/libraries/stdlib/src/generated/_Snapshots.kt b/libraries/stdlib/src/generated/_Snapshots.kt index 5fcf852e08d..3f17c9fe558 100644 --- a/libraries/stdlib/src/generated/_Snapshots.kt +++ b/libraries/stdlib/src/generated/_Snapshots.kt @@ -11,7 +11,7 @@ import java.util.* * Returns an ArrayList of all elements */ public fun Array.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -20,7 +20,7 @@ public fun Array.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun BooleanArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -29,7 +29,7 @@ public fun BooleanArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun ByteArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -38,7 +38,7 @@ public fun ByteArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun CharArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -47,7 +47,7 @@ public fun CharArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun DoubleArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -56,7 +56,7 @@ public fun DoubleArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun FloatArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -65,7 +65,7 @@ public fun FloatArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun IntArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -74,7 +74,7 @@ public fun IntArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun LongArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -83,7 +83,7 @@ public fun LongArray.toArrayList(): ArrayList { * Returns an ArrayList of all elements */ public fun ShortArray.toArrayList(): ArrayList { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -401,7 +401,7 @@ public fun String.toLinkedList(): LinkedList { * Returns a List containing all key-value pairs */ public fun Map.toList(): List> { - val result = ArrayList>(size) + val result = ArrayList>(size()) for (item in this) result.add(item.key to item.value) return result @@ -418,7 +418,7 @@ public fun Array.toList(): List { * Returns a List containing all elements */ public fun BooleanArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -427,7 +427,7 @@ public fun BooleanArray.toList(): List { * Returns a List containing all elements */ public fun ByteArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -436,7 +436,7 @@ public fun ByteArray.toList(): List { * Returns a List containing all elements */ public fun CharArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -445,7 +445,7 @@ public fun CharArray.toList(): List { * Returns a List containing all elements */ public fun DoubleArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -454,7 +454,7 @@ public fun DoubleArray.toList(): List { * Returns a List containing all elements */ public fun FloatArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -463,7 +463,7 @@ public fun FloatArray.toList(): List { * Returns a List containing all elements */ public fun IntArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -472,7 +472,7 @@ public fun IntArray.toList(): List { * Returns a List containing all elements */ public fun LongArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } @@ -481,7 +481,7 @@ public fun LongArray.toList(): List { * Returns a List containing all elements */ public fun ShortArray.toList(): List { - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list } diff --git a/libraries/stdlib/src/generated/_SpecialJVM.kt b/libraries/stdlib/src/generated/_SpecialJVM.kt index a37737b08f3..87b9c4d1210 100644 --- a/libraries/stdlib/src/generated/_SpecialJVM.kt +++ b/libraries/stdlib/src/generated/_SpecialJVM.kt @@ -10,56 +10,56 @@ import java.util.* /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun Array.binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun ByteArray.binarySearch(element: Byte, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun CharArray.binarySearch(element: Char, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun DoubleArray.binarySearch(element: Double, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun FloatArray.binarySearch(element: Float, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun IntArray.binarySearch(element: Int, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun LongArray.binarySearch(element: Long, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } /** * Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted. */ -public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size): Int { +public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: Int = size()): Int { return Arrays.binarySearch(this, fromIndex, toIndex, element) } @@ -67,63 +67,63 @@ public fun ShortArray.binarySearch(element: Short, fromIndex: Int = 0, toIndex: * Returns new array which is a copy of the original array */ public fun Array.copyOf(): Array { - return Arrays.copyOf(this, size) as Array + return Arrays.copyOf(this, size()) as Array } /** * Returns new array which is a copy of the original array */ public fun BooleanArray.copyOf(): BooleanArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun ByteArray.copyOf(): ByteArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun CharArray.copyOf(): CharArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun DoubleArray.copyOf(): DoubleArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun FloatArray.copyOf(): FloatArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun IntArray.copyOf(): IntArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun LongArray.copyOf(): LongArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** * Returns new array which is a copy of the original array */ public fun ShortArray.copyOf(): ShortArray { - return Arrays.copyOf(this, size) + return Arrays.copyOf(this, size()) } /** @@ -417,56 +417,56 @@ public fun , R : T> Stream.filterIsInstanceTo( /** * Sorts array or range in array inplace */ -public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun Array.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun ByteArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun DoubleArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun FloatArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun IntArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun LongArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } /** * Sorts array or range in array inplace */ -public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit { +public fun ShortArray.sort(fromIndex: Int = 0, toIndex: Int = size()): Unit { Arrays.sort(this, fromIndex, toIndex) } diff --git a/libraries/stdlib/src/kotlin/Deprecated.kt b/libraries/stdlib/src/kotlin/Deprecated.kt index 447b4dd0483..e8f0328af0f 100644 --- a/libraries/stdlib/src/kotlin/Deprecated.kt +++ b/libraries/stdlib/src/kotlin/Deprecated.kt @@ -79,3 +79,68 @@ public val BooleanArray.size: Int get() = size() deprecated("Use compareValuesBy() instead") public fun compareBy(a: T?, b: T?, vararg functions: (T) -> Comparable<*>?): Int = compareValuesBy(a, b, *functions) + + +/** + * Returns the first item in the list or null if the list is empty + * + * @includeFunctionBody ../../test/collections/ListSpecificTest.kt first + */ +deprecated("Use firstOrNull() function instead") +public val List.first: T? + get() = this.firstOrNull() + + +/** + * Returns the last item in the list or null if the list is empty + * + * @includeFunctionBody ../../test/collections/ListSpecificTest.kt last + */ +deprecated("Use lastOrNull() function instead") +public val List.last: T? + get() { + val s = this.size() + return if (s > 0) this[s - 1] else null + } + + +/** + * Returns the first item in the list or null if the list is empty + * + * @includeFunctionBody ../../test/collections/ListSpecificTest.kt head + */ +deprecated("Use firstOrNull() function instead") +public val List.head: T? + get() = firstOrNull() + +/** + * Returns all elements in this collection apart from the first one + * + * @includeFunctionBody ../../test/collections/ListSpecificTest.kt tail + */ +deprecated("Use drop(1) function call instead") +public val List.tail: List + get() { + return drop(1) + } + +/** Returns true if this collection is empty */ +deprecated("Use isEmpty() function call instead") +public val Collection<*>.empty: Boolean + get() = isEmpty() + +/** Returns the size of the collection */ +deprecated("Use size() function call instead") +public val Collection<*>.size: Int + get() = size() + + +/** Returns the size of the map */ +deprecated("Use size() function call instead") +public val Map<*, *>.size: Int + get() = size() + +/** Returns true if this map is empty */ +deprecated("Use isEmpty() function call instead") +public val Map<*, *>.empty: Boolean + get() = isEmpty() diff --git a/libraries/stdlib/src/kotlin/DeprecatedJVM.kt b/libraries/stdlib/src/kotlin/DeprecatedJVM.kt index 2f5930563b3..b0beed1198e 100644 --- a/libraries/stdlib/src/kotlin/DeprecatedJVM.kt +++ b/libraries/stdlib/src/kotlin/DeprecatedJVM.kt @@ -37,3 +37,8 @@ public /*inline*/ fun callable(action: () -> T): Callable { public override fun call() = action() } } + +deprecated("Use length() instead") +public val String.size: Int + get() = length() + diff --git a/libraries/stdlib/src/kotlin/Ordering.kt b/libraries/stdlib/src/kotlin/Ordering.kt index d6265a5657f..4a3bc01e288 100644 --- a/libraries/stdlib/src/kotlin/Ordering.kt +++ b/libraries/stdlib/src/kotlin/Ordering.kt @@ -22,7 +22,7 @@ import java.util.Comparator * Compares two values using the sequence of functions to calculate a result of comparison. */ public fun compareValuesBy(a: T?, b: T?, vararg functions: (T) -> Comparable<*>?): Int { - require(functions.size > 0) + require(functions.size() > 0) if (a identityEquals b) return 0 if (a == null) return -1 if (b == null) return 1 diff --git a/libraries/stdlib/src/kotlin/collections/Arrays.kt b/libraries/stdlib/src/kotlin/collections/Arrays.kt index 75540a56529..5adf4874fdd 100644 --- a/libraries/stdlib/src/kotlin/collections/Arrays.kt +++ b/libraries/stdlib/src/kotlin/collections/Arrays.kt @@ -11,56 +11,56 @@ public inline fun Array(size: Int, init: (Int) -> T): Array { } public val BooleanArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val ByteArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val ShortArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val IntArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val LongArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val FloatArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val DoubleArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val CharArray.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val Array<*>.lastIndex: Int - get() = this.size - 1 + get() = size() - 1 public val BooleanArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val ByteArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val ShortArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val IntArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val LongArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val FloatArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val DoubleArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val CharArray.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) public val Array<*>.indices: IntRange - get() = IntRange(0, this.size - 1) + get() = IntRange(0, lastIndex) diff --git a/libraries/stdlib/src/kotlin/collections/ImmutableArrayList.kt b/libraries/stdlib/src/kotlin/collections/ImmutableArrayList.kt index fbb9bee3a18..fe7f89f38a8 100644 --- a/libraries/stdlib/src/kotlin/collections/ImmutableArrayList.kt +++ b/libraries/stdlib/src/kotlin/collections/ImmutableArrayList.kt @@ -17,8 +17,8 @@ private class ImmutableArrayList( throw IllegalArgumentException("Negative length ($length)") } // possible when builder is used from different threads - if (offset + length > array.size) { - throw IllegalArgumentException("offset ($offset) + length ($length) > array.length (${array.size})") + if (offset + length > array.size()) { + throw IllegalArgumentException("offset ($offset) + length ($length) > array.length (${array.size()})") } } @@ -79,8 +79,8 @@ public class ImmutableArrayListBuilder() { } public fun ensureCapacity(capacity: Int) { - if (array.size < capacity) { - val newSize = Math.max(capacity, Math.max(array.size * 2, 11)) + if (array.size() < capacity) { + val newSize = Math.max(capacity, Math.max(array.size() * 2, 11)) array = array.copyOf(newSize) } } diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index 4b76a5ebeea..148820a3380 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -11,13 +11,13 @@ private val stdlib_emptyMap : Map = stdlib_emptyMapClass() private fun stdlib_emptyMap() = stdlib_emptyMap as Map /** Returns a new read-only list of given elements */ -public fun listOf(vararg values: T): List = if (values.size == 0) stdlib_emptyList() else arrayListOf(*values) +public fun listOf(vararg values: T): List = if (values.size() == 0) stdlib_emptyList() else arrayListOf(*values) /** Returns an empty list */ public fun listOf(): List = stdlib_emptyList() /** Returns a new read-only map of given pairs, where the first value is the key, and the second is value */ -public fun mapOf(vararg values: Pair): Map = if (values.size == 0) stdlib_emptyMap() else linkedMapOf(*values) +public fun mapOf(vararg values: Pair): Map = if (values.size() == 0) stdlib_emptyMap() else linkedMapOf(*values) /** Returns an empty read-only map */ public fun mapOf(): Map = stdlib_emptyMap() @@ -29,10 +29,10 @@ public fun setOf(vararg values: T): Set = values.toCollection(LinkedHashSe public fun linkedListOf(vararg values: T): LinkedList = values.toCollection(LinkedList()) /** Returns a new ArrayList with a variable number of initial elements */ -public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size)) +public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size())) /** Returns a new HashSet with a variable number of initial elements */ -public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(values.size)) +public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(values.size())) /** * Returns a new [[HashMap]] populated with the given pairs where the first value in each pair @@ -41,7 +41,7 @@ public fun hashSetOf(vararg values: T): HashSet = values.toCollection(Hash * @includeFunctionBody ../../test/collections/MapTest.kt createUsingPairs */ public fun hashMapOf(vararg values: Pair): HashMap { - val answer = HashMap(values.size) + val answer = HashMap(values.size()) answer.putAll(*values) return answer } @@ -54,25 +54,26 @@ public fun hashMapOf(vararg values: Pair): HashMap { * @includeFunctionBody ../../test/collections/MapTest.kt createLinkedMap */ public fun linkedMapOf(vararg values: Pair): LinkedHashMap { - val answer = LinkedHashMap(values.size) + val answer = LinkedHashMap(values.size()) answer.putAll(*values) return answer } -/** Returns the size of the collection */ -public val Collection<*>.size: Int - get() = size() - -/** Returns true if this collection is empty */ -public val Collection<*>.empty: Boolean - get() = isEmpty() - public val Collection<*>.indices: IntRange - get() = 0..size - 1 + get() = 0..size() - 1 public val Int.indices: IntRange get() = 0..this - 1 +/** + * Returns the index of the last item in the list or -1 if the list is empty + * + * @includeFunctionBody ../../test/collections/ListSpecificTest.kt lastIndex + */ +public val List.lastIndex: Int + get() = this.size() - 1 + + /** Returns true if the collection is not empty */ public fun Collection.isNotEmpty(): Boolean = !this.isEmpty() @@ -87,49 +88,3 @@ public fun Collection?.orEmpty(): Collection = this ?: stdlib_emptyLis /** Returns the List if its not null otherwise returns the empty list */ public fun List?.orEmpty(): List = this ?: stdlib_emptyList() - -/** - * Returns the first item in the list or null if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt first - */ -public val List.first: T? - get() = this.head - - -/** - * Returns the last item in the list or null if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt last - */ -public val List.last: T? - get() { - val s = this.size - return if (s > 0) this[s - 1] else null - } - -/** - * Returns the index of the last item in the list or -1 if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt lastIndex - */ -public val List.lastIndex: Int - get() = this.size - 1 - -/** - * Returns the first item in the list or null if the list is empty - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt head - */ -public val List.head: T? - get() = if (this.isNotEmpty()) this[0] else null - -/** - * Returns all elements in this collection apart from the first one - * - * @includeFunctionBody ../../test/collections/ListSpecificTest.kt tail - */ -public val List.tail: List - get() { - return this.drop(1) - } diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index 29a374e6880..2fa56f12e56 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -4,14 +4,6 @@ import java.util.* // Map APIs -/** Returns the size of the map */ -public val Map<*, *>.size: Int - get() = size() - -/** Returns true if this map is empty */ -public val Map<*, *>.empty: Boolean - get() = isEmpty() - /** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */ public fun Map?.orEmpty() : Map = if (this != null) this else stdlib_emptyMap() diff --git a/libraries/stdlib/src/kotlin/dom/DomJVM.kt b/libraries/stdlib/src/kotlin/dom/DomJVM.kt index dd8b752a310..efc3fec54ef 100644 --- a/libraries/stdlib/src/kotlin/dom/DomJVM.kt +++ b/libraries/stdlib/src/kotlin/dom/DomJVM.kt @@ -123,7 +123,7 @@ public var Element.classSet: MutableSet val answer = LinkedHashSet() val array = this.classes.split("""\s""") for (s in array) { - if (s.size > 0) { + if (s.length() > 0) { answer.add(s) } } diff --git a/libraries/stdlib/src/kotlin/io/Files.kt b/libraries/stdlib/src/kotlin/io/Files.kt index bfd08feaa4f..dc17f93e407 100644 --- a/libraries/stdlib/src/kotlin/io/Files.kt +++ b/libraries/stdlib/src/kotlin/io/Files.kt @@ -59,8 +59,8 @@ public fun File.relativePath(descendant: File): String { val prefix = directory.canonicalPath val answer = descendant.canonicalPath return if (answer.startsWith(prefix)) { - val prefixSize = prefix.size - if (answer.size > prefixSize) { + val prefixSize = prefix.length() + if (answer.length() > prefixSize) { answer.substring(prefixSize + 1) } else "" } else { diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index f83224e375a..3d5ba5130b4 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -69,6 +69,12 @@ public val CharSequence.length: Int public fun CharSequence.get(index: Int): Char = this.charAt(index) +/** + * Returns the index of the last character in the String or -1 if the String is empty + */ +public val String.lastIndex: Int + get() = this.length() - 1 + /** * Returns a subsequence specified by given set of indices. */ diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index 510eee92eae..f087cecf7bf 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -122,9 +122,6 @@ public fun String.toUpperCase(locale: java.util.Locale): String = (this as java. public val CharSequence.size: Int get() = this.length -public val String.size: Int - get() = length() - public fun String.toBoolean(): Boolean = java.lang.Boolean.parseBoolean(this) public fun String.toShort(): Short = java.lang.Short.parseShort(this) public fun String.toInt(): Int = java.lang.Integer.parseInt(this) diff --git a/libraries/stdlib/test/ExceptionTest.kt b/libraries/stdlib/test/ExceptionTest.kt index 5481959a823..39a854cd1aa 100644 --- a/libraries/stdlib/test/ExceptionTest.kt +++ b/libraries/stdlib/test/ExceptionTest.kt @@ -40,6 +40,6 @@ class ExceptionTest { } val bytes = assertNotNull(byteBuffer.toByteArray()) - assertTrue(bytes.size > 10) + assertTrue(bytes.size() > 10) } } diff --git a/libraries/stdlib/test/OldStdlibTest.kt b/libraries/stdlib/test/OldStdlibTest.kt index 3006520a0bf..ea2a88713ad 100644 --- a/libraries/stdlib/test/OldStdlibTest.kt +++ b/libraries/stdlib/test/OldStdlibTest.kt @@ -10,13 +10,13 @@ import org.junit.Test as test class OldStdlibTest() { test fun testCollectionEmpty() { assertNot { - listOf(0, 1, 2).empty + listOf(0, 1, 2).isEmpty() } } test fun testCollectionSize() { assertTrue { - listOf(0, 1, 2).size == 3 + listOf(0, 1, 2).size() == 3 } } diff --git a/libraries/stdlib/test/TuplesTest.kt b/libraries/stdlib/test/TuplesTest.kt index ca241f1fc5d..60868864909 100644 --- a/libraries/stdlib/test/TuplesTest.kt +++ b/libraries/stdlib/test/TuplesTest.kt @@ -40,8 +40,8 @@ class PairTest { } test fun pairHashSet() { - val s = hashSet(Pair(1, "a"), Pair(1, "b"), Pair(1, "a")) - assertEquals(2, s.size) + val s = hashSetOf(Pair(1, "a"), Pair(1, "b"), Pair(1, "a")) + assertEquals(2, s.size()) assertTrue(s.contains(p)) } } @@ -85,8 +85,8 @@ class TripleTest { } test fun tripleHashSet() { - val s = hashSet(Triple(1, "a", 0.07), Triple(1, "b", 0.07), Triple(1, "a", 0.07)) - assertEquals(2, s.size) + val s = hashSetOf(Triple(1, "a", 0.07), Triple(1, "b", 0.07), Triple(1, "a", 0.07)) + assertEquals(2, s.size()) assertTrue(s.contains(t)) } } diff --git a/libraries/stdlib/test/collections/ArraysJVMTest.kt b/libraries/stdlib/test/collections/ArraysJVMTest.kt index cf89e7c7d76..4fe72d09029 100644 --- a/libraries/stdlib/test/collections/ArraysJVMTest.kt +++ b/libraries/stdlib/test/collections/ArraysJVMTest.kt @@ -201,13 +201,13 @@ class ArraysJVMTest { test fun orEmptyNull() { val x: Array? = null val xArray: Array = x.orEmpty() - expect(0) { xArray.size } + expect(0) { xArray.size() } } test fun orEmptyNotNull() { val x: Array? = array("1", "2") val xArray: Array = x.orEmpty() - expect(2) { xArray.size } + expect(2) { xArray.size() } expect("1") { xArray[0] } expect("2") { xArray[1] } } diff --git a/libraries/stdlib/test/collections/ArraysTest.kt b/libraries/stdlib/test/collections/ArraysTest.kt index 8a1fc07f6d6..c05ecf1d6cd 100644 --- a/libraries/stdlib/test/collections/ArraysTest.kt +++ b/libraries/stdlib/test/collections/ArraysTest.kt @@ -37,7 +37,7 @@ class ArraysTest { val arr = ByteArray(2) val expected: Byte = 0 - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(expected, arr[0]) assertEquals(expected, arr[1]) } @@ -46,7 +46,7 @@ class ArraysTest { val arr = ShortArray(2) val expected: Short = 0 - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(expected, arr[0]) assertEquals(expected, arr[1]) } @@ -54,7 +54,7 @@ class ArraysTest { test fun intArray() { val arr = IntArray(2) - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(0, arr[0]) assertEquals(0, arr[1]) } @@ -63,7 +63,7 @@ class ArraysTest { val arr = LongArray(2) val expected: Long = 0 - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(expected, arr[0]) assertEquals(expected, arr[1]) } @@ -72,7 +72,7 @@ class ArraysTest { val arr = FloatArray(2) val expected: Float = 0.0.toFloat() - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(expected, arr[0]) assertEquals(expected, arr[1]) } @@ -80,7 +80,7 @@ class ArraysTest { test fun doubleArray() { val arr = DoubleArray(2) - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(0.0, arr[0]) assertEquals(0.0, arr[1]) } @@ -89,14 +89,14 @@ class ArraysTest { val arr = CharArray(2) val expected: Char = '\u0000' - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(expected, arr[0]) assertEquals(expected, arr[1]) } test fun booleanArray() { val arr = BooleanArray(2) - assertEquals(arr.size, 2) + assertEquals(arr.size(), 2) assertEquals(false, arr[0]) assertEquals(false, arr[1]) } diff --git a/libraries/stdlib/test/collections/CollectionJVMTest.kt b/libraries/stdlib/test/collections/CollectionJVMTest.kt index 0ec08769e41..97f79dc9a34 100644 --- a/libraries/stdlib/test/collections/CollectionJVMTest.kt +++ b/libraries/stdlib/test/collections/CollectionJVMTest.kt @@ -18,14 +18,14 @@ class CollectionJVMTest { } - test fun filterIntolinkedListOf() { + test fun filterIntoLinkedList() { val data = arrayListOf("foo", "bar") val foo = data.filterTo(linkedListOf()) { it.startsWith("f") } assertTrue { foo.all { it.startsWith("f") } } - assertEquals(1, foo.size) + assertEquals(1, foo.size()) assertEquals(linkedListOf("foo"), foo) assertTrue { @@ -40,7 +40,7 @@ class CollectionJVMTest { assertTrue { foo.all { !it.startsWith("f") } } - assertEquals(1, foo.size) + assertEquals(1, foo.size()) assertEquals(linkedListOf("bar"), foo) assertTrue { @@ -52,7 +52,7 @@ class CollectionJVMTest { val data = arrayListOf(null, "foo", null, "bar") val foo = data.filterNotNullTo(linkedListOf()) - assertEquals(2, foo.size) + assertEquals(2, foo.size()) assertEquals(linkedListOf("foo", "bar"), foo) assertTrue { @@ -63,7 +63,7 @@ class CollectionJVMTest { test fun filterIntoSortedSet() { val data = arrayListOf("foo", "bar") val sorted = data.filterTo(sortedSetOf()) { it.length == 3 } - assertEquals(2, sorted.size) + assertEquals(2, sorted.size()) assertEquals(sortedSetOf("bar", "foo"), sorted) assertTrue { sorted is TreeSet @@ -93,7 +93,7 @@ class CollectionJVMTest { val data = arrayListOf("foo", "bar") val arr = data.toArray() println("Got array ${arr}") - assertEquals(2, arr.size) + assertEquals(2, arr.size()) todo { assertTrue { arr is Array diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 6866153808f..8100b53f72f 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -27,7 +27,7 @@ class CollectionTest { val data = arrayListOf(null, "foo", null, "bar") val foo = data.filterNotNull() - assertEquals(2, foo.size) + assertEquals(2, foo.size()) assertEquals(arrayListOf("foo", "bar"), foo) assertTrue { @@ -38,7 +38,7 @@ class CollectionTest { test fun mapNotNull() { val data = arrayListOf(null, "foo", null, "bar") val foo = data.mapNotNull { it.length } - assertEquals(2, foo.size) + assertEquals(2, foo.size()) assertEquals(arrayListOf(3, 3), foo) assertTrue { @@ -53,7 +53,7 @@ class CollectionTest { assertTrue { foo.all { it.startsWith("f") } } - assertEquals(1, foo.size) + assertEquals(1, foo.size()) assertEquals(hashSetOf("foo"), foo) assertTrue { @@ -83,7 +83,7 @@ class CollectionTest { test fun foldWithDifferentTypes() { expect(7) { val numbers = arrayListOf("a", "ab", "abc") - numbers.fold(1) { a, b -> a + b.size } + numbers.fold(1) { a, b -> a + b.length() } } expect("1234") { @@ -136,7 +136,7 @@ class CollectionTest { test fun partition() { val data = arrayListOf("foo", "bar", "something", "xyz") - val pair = data.partition { it.size == 3 } + val pair = data.partition { it.length() == 3 } assertEquals(arrayListOf("foo", "bar", "xyz"), pair.first, "pair.first") assertEquals(arrayListOf("something"), pair.second, "pair.second") @@ -181,7 +181,7 @@ class CollectionTest { assertEquals(4, listOfPairs[3].first) val l3 = byLength.getOrElse(3, { ArrayList() }) - assertEquals(2, l3.size) + assertEquals(2, l3.size()) } test fun plusRanges() { @@ -281,14 +281,14 @@ class CollectionTest { test fun takeWhile() { val coll = arrayListOf("foo", "bar", "abc") assertEquals(arrayListOf("foo"), coll.takeWhile { it.startsWith("f") }) - assertEquals(arrayListOf("foo", "bar", "abc"), coll.takeWhile { it.size == 3 }) + assertEquals(arrayListOf("foo", "bar", "abc"), coll.takeWhile { it.length() == 3 }) } test fun copyToArray() { val data = arrayListOf("foo", "bar") val arr = data.copyToArray() println("Got array ${arr}") - assertEquals(2, arr.size) + assertEquals(2, arr.size()) todo { assertTrue { arr is Array @@ -347,7 +347,7 @@ class CollectionTest { val indices = data.indices assertEquals(0, indices.start) assertEquals(1, indices.end) - assertEquals(indices, data.size.indices) + assertEquals(indices, data.size().indices) } test fun contains() { diff --git a/libraries/stdlib/test/collections/ImmutableArrayListTest.kt b/libraries/stdlib/test/collections/ImmutableArrayListTest.kt index ea29324dc35..a97d897ae98 100644 --- a/libraries/stdlib/test/collections/ImmutableArrayListTest.kt +++ b/libraries/stdlib/test/collections/ImmutableArrayListTest.kt @@ -19,7 +19,7 @@ class ImmutableArrayListTest() : TestCase() { fun testGet() { for (length in 0 .. 55) { val list = buildIntArray(length, 19) - assertEquals(length, list.size) + assertEquals(length, list.size()) checkList(list, length, 19) } } @@ -34,7 +34,7 @@ class ImmutableArrayListTest() : TestCase() { private fun checkList(list: List, expectedLength: Int, expectedFirstValue: Int) { - assertEquals(expectedLength, list.size) + assertEquals(expectedLength, list.size()) for (i in 0 .. expectedLength - 1) { assertEquals(expectedFirstValue + i, list[i]) } diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 2dbbf558d71..58ca9816691 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -107,7 +107,7 @@ abstract class IterableTests>(val data: T, val empty: T) { val foo = data.filter { it.startsWith("f") } expect(true) { foo is List } expect(true) { foo.all { it.startsWith("f") } } - expect(1) { foo.size } + expect(1) { foo.size() } assertEquals(listOf("foo"), foo) } @@ -115,7 +115,7 @@ abstract class IterableTests>(val data: T, val empty: T) { val notFoo = data.filterNot { it.startsWith("f") } expect(true) { notFoo is List } expect(true) { notFoo.none { it.startsWith("f") } } - expect(1) { notFoo.size } + expect(1) { notFoo.size() } assertEquals(listOf("bar"), notFoo) } @@ -159,7 +159,7 @@ abstract class IterableTests>(val data: T, val empty: T) { assertTrue { lengths.all { it == 3 } } - assertEquals(2, lengths.size) + assertEquals(2, lengths.size()) assertEquals(arrayListOf(3, 3), lengths) } @@ -220,7 +220,7 @@ abstract class IterableTests>(val data: T, val empty: T) { Test fun reduce() { val reduced = data.reduce {a, b -> a + b } - assertEquals(6, reduced.size) + assertEquals(6, reduced.length()) assertTrue(reduced == "foobar" || reduced == "barfoo") } } diff --git a/libraries/stdlib/test/collections/ListSpecificTest.kt b/libraries/stdlib/test/collections/ListSpecificTest.kt index 9258712fdbf..cf8eec67245 100644 --- a/libraries/stdlib/test/collections/ListSpecificTest.kt +++ b/libraries/stdlib/test/collections/ListSpecificTest.kt @@ -13,7 +13,7 @@ class ListSpecificTest { Test fun tail() { val data = arrayListOf("foo", "bar", "whatnot") - val actual = data.tail + val actual = data.drop(1) val expected = arrayListOf("bar", "whatnot") assertEquals(expected, actual) } @@ -30,14 +30,7 @@ class ListSpecificTest { } Test fun utils() { - assertNull(empty.head) - assertNull(empty.first) - assertNull(empty.last) assertEquals(-1, empty.lastIndex) - - assertEquals("foo", data.head) - assertEquals("foo", data.first) - assertEquals("bar", data.last) assertEquals(1, data.lastIndex) } diff --git a/libraries/stdlib/test/dom/DomBuilderTest.kt b/libraries/stdlib/test/dom/DomBuilderTest.kt index d41659a9ec4..c15ecd09e49 100644 --- a/libraries/stdlib/test/dom/DomBuilderTest.kt +++ b/libraries/stdlib/test/dom/DomBuilderTest.kt @@ -86,7 +86,7 @@ class DomBuilderTest() { } } - val grandChild = doc["grandChild"].first + val grandChild = doc["grandChild"].firstOrNull() if (grandChild != null) { assertEquals("Hello World!", grandChild.text) assertEquals(" bar tiny", grandChild.attribute("class")) @@ -96,7 +96,7 @@ class DomBuilderTest() { assertTrue(classSet.contains("bar")) assertTrue(classSet.contains("tiny")) - assertTrue(classSet.size == 2 ) + assertTrue(classSet.size() == 2 ) assertFalse(classSet.contains("doesNotExist")) // lets add a new class and some existing classes @@ -114,12 +114,12 @@ class DomBuilderTest() { } else { fail("Not an Element $grandChild") } - val child = doc["child"].first + val child = doc["child"].firstOrNull() if (child != null) { val gc1 = child.childElements("grandChild") - assertEquals(1, gc1.size, "Expected a single child but found $gc1") + assertEquals(1, gc1.size(), "Expected a single child but found $gc1") val gc2 = child.childElements("grandChild2") - assertEquals(1, gc2.size, "Expected a single child but found $gc2") + assertEquals(1, gc2.size(), "Expected a single child but found $gc2") } else { fail("No child found!") } diff --git a/libraries/stdlib/test/io/Files.kt b/libraries/stdlib/test/io/Files.kt index e52b4986590..1d8289874ac 100644 --- a/libraries/stdlib/test/io/Files.kt +++ b/libraries/stdlib/test/io/Files.kt @@ -15,7 +15,7 @@ class FilesTest { File.createTempFile("temp3", ".kt", dir) val result = dir.listFiles { it.getName().endsWith(".kt") } - assertEquals(2, result!!.size) + assertEquals(2, result!!.size()) } test fun recurse() { diff --git a/libraries/stdlib/test/js/JsArrayScript.kt b/libraries/stdlib/test/js/JsArrayScript.kt index edf1c437214..19c895bc139 100644 --- a/libraries/stdlib/test/js/JsArrayScript.kt +++ b/libraries/stdlib/test/js/JsArrayScript.kt @@ -5,7 +5,7 @@ fun testSize(): Int { val a2 = array("foo") val a3 = array("foo", "bar") - return a1.size + a2.size + a3.size + return a1.size() + a2.size() + a3.size() } fun testToListToString(): String { diff --git a/libraries/stdlib/test/js/JsArrayTest.kt b/libraries/stdlib/test/js/JsArrayTest.kt index a45c7b9799c..c9aaf78ab97 100644 --- a/libraries/stdlib/test/js/JsArrayTest.kt +++ b/libraries/stdlib/test/js/JsArrayTest.kt @@ -11,9 +11,9 @@ class JsArrayTest { val a2 = array("foo") val a3 = array("foo", "bar") - assertEquals(0, a1.size) - assertEquals(1, a2.size) - assertEquals(2, a3.size) + assertEquals(0, a1.size()) + assertEquals(1, a2.size()) + assertEquals(2, a3.size()) assertEquals("[]", a1.toList().toString()) assertEquals("[foo]", a2.toList().toString()) @@ -25,7 +25,7 @@ class JsArrayTest { var c: Collection = array("A", "B", "C").toList() var a = ArrayList(c) - assertEquals(3, a.size) + assertEquals(3, a.size()) assertEquals("A", a[0]) assertEquals("B", a[1]) assertEquals("C", a[2]) diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index 96d86e1e70d..3f3c5e6bd61 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt @@ -138,8 +138,8 @@ abstract class MapJsTest { assertFalse(data.isEmpty()) assertFalse(data.empty) - assertEquals(KEYS.size, data.size()) - assertEquals(KEYS.size, data.size) + assertEquals(KEYS.size(), data.size()) + assertEquals(KEYS.size(), data.size) } // #KT-3035 @@ -200,7 +200,7 @@ abstract class MapJsTest { val map = createTestMap() val newMap = emptyMutableMap() newMap.putAll(map) - assertEquals(KEYS.size, newMap.size) + assertEquals(KEYS.size(), newMap.size) } test fun mapRemove() { diff --git a/libraries/stdlib/test/text/StringJVMTest.kt b/libraries/stdlib/test/text/StringJVMTest.kt index dd2ffd68731..b4b060634fc 100644 --- a/libraries/stdlib/test/text/StringJVMTest.kt +++ b/libraries/stdlib/test/text/StringJVMTest.kt @@ -48,14 +48,14 @@ class StringJVMTest { test fun testSplitByChar() { val s = "ab\n[|^$&\\]^cd" var list = s.split('b'); - assertEquals(2, list.size) + assertEquals(2, list.size()) assertEquals("a", list[0]) assertEquals("\n[|^$&\\]^cd", list[1]) list = s.split('^') - assertEquals(3, list.size) + assertEquals(3, list.size()) assertEquals("cd", list[2]) list = s.split('.') - assertEquals(1, list.size) + assertEquals(1, list.size()) assertEquals(s, list[0]) } @@ -113,14 +113,14 @@ class StringJVMTest { test fun find() { val data = "a1b2c3" - assertEquals('1', data.find { it.isDigit() }) - assertNull(data.find { it.isUpperCase() }) + assertEquals('1', data.first { it.isDigit() }) + assertNull(data.firstOrNull { it.isUpperCase() }) } test fun findNot() { val data = "1a2b3c" - assertEquals('a', data.findNot { it.isDigit() }) - assertNull(data.findNot { it.isJavaLetterOrDigit() }) + assertEquals('a', data.filterNot { it.isDigit() }.firstOrNull()) + assertNull(data.filterNot { it.isJavaLetterOrDigit() }.firstOrNull()) } test fun partition() { @@ -165,7 +165,7 @@ class StringJVMTest { test fun flatMap() { val data = "abcd" val result = data.flatMap { listOf(it) } - assertEquals(data.size, result.count()) + assertEquals(data.length(), result.count()) assertEquals(data.toCharList(), result) } diff --git a/libraries/stdlib/validator/src/test/kotlin/NoInternalVisibilityInStdLibTest.kt b/libraries/stdlib/validator/src/test/kotlin/NoInternalVisibilityInStdLibTest.kt index b49240074de..b1c0c99cc60 100644 --- a/libraries/stdlib/validator/src/test/kotlin/NoInternalVisibilityInStdLibTest.kt +++ b/libraries/stdlib/validator/src/test/kotlin/NoInternalVisibilityInStdLibTest.kt @@ -85,7 +85,7 @@ class NoInternalVisibilityInStdLibTest { val byPackage = byFile.keySet().groupBy { it.getPackageFqName() } val message = StringBuilder { - appendln("There are ${internalDescriptors.size} descriptors that have internal visibility:") + appendln("There are ${internalDescriptors.size()} descriptors that have internal visibility:") for ((packageFqName, files) in byPackage) { appendln("In package ${packageFqName}:") diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt index 73a9c8eef0f..f3cf3f0ddd9 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Aggregates.kt @@ -95,8 +95,11 @@ fun aggregates(): List { return count """ } - body(Strings, Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { - "return size" + body(Strings) { + "return length()" + } + body(Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { + "return size()" } } @@ -158,7 +161,7 @@ fun aggregates(): List { } body(ArraysOfObjects, ArraysOfPrimitives) { """ - if (size == 0) return null + if (size() == 0) return null var minElem = this[0] var minValue = f(minElem) @@ -330,7 +333,7 @@ fun aggregates(): List { returns("R") body { """ - var index = size - 1 + var index = lastIndex var accumulator = initial while (index >= 0) { accumulator = operation(get(index--), accumulator) @@ -368,7 +371,7 @@ fun aggregates(): List { returns("T") body { """ - var index = size - 1 + var index = lastIndex if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced") var accumulator = get(index--) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 3e0a1435742..9c194bc5e96 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -10,7 +10,7 @@ fun arrays(): List { doc { "Returns true if the array is empty" } returns("Boolean") body { - "return size == 0" + "return size() == 0" } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt index 0451084a52b..b8afcdde231 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt @@ -159,7 +159,7 @@ fun elements(): List { """ when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") else return this[0] as T @@ -175,7 +175,7 @@ fun elements(): List { } body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { """ - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") return this[0] """ @@ -188,7 +188,7 @@ fun elements(): List { """ when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) return null else return this[0] as T @@ -204,7 +204,7 @@ fun elements(): List { } body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { """ - return if (size > 0) this[0] else null + return if (isEmpty()) null else this[0] """ } } @@ -242,10 +242,10 @@ fun elements(): List { """ when (this) { is List<*> -> { - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") else - return this[size - 1] as T + return this[this.lastIndex] as T } else -> { val iterator = iterator() @@ -259,11 +259,22 @@ fun elements(): List { } """ } + body(Streams) { + """ + val iterator = iterator() + if (!iterator.hasNext()) + throw NoSuchElementException("Collection is empty") + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last + """ + } body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { """ - if (size == 0) + if (isEmpty()) throw NoSuchElementException("Collection is empty") - return this[size - 1] + return this[lastIndex] """ } } @@ -274,7 +285,7 @@ fun elements(): List { body { """ when (this) { - is List<*> -> return if (size > 0) this[size - 1] as T else null + is List<*> -> return if (isEmpty()) null else this[size() - 1] as T else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -287,9 +298,25 @@ fun elements(): List { } """ } - body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(Streams) { """ - return if (size > 0) this[size - 1] else null + val iterator = iterator() + if (!iterator.hasNext()) + return null + var last = iterator.next() + while (iterator.hasNext()) + last = iterator.next() + return last + """ + } + body(Strings) { + """ + return if (isEmpty()) null else this[length() - 1] + """ + } + body(Lists, ArraysOfObjects, ArraysOfPrimitives) { + """ + return if (isEmpty()) null else this[size() - 1] """ } } @@ -331,14 +358,13 @@ fun elements(): List { } } - val bucks = '$' templates add f("single()") { doc { "Returns single element, or throws exception if there is no or more than one element" } returns("T") body { """ when (this) { - is List<*> -> return when (size) { + is List<*> -> return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] as T else -> throw IllegalArgumentException("Collection has more than one element") @@ -355,9 +381,18 @@ fun elements(): List { } """ } - body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(Strings) { """ - return when (size) { + return when (length()) { + 0 -> throw NoSuchElementException("Collection is empty") + 1 -> this[0] + else -> throw IllegalArgumentException("Collection has more than one element") + } + """ + } + body(Lists, ArraysOfObjects, ArraysOfPrimitives) { + """ + return when (size()) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") @@ -372,7 +407,7 @@ fun elements(): List { body { """ when (this) { - is List<*> -> return if (size == 1) this[0] as T else null + is List<*> -> return if (size() == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) @@ -385,9 +420,14 @@ fun elements(): List { } """ } - body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) { + body(Strings) { """ - return if (size == 1) this[0] else null + return if (length() == 1) this[0] else null + """ + } + body(Lists, ArraysOfObjects, ArraysOfPrimitives) { + """ + return if (size() == 1) this[0] else null """ } } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index 61e65b31e11..94be3ef24b3 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -27,16 +27,16 @@ fun filtering(): List { """ } - body(Strings) { "return substring(Math.min(n, size))" } + body(Strings) { "return substring(Math.min(n, length()))" } returns(Strings) { "String" } body(Collections, ArraysOfObjects, ArraysOfPrimitives) { """ - if (n >= size) + if (n >= size()) return ArrayList() var count = 0 - val list = ArrayList(size - n) + val list = ArrayList(size() - n) for (item in this) { if (count++ >= n) list.add(item) } @@ -61,7 +61,7 @@ fun filtering(): List { """ } - body(Strings) { "return substring(0, Math.min(n, size))" } + body(Strings) { "return substring(0, Math.min(n, length()))" } returns(Strings) { "String" } doc(Streams) { "Returns a stream containing first *n* elements" } @@ -76,7 +76,7 @@ fun filtering(): List { body(Collections, ArraysOfObjects, ArraysOfPrimitives) { """ var count = 0 - val realN = if (n > size) size else n + val realN = if (n > size()) size() else n val list = ArrayList(realN) for (item in this) { if (count++ == realN) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt index 6009f4535ec..a2908c73597 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Sets.kt @@ -19,7 +19,7 @@ fun sets(): List { } body(ArraysOfObjects, ArraysOfPrimitives) { """ - val set = LinkedHashSet(size) + val set = LinkedHashSet(size()) for (item in this) set.add(item) return set """ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt index 4692853aa65..f9da0307009 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Snapshots.kt @@ -52,7 +52,7 @@ fun snapshots(): List { */ body(ArraysOfObjects, ArraysOfPrimitives) { """ - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list """ @@ -65,7 +65,7 @@ fun snapshots(): List { returns("List>") body { """ - val result = ArrayList>(size) + val result = ArrayList>(size()) for (item in this) result.add(item.key to item.value) return result @@ -93,7 +93,7 @@ fun snapshots(): List { */ body(ArraysOfPrimitives) { """ - val list = ArrayList(size) + val list = ArrayList(size()) for (item in this) list.add(item) return list """ diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 41844500e07..582deb28686 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -19,10 +19,10 @@ fun specialJVM(): List { doc { "Returns new array which is a copy of the original array" } returns("SELF") body { - "return Arrays.copyOf(this, size)" + "return Arrays.copyOf(this, size())" } body(ArraysOfObjects) { - "return Arrays.copyOf(this, size) as Array" + "return Arrays.copyOf(this, size()) as Array" } } @@ -53,7 +53,7 @@ fun specialJVM(): List { } } - templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") { + templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size())") { only(ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) doc { "Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted." } @@ -63,7 +63,7 @@ fun specialJVM(): List { } } - templates add f("sort(fromIndex: Int = 0, toIndex: Int = size)") { + templates add f("sort(fromIndex: Int = 0, toIndex: Int = size())") { only(ArraysOfObjects, ArraysOfPrimitives) exclude(PrimitiveType.Boolean) doc { "Sorts array or range in array inplace" }