Improve toReadOnlyList(), consider the case when size = 1

This commit is contained in:
Alexander Udalov
2015-08-07 02:43:36 +03:00
parent 182e2ebbab
commit d7b1e5d7a8
3 changed files with 12 additions and 7 deletions
@@ -16,12 +16,10 @@
package org.jetbrains.kotlin.utils
import java.util.LinkedHashMap
import java.util.ArrayList
import java.util.HashMap
import java.util.HashSet
import java.util.Collections
import java.util.LinkedHashSet
import java.util.LinkedHashMap
public fun <K, V> Sequence<V>.valuesToMap(key: (V) -> K): Map<K, V> {
val map = LinkedHashMap<K, V>()
@@ -102,6 +100,11 @@ public fun <E> newHashSetWithExpectedSize(expectedSize: Int): HashSet<E> {
}
public fun <T> Collection<T>.toReadOnlyList(): List<T> =
if (isEmpty()) Collections.emptyList() else ArrayList(this)
when (size()) {
0 -> emptyList()
1 -> listOf(first())
else -> ArrayList(this)
}
public fun <T: Any> T?.singletonOrEmptyList(): List<T> = if (this != null) Collections.singletonList(this) else Collections.emptyList()
public fun <T: Any> T?.singletonOrEmptyList(): List<T> =
if (this != null) listOf(this) else emptyList()