Make empty list produced by listOf() a singleton List object.

This commit is contained in:
Ilya Ryzhenkov
2014-05-07 18:43:39 +04:00
committed by Andrey Breslav
parent 692f60b1d6
commit 5f224efdc1
@@ -2,17 +2,31 @@ package kotlin
import java.util.*
class stdlib_emptyListClass : List<Any> by ArrayList<Any>() {}
private val stdlib_emptyList : List<Any> = ArrayList<Any>() // TODO: Change to stdlib_emptyListClass() when KT-5192 is fixed
private fun stdlib_emptyList<T>() = stdlib_emptyList as List<T>
class stdlib_emptyMapClass : Map<Any, Any> by HashMap<Any, Any>() {}
private val stdlib_emptyMap : Map<Any, Any> = HashMap<Any, Any>() // TODO: Change to stdlib_emptyMapClass() when KT-5192 is fixed
private fun stdlib_emptyMap<K,V>() = stdlib_emptyMap as Map<K,V>
/** Returns a new read-only list of given elements */
public fun listOf<T>(vararg values: T): List<T> = arrayListOf(*values)
public fun listOf<T>(vararg values: T): List<T> = if (values.size == 0) stdlib_emptyList() else arrayListOf(*values)
/** Returns an empty list */
public fun listOf<T>(): List<T> = 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<K, V>(vararg values: Pair<K, V>): Map<K, V> = hashMapOf(*values)
public fun mapOf<K, V>(vararg values: Pair<K, V>): Map<K, V> = if (values.size == 0) stdlib_emptyMap() else hashMapOf(*values)
/** Returns an empty read-only map */
public fun mapOf<K, V>(): Map<K, V> = stdlib_emptyMap()
/** Returns a new ArrayList with a variable number of initial elements */
public fun arrayListOf<T>(vararg values: T): ArrayList<T> = values.toCollection(ArrayList<T>(values.size))
public fun arrayListOf<T>(vararg values: T): ArrayList<T> = values.toCollection(ArrayList(values.size))
/** Returns a new HashSet with a variable number of initial elements */
public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(HashSet<T>(values.size))
public fun hashSetOf<T>(vararg values: T): HashSet<T> = values.toCollection(HashSet(values.size))
/**
* Returns a new [[HashMap]] populated with the given pairs where the first value in each pair
@@ -48,12 +62,12 @@ val Collection<*>.notEmpty: Boolean
get() = isNotEmpty()
/** Returns the Collection if its not null otherwise it returns the empty list */
public fun <T> Collection<T>?.orEmpty(): Collection<T> = this ?: Collections.emptyList<T>()
public fun <T> Collection<T>?.orEmpty(): Collection<T> = this ?: stdlib_emptyList()
// List APIs
/** Returns the List if its not null otherwise returns the empty list */
public fun <T> List<T>?.orEmpty(): List<T> = this ?: Collections.emptyList<T>()
public fun <T> List<T>?.orEmpty(): List<T> = this ?: stdlib_emptyList()
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -90,7 +104,7 @@ val <T> List<T>.first: T?
val <T> List<T>.last: T?
get() {
val s = this.size
return if (s > 0) this.get(s - 1) else null
return if (s > 0) this[s - 1] else null
}
/**
@@ -107,7 +121,7 @@ val <T> List<T>.lastIndex: Int
* @includeFunctionBody ../../test/collections/ListSpecificTest.kt head
*/
val <T> List<T>.head: T?
get() = if (this.isNotEmpty()) this.get(0) else null
get() = if (this.isNotEmpty()) this[0] else null
/**
* Returns all elements in this collection apart from the first one