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
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.types.DescriptorSubstitutor;
import org.jetbrains.kotlin.types.JetType;
import org.jetbrains.kotlin.types.TypeSubstitutor;
import org.jetbrains.kotlin.types.Variance;
import org.jetbrains.kotlin.utils.UtilsPackage;
import java.util.*;
@@ -64,7 +65,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
@Nullable Modality modality,
@NotNull Visibility visibility
) {
this.typeParameters = new ArrayList<TypeParameterDescriptor>(typeParameters);
this.typeParameters = UtilsPackage.toReadOnlyList(typeParameters);
this.unsubstitutedValueParameters = unsubstitutedValueParameters;
this.unsubstitutedReturnType = unsubstitutedReturnType;
this.modality = modality;
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.FUN
import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.VAL
import org.jetbrains.kotlin.serialization.ProtoBuf.Callable.CallableKind.VAR
import org.jetbrains.kotlin.serialization.deserialization.descriptors.*
import org.jetbrains.kotlin.utils.toReadOnlyList
public class MemberDeserializer(private val c: DeserializationContext) {
public fun loadCallable(proto: Callable): CallableMemberDescriptor {
@@ -187,7 +188,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
if (proto.hasVarargElementType()) c.typeDeserializer.type(proto.getVarargElementType()) else null,
SourceElement.NO_SOURCE
)
}
}.toReadOnlyList()
}
private fun getParameterAnnotations(
@@ -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()