@file:kotlin.jvm.JvmMultifileClass @file:kotlin.jvm.JvmName("CollectionsKt") package kotlin.collections internal object EmptyIterator : ListIterator { override fun hasNext(): Boolean = false override fun hasPrevious(): Boolean = false override fun nextIndex(): Int = 0 override fun previousIndex(): Int = -1 override fun next(): Nothing = throw NoSuchElementException() override fun previous(): Nothing = throw NoSuchElementException() } internal object EmptyList : List, java.io.Serializable, RandomAccess { private const val serialVersionUID: Long = -7390468764508069838L override fun equals(other: Any?): Boolean = other is List<*> && other.isEmpty() override fun hashCode(): Int = 1 override fun toString(): String = "[]" override val size: Int get() = 0 override fun isEmpty(): Boolean = true override fun contains(element: Nothing): Boolean = false override fun containsAll(elements: Collection): Boolean = elements.isEmpty() override fun get(index: Int): Nothing = throw IndexOutOfBoundsException("Empty list doesn't contain element at index $index.") override fun indexOf(element: Nothing): Int = -1 override fun lastIndexOf(element: Nothing): Int = -1 override fun iterator(): Iterator = EmptyIterator override fun listIterator(): ListIterator = EmptyIterator override fun listIterator(index: Int): ListIterator { if (index != 0) throw IndexOutOfBoundsException("Index: $index") return EmptyIterator } override fun subList(fromIndex: Int, toIndex: Int): List { if (fromIndex == 0 && toIndex == 0) return this throw IndexOutOfBoundsException("fromIndex: $fromIndex, toIndex: $toIndex") } private fun readResolve(): Any = EmptyList } public fun emptyList(): List = EmptyList public fun listOf(vararg elements: T): List = if (elements.size > 0) elements.asList() else emptyList() public inline fun listOf(): List = emptyList() public inline fun mutableListOf(): MutableList = ArrayList() public inline fun arrayListOf(): ArrayList = ArrayList() public fun mutableListOf(vararg elements: T): MutableList = kotlin.UnsupportedOperationException("This is intrinsic") public fun arrayListOf(vararg elements: T): ArrayList = kotlin.UnsupportedOperationException("This is intrinsic") public fun listOfNotNull(element: T?): List = if (element != null) listOf(element) else emptyList() public inline fun List(size: Int, init: (index: Int) -> T): List = MutableList(size, init) public inline fun MutableList(size: Int, init: (index: Int) -> T): MutableList { val list = ArrayList(size) repeat(size) { index -> list.add(init(index)) } return list } public val Collection<*>.indices: IntRange get() = 0..size - 1 public val List.lastIndex: Int get() = this.size - 1 public inline fun Collection.isNotEmpty(): Boolean = !isEmpty() public inline fun List.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T { return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index) } public fun List.first(): T { if (isEmpty()) throw NoSuchElementException("List is empty.") return this[0] } public fun Iterable.single(): T { when (this) { is List -> return this.single() else -> { val iterator = iterator() if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty.") val single = iterator.next() if (iterator.hasNext()) throw IllegalArgumentException("Collection has more than one element.") return single } } } public fun List.single(): T { return when (size) { 0 -> throw NoSuchElementException("List is empty.") 1 -> this[0] else -> throw IllegalArgumentException("List has more than one element.") } } public fun Iterable.toList(): List { if (this is Collection) { return when (size) { 0 -> emptyList() 1 -> listOf(if (this is List) get(0) else iterator().next()) else -> this.toMutableList() } } return this.toMutableList().optimizeReadOnlyList() } public fun Iterable.toMutableList(): MutableList { if (this is Collection) return this.toMutableList() return toCollection(ArrayList()) } public fun Collection.toMutableList(): MutableList { return ArrayList(this) } public fun > Iterable.toCollection(destination: C): C { for (item in this) { destination.add(item) } return destination } public inline fun Iterable.map(transform: (T) -> R): List { return mapTo(ArrayList(if (this is Collection<*>) this.size else 10), transform) } public inline fun > Iterable.mapTo(destination: C, transform: (T) -> R): C { for (item in this) destination.add(transform(item)) return destination } internal fun List.optimizeReadOnlyList() = when (size) { 0 -> emptyList() 1 -> listOf(this[0]) else -> this } public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean { if (this is Collection && isEmpty()) return true for (element in this) if (!predicate(element)) return false return true } public fun Iterable.joinTo(buffer: A, separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): A { buffer.append(prefix) var count = 0 for (element in this) { if (++count > 1) buffer.append(separator) if (limit < 0 || count <= limit) { buffer.appendElement(element, transform) } else break } if (limit >= 0 && count > limit) buffer.append(truncated) buffer.append(postfix) return buffer } private fun Appendable.appendElement(element: T, transform: ((T) -> CharSequence)?) { when { transform != null -> append(transform(element)) element is CharSequence? -> append(element) element is Char -> append(element) else -> append(element.toString()) } } public fun Iterable.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = "", limit: Int = -1, truncated: CharSequence = "...", transform: ((T) -> CharSequence)? = null): String { return joinTo(StringBuilder(), separator, prefix, postfix, limit, truncated, transform).toString() }