package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // import java.util.* /** * Returns true if *element* is found in the collection */ public fun Array.contains(element: T): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun BooleanArray.contains(element: Boolean): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun ByteArray.contains(element: Byte): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun CharArray.contains(element: Char): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun DoubleArray.contains(element: Double): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun FloatArray.contains(element: Float): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun IntArray.contains(element: Int): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun LongArray.contains(element: Long): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun ShortArray.contains(element: Short): Boolean { return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun Iterable.contains(element: T): Boolean { if (this is Collection<*>) return contains(element) return indexOf(element) >= 0 } /** * Returns true if *element* is found in the collection */ public fun Stream.contains(element: T): Boolean { if (this is Collection<*>) return contains(element) return indexOf(element) >= 0 } /** * Returns element at given *index* */ public fun Array.elementAt(index: Int): T { return get(index) } /** * Returns element at given *index* */ public fun BooleanArray.elementAt(index: Int): Boolean { return get(index) } /** * Returns element at given *index* */ public fun ByteArray.elementAt(index: Int): Byte { return get(index) } /** * Returns element at given *index* */ public fun CharArray.elementAt(index: Int): Char { return get(index) } /** * Returns element at given *index* */ public fun DoubleArray.elementAt(index: Int): Double { return get(index) } /** * Returns element at given *index* */ public fun FloatArray.elementAt(index: Int): Float { return get(index) } /** * Returns element at given *index* */ public fun IntArray.elementAt(index: Int): Int { return get(index) } /** * Returns element at given *index* */ public fun LongArray.elementAt(index: Int): Long { return get(index) } /** * Returns element at given *index* */ public fun ShortArray.elementAt(index: Int): Short { return get(index) } /** * Returns element at given *index* */ public fun Iterable.elementAt(index: Int): T { if (this is List<*>) return get(index) as T val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } throw IndexOutOfBoundsException("Collection doesn't contain element at index") } /** * Returns element at given *index* */ public fun List.elementAt(index: Int): T { return get(index) } /** * Returns element at given *index* */ public fun Stream.elementAt(index: Int): T { val iterator = iterator() var count = 0 while (iterator.hasNext()) { val element = iterator.next() if (index == count++) return element } throw IndexOutOfBoundsException("Collection doesn't contain element at index") } /** * Returns element at given *index* */ public fun String.elementAt(index: Int): Char { return get(index) } /** * Returns first element */ public fun Array.first(): T { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun BooleanArray.first(): Boolean { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun ByteArray.first(): Byte { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun CharArray.first(): Char { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun DoubleArray.first(): Double { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun FloatArray.first(): Float { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun IntArray.first(): Int { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun LongArray.first(): Long { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun ShortArray.first(): Short { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun Iterable.first(): T { when (this) { is List<*> -> { if (size == 0) throw NoSuchElementException("Collection is empty") else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") return iterator.next() } } } /** * Returns first element */ public fun List.first(): T { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element */ public fun Stream.first(): T { when (this) { is List<*> -> { if (size == 0) throw NoSuchElementException("Collection is empty") else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") return iterator.next() } } } /** * Returns first element */ public fun String.first(): Char { if (size == 0) throw NoSuchElementException("Collection is empty") return this[0] } /** * Returns first element matching the given *predicate* */ public inline fun Array.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun BooleanArray.first(predicate: (Boolean) -> Boolean): Boolean { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun ByteArray.first(predicate: (Byte) -> Boolean): Byte { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun CharArray.first(predicate: (Char) -> Boolean): Char { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun DoubleArray.first(predicate: (Double) -> Boolean): Double { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun FloatArray.first(predicate: (Float) -> Boolean): Float { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun IntArray.first(predicate: (Int) -> Boolean): Int { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun LongArray.first(predicate: (Long) -> Boolean): Long { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun ShortArray.first(predicate: (Short) -> Boolean): Short { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun Iterable.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun Stream.first(predicate: (T) -> Boolean): T { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element matching the given *predicate* */ public inline fun String.first(predicate: (Char) -> Boolean): Char { for (element in this) if (predicate(element)) return element throw NoSuchElementException("No element matching predicate was found") } /** * Returns first element, or null if collection is empty */ public fun Array.firstOrNull(): T? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun BooleanArray.firstOrNull(): Boolean? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun ByteArray.firstOrNull(): Byte? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun CharArray.firstOrNull(): Char? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun DoubleArray.firstOrNull(): Double? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun FloatArray.firstOrNull(): Float? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun IntArray.firstOrNull(): Int? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun LongArray.firstOrNull(): Long? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun ShortArray.firstOrNull(): Short? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun Iterable.firstOrNull(): T? { when (this) { is List<*> -> { if (size == 0) return null else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) return null return iterator.next() } } } /** * Returns first element, or null if collection is empty */ public fun List.firstOrNull(): T? { return if (size > 0) this[0] else null } /** * Returns first element, or null if collection is empty */ public fun Stream.firstOrNull(): T? { when (this) { is List<*> -> { if (size == 0) return null else return this[0] as T } else -> { val iterator = iterator() if (!iterator.hasNext()) return null return iterator.next() } } } /** * Returns first element, or null if collection is empty */ public fun String.firstOrNull(): Char? { return if (size > 0) this[0] else null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun Array.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun BooleanArray.firstOrNull(predicate: (Boolean) -> Boolean): Boolean? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun ByteArray.firstOrNull(predicate: (Byte) -> Boolean): Byte? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun DoubleArray.firstOrNull(predicate: (Double) -> Boolean): Double? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun FloatArray.firstOrNull(predicate: (Float) -> Boolean): Float? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun IntArray.firstOrNull(predicate: (Int) -> Boolean): Int? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun LongArray.firstOrNull(predicate: (Long) -> Boolean): Long? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun ShortArray.firstOrNull(predicate: (Short) -> Boolean): Short? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun Iterable.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun Stream.firstOrNull(predicate: (T) -> Boolean): T? { for (element in this) if (predicate(element)) return element return null } /** * Returns first element matching the given *predicate*, or *null* if element was not found */ public inline fun String.firstOrNull(predicate: (Char) -> Boolean): Char? { for (element in this) if (predicate(element)) return element return null } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun Array.indexOf(element: T): Int { if (element == null) { for (index in indices) { if (this[index] == null) { return index } } } else { for (index in indices) { if (element == this[index]) { return index } } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun BooleanArray.indexOf(element: Boolean): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun ByteArray.indexOf(element: Byte): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun CharArray.indexOf(element: Char): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun DoubleArray.indexOf(element: Double): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun FloatArray.indexOf(element: Float): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun IntArray.indexOf(element: Int): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun LongArray.indexOf(element: Long): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun ShortArray.indexOf(element: Short): Int { for (index in indices) { if (element == this[index]) { return index } } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun Iterable.indexOf(element: T): Int { var index = 0 for (item in this) { if (element == item) return index index++ } return -1 } /** * Returns first index of *element*, or -1 if the collection does not contain element */ public fun Stream.indexOf(element: T): Int { var index = 0 for (item in this) { if (element == item) return index index++ } return -1 } /** * Returns last element */ public fun Array.last(): T { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun BooleanArray.last(): Boolean { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun ByteArray.last(): Byte { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun CharArray.last(): Char { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun DoubleArray.last(): Double { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun FloatArray.last(): Float { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun IntArray.last(): Int { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun LongArray.last(): Long { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun ShortArray.last(): Short { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element */ public fun Iterable.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 } } } /** * Returns last element */ public fun List.last(): T { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * 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 } } } /** * Returns last element */ public fun String.last(): Char { if (size == 0) throw NoSuchElementException("Collection is empty") return this[size - 1] } /** * Returns last element matching the given *predicate* */ public inline fun Array.last(predicate: (T) -> Boolean): T { var last: T? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as T } /** * Returns last element matching the given *predicate* */ public inline fun BooleanArray.last(predicate: (Boolean) -> Boolean): Boolean { var last: Boolean? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Boolean } /** * Returns last element matching the given *predicate* */ public inline fun ByteArray.last(predicate: (Byte) -> Boolean): Byte { var last: Byte? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Byte } /** * Returns last element matching the given *predicate* */ public inline fun CharArray.last(predicate: (Char) -> Boolean): Char { var last: Char? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Char } /** * Returns last element matching the given *predicate* */ public inline fun DoubleArray.last(predicate: (Double) -> Boolean): Double { var last: Double? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Double } /** * Returns last element matching the given *predicate* */ public inline fun FloatArray.last(predicate: (Float) -> Boolean): Float { var last: Float? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Float } /** * Returns last element matching the given *predicate* */ public inline fun IntArray.last(predicate: (Int) -> Boolean): Int { var last: Int? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Int } /** * Returns last element matching the given *predicate* */ public inline fun LongArray.last(predicate: (Long) -> Boolean): Long { var last: Long? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Long } /** * Returns last element matching the given *predicate* */ public inline fun ShortArray.last(predicate: (Short) -> Boolean): Short { var last: Short? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Short } /** * Returns last element matching the given *predicate* */ public inline fun Iterable.last(predicate: (T) -> Boolean): T { var last: T? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as T } /** * Returns last element matching the given *predicate* */ public inline fun Stream.last(predicate: (T) -> Boolean): T { var last: T? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as T } /** * Returns last element matching the given *predicate* */ public inline fun String.last(predicate: (Char) -> Boolean): Char { var last: Char? = null var found = false for (element in this) { if (predicate(element)) { last = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return last as Char } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Array.lastIndexOf(element: T): Int { if (element == null) { for (index in indices.reverse()) { if (this[index] == null) { return index } } } else { for (index in indices.reverse()) { if (element == this[index]) { return index } } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun BooleanArray.lastIndexOf(element: Boolean): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun ByteArray.lastIndexOf(element: Byte): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun CharArray.lastIndexOf(element: Char): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun DoubleArray.lastIndexOf(element: Double): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun FloatArray.lastIndexOf(element: Float): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun IntArray.lastIndexOf(element: Int): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun LongArray.lastIndexOf(element: Long): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun ShortArray.lastIndexOf(element: Short): Int { for (index in indices.reverse()) { if (element == this[index]) { return index } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Iterable.lastIndexOf(element: T): Int { var lastIndex = -1 var index = 0 for (item in this) { if (element == item) lastIndex = index index++ } return lastIndex } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun List.lastIndexOf(element: T): Int { if (element == null) { for (index in indices.reverse()) { if (this[index] == null) { return index } } } else { for (index in indices.reverse()) { if (element == this[index]) { return index } } } return -1 } /** * Returns last index of *element*, or -1 if the collection does not contain element */ public fun Stream.lastIndexOf(element: T): Int { var lastIndex = -1 var index = 0 for (item in this) { if (element == item) lastIndex = index index++ } return lastIndex } /** * Returns last element, or null if collection is empty */ public fun Array.lastOrNull(): T? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun BooleanArray.lastOrNull(): Boolean? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun ByteArray.lastOrNull(): Byte? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun CharArray.lastOrNull(): Char? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun DoubleArray.lastOrNull(): Double? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun FloatArray.lastOrNull(): Float? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun IntArray.lastOrNull(): Int? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun LongArray.lastOrNull(): Long? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun ShortArray.lastOrNull(): Short? { return if (size > 0) this[size - 1] else null } /** * Returns last element, or null if collection is empty */ public fun Iterable.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 } } } /** * Returns last element, or null if collection is empty */ public fun List.lastOrNull(): T? { return if (size > 0) this[size - 1] else null } /** * 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 } } } /** * Returns last element, or null if collection is empty */ public fun String.lastOrNull(): Char? { return if (size > 0) this[size - 1] else null } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun Array.lastOrNull(predicate: (T) -> Boolean): T? { var last: T? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun BooleanArray.lastOrNull(predicate: (Boolean) -> Boolean): Boolean? { var last: Boolean? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun ByteArray.lastOrNull(predicate: (Byte) -> Boolean): Byte? { var last: Byte? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun CharArray.lastOrNull(predicate: (Char) -> Boolean): Char? { var last: Char? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun DoubleArray.lastOrNull(predicate: (Double) -> Boolean): Double? { var last: Double? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun FloatArray.lastOrNull(predicate: (Float) -> Boolean): Float? { var last: Float? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun IntArray.lastOrNull(predicate: (Int) -> Boolean): Int? { var last: Int? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun LongArray.lastOrNull(predicate: (Long) -> Boolean): Long? { var last: Long? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun ShortArray.lastOrNull(predicate: (Short) -> Boolean): Short? { var last: Short? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun Iterable.lastOrNull(predicate: (T) -> Boolean): T? { var last: T? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun Stream.lastOrNull(predicate: (T) -> Boolean): T? { var last: T? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns last element matching the given *predicate*, or null if element was not found */ public inline fun String.lastOrNull(predicate: (Char) -> Boolean): Char? { var last: Char? = null for (element in this) { if (predicate(element)) { last = element } } return last } /** * Returns single element, or throws exception if there is no or more than one element */ public fun Array.single(): T { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun BooleanArray.single(): Boolean { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun ByteArray.single(): Byte { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun CharArray.single(): Char { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun DoubleArray.single(): Double { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun FloatArray.single(): Float { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun IntArray.single(): Int { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun LongArray.single(): Long { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun ShortArray.single(): Short { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun Iterable.single(): T { when (this) { 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") } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var single = iterator.next() if (iterator.hasNext()) throw IllegalArgumentException("Collection has more than one element") return single } } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun List.single(): T { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun Stream.single(): T { when (this) { 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") } else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty") var single = iterator.next() if (iterator.hasNext()) throw IllegalArgumentException("Collection has more than one element") return single } } } /** * Returns single element, or throws exception if there is no or more than one element */ public fun String.single(): Char { return when (size) { 0 -> throw NoSuchElementException("Collection is empty") 1 -> this[0] else -> throw IllegalArgumentException("Collection has more than one element") } } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun Array.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun BooleanArray.single(predicate: (Boolean) -> Boolean): Boolean { var single: Boolean? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Boolean } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun ByteArray.single(predicate: (Byte) -> Boolean): Byte { var single: Byte? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Byte } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun CharArray.single(predicate: (Char) -> Boolean): Char { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Char } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun DoubleArray.single(predicate: (Double) -> Boolean): Double { var single: Double? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Double } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun FloatArray.single(predicate: (Float) -> Boolean): Float { var single: Float? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Float } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun IntArray.single(predicate: (Int) -> Boolean): Int { var single: Int? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Int } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun LongArray.single(predicate: (Long) -> Boolean): Long { var single: Long? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Long } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun ShortArray.single(predicate: (Short) -> Boolean): Short { var single: Short? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as Short } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun Iterable.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun Stream.single(predicate: (T) -> Boolean): T { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as T } /** * Returns single element matching the given *predicate*, or throws exception if there is no or more than one element */ public inline fun String.single(predicate: (Char) -> Boolean): Char { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) throw IllegalArgumentException("Collection contains more than one matching element") single = element found = true } } if (!found) throw NoSuchElementException("Collection doesn't contain any element matching predicate") return single as 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 } /** * 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 } /** * 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 } /** * 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 } /** * 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 } /** * 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 } /** * 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 } /** * 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 } /** * 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 } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun Iterable.singleOrNull(): T? { when (this) { is List<*> -> return if (size == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) return null return single } } } /** * 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 } /** * Returns single element, or null if collection is empty, or throws exception if there is more than one element */ public fun Stream.singleOrNull(): T? { when (this) { is List<*> -> return if (size == 1) this[0] as T else null else -> { val iterator = iterator() if (!iterator.hasNext()) return null var single = iterator.next() if (iterator.hasNext()) return null return single } } } /** * 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 } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun Array.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun BooleanArray.singleOrNull(predicate: (Boolean) -> Boolean): Boolean? { var single: Boolean? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun ByteArray.singleOrNull(predicate: (Byte) -> Boolean): Byte? { var single: Byte? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun CharArray.singleOrNull(predicate: (Char) -> Boolean): Char? { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun DoubleArray.singleOrNull(predicate: (Double) -> Boolean): Double? { var single: Double? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun FloatArray.singleOrNull(predicate: (Float) -> Boolean): Float? { var single: Float? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun IntArray.singleOrNull(predicate: (Int) -> Boolean): Int? { var single: Int? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun LongArray.singleOrNull(predicate: (Long) -> Boolean): Long? { var single: Long? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun ShortArray.singleOrNull(predicate: (Short) -> Boolean): Short? { var single: Short? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun Iterable.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun Stream.singleOrNull(predicate: (T) -> Boolean): T? { var single: T? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single } /** * Returns single element matching the given *predicate*, or null if element was not found or more than one elements were found */ public inline fun String.singleOrNull(predicate: (Char) -> Boolean): Char? { var single: Char? = null var found = false for (element in this) { if (predicate(element)) { if (found) return null single = element found = true } } if (!found) return null return single }