core.collections.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.collections
@library("copyToArray")
public fun Collection.toTypedArray(): Array = noImpl
/**
* Returns an immutable list containing only the specified object [element].
*/
public fun listOf(element: T): List = arrayListOf(element)
/**
* Returns an immutable set containing only the specified object [element].
*/
public fun setOf(element: T): Set = hashSetOf(element)
/**
* Returns an immutable map, mapping only the specified key to the
* specified value.
*/
public fun mapOf(pair: Pair): Map = hashMapOf(pair)
public inline fun MutableMap.getOrPut(key: K, defaultValue: () -> V): V {
val value = get(key)
return if (value == null && !containsKey(key)) {
val answer = defaultValue()
put(key, answer)
answer
} else {
value as V
}
}