From 5f224efdc1850124d941c7c90702e172f2e395d7 Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Wed, 7 May 2014 18:43:39 +0400 Subject: [PATCH] Make empty list produced by listOf() a singleton List object. --- .../stdlib/src/kotlin/collections/JUtil.kt | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/libraries/stdlib/src/kotlin/collections/JUtil.kt b/libraries/stdlib/src/kotlin/collections/JUtil.kt index ccf15acf398..b5025cd45c5 100644 --- a/libraries/stdlib/src/kotlin/collections/JUtil.kt +++ b/libraries/stdlib/src/kotlin/collections/JUtil.kt @@ -2,17 +2,31 @@ package kotlin import java.util.* +class stdlib_emptyListClass : List by ArrayList() {} +private val stdlib_emptyList : List = ArrayList() // TODO: Change to stdlib_emptyListClass() when KT-5192 is fixed +private fun stdlib_emptyList() = stdlib_emptyList as List + +class stdlib_emptyMapClass : Map by HashMap() {} +private val stdlib_emptyMap : Map = HashMap() // TODO: Change to stdlib_emptyMapClass() when KT-5192 is fixed +private fun stdlib_emptyMap() = stdlib_emptyMap as Map + /** Returns a new read-only list of given elements */ -public fun listOf(vararg values: T): List = arrayListOf(*values) +public fun listOf(vararg values: T): List = if (values.size == 0) stdlib_emptyList() else arrayListOf(*values) + +/** Returns an empty list */ +public fun listOf(): List = 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(vararg values: Pair): Map = hashMapOf(*values) +public fun mapOf(vararg values: Pair): Map = if (values.size == 0) stdlib_emptyMap() else hashMapOf(*values) + +/** Returns an empty read-only map */ +public fun mapOf(): Map = stdlib_emptyMap() /** Returns a new ArrayList with a variable number of initial elements */ -public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size)) +public fun arrayListOf(vararg values: T): ArrayList = values.toCollection(ArrayList(values.size)) /** Returns a new HashSet with a variable number of initial elements */ -public fun hashSetOf(vararg values: T): HashSet = values.toCollection(HashSet(values.size)) +public fun hashSetOf(vararg values: T): HashSet = 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 Collection?.orEmpty(): Collection = this ?: Collections.emptyList() +public fun Collection?.orEmpty(): Collection = this ?: stdlib_emptyList() // List APIs /** Returns the List if its not null otherwise returns the empty list */ -public fun List?.orEmpty(): List = this ?: Collections.emptyList() +public fun List?.orEmpty(): List = this ?: stdlib_emptyList() /** TODO figure out necessary variance/generics ninja stuff... :) @@ -90,7 +104,7 @@ val List.first: T? val List.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 List.lastIndex: Int * @includeFunctionBody ../../test/collections/ListSpecificTest.kt head */ val List.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