All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.kotlin.collections.CollectionsJVM.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")

package kotlin.collections

import kotlin.collections.builders.ListBuilder
import kotlin.internal.InlineOnly
import kotlin.internal.apiVersionIsAtLeast

/**
 * Returns a new read-only list containing only the specified object [element].
 *
 * The returned list is serializable.
 *
 * @sample samples.collections.Collections.Lists.singletonReadOnlyList
 */
public actual fun  listOf(element: T): List = java.util.Collections.singletonList(element)

@PublishedApi
@SinceKotlin("1.3")
@kotlin.internal.InlineOnly
internal actual inline fun  buildListInternal(builderAction: MutableList.() -> Unit): List {
    return build(createListBuilder().apply(builderAction))
}

@PublishedApi
@SinceKotlin("1.3")
@kotlin.internal.InlineOnly
internal actual inline fun  buildListInternal(capacity: Int, builderAction: MutableList.() -> Unit): List {
    return build(createListBuilder(capacity).apply(builderAction))
}

@PublishedApi
@SinceKotlin("1.3")
internal fun  createListBuilder(): MutableList {
    return ListBuilder()
}

@PublishedApi
@SinceKotlin("1.3")
internal fun  createListBuilder(capacity: Int): MutableList {
    return ListBuilder(capacity)
}

@PublishedApi
@SinceKotlin("1.3")
internal fun  build(builder: MutableList): List {
    return (builder as ListBuilder).build()
}

/**
 * Returns a list containing the elements returned by this enumeration
 * in the order they are returned by the enumeration.
 * @sample samples.collections.Collections.Lists.listFromEnumeration
 */
@kotlin.internal.InlineOnly
public inline fun  java.util.Enumeration.toList(): List = java.util.Collections.list(this)


/**
 * Returns a new list with the elements of this collection randomly shuffled.
 */
@SinceKotlin("1.2")
public actual fun  Iterable.shuffled(): List = toMutableList().apply { shuffle() }

/**
 * Returns a new list with the elements of this list randomly shuffled
 * using the specified [random] instance as the source of randomness.
 */
@SinceKotlin("1.2")
public fun  Iterable.shuffled(random: java.util.Random): List = toMutableList().apply { shuffle(random) }


@Suppress("DEPRECATION_ERROR")
@kotlin.internal.InlineOnly
internal actual inline fun collectionToArray(collection: Collection<*>): Array =
    kotlin.jvm.internal.collectionToArray(collection)

@kotlin.internal.InlineOnly
@Suppress("UNCHECKED_CAST", "DEPRECATION_ERROR")
internal actual inline fun  collectionToArray(collection: Collection<*>, array: Array): Array =
    kotlin.jvm.internal.collectionToArray(collection, array as Array) as Array

internal actual fun  terminateCollectionToArray(collectionSize: Int, array: Array): Array {
    if (collectionSize < array.size) {
        @Suppress("UNCHECKED_CAST")
        array[collectionSize] = null as T // null-terminate
    }
    return array
}

// copies typed varargs array to array of objects
internal actual fun  Array.copyToArrayOfAny(isVarargs: Boolean): Array =
    if (isVarargs && this.javaClass == Array::class.java)
    // if the array came from varargs and already is array of Any, copying isn't required
        @Suppress("UNCHECKED_CAST") (this as Array)
    else
        java.util.Arrays.copyOf(this, this.size, Array::class.java)


@PublishedApi
@SinceKotlin("1.3")
@InlineOnly
internal actual inline fun checkIndexOverflow(index: Int): Int {
    if (index < 0) {
        if (apiVersionIsAtLeast(1, 3, 0))
            throwIndexOverflow()
        else
            throw ArithmeticException("Index overflow has happened.")
    }
    return index
}

@PublishedApi
@SinceKotlin("1.3")
@InlineOnly
internal actual inline fun checkCountOverflow(count: Int): Int {
    if (count < 0) {
        if (apiVersionIsAtLeast(1, 3, 0))
            throwCountOverflow()
        else
            throw ArithmeticException("Count overflow has happened.")
    }
    return count
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy