From ff2b190f2d5cb62ca537efc5ba69e48a74e4e51c Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Fri, 18 May 2012 15:04:38 +0400 Subject: [PATCH] shared copy of empty ImmutableArrayList --- .../stdlib/src/kotlin/ImmutableArrayList.kt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libraries/stdlib/src/kotlin/ImmutableArrayList.kt b/libraries/stdlib/src/kotlin/ImmutableArrayList.kt index 88821f78a3c..2de2a9fd8b0 100644 --- a/libraries/stdlib/src/kotlin/ImmutableArrayList.kt +++ b/libraries/stdlib/src/kotlin/ImmutableArrayList.kt @@ -47,6 +47,9 @@ private class ImmutableArrayList( if (toIndex > length) { throw IndexOutOfBoundsException("fromIndex ($fromIndex) + toIndex ($toIndex) > length ($length)") } + if (fromIndex == toIndex) { + return emptyImmutableArrayList as List + } if (fromIndex == 0 && toIndex == length) { return this } @@ -58,6 +61,7 @@ private class ImmutableArrayList( // TODO: make private val, see http://youtrack.jetbrains.com/issue/KT-2028 internal val emptyArray = arrayOfNulls(0) +internal val emptyImmutableArrayList = ImmutableArrayList(emptyArray, 0, 0) public class ImmutableArrayListBuilder() { @@ -65,10 +69,15 @@ public class ImmutableArrayListBuilder() { private var length = 0 public fun build(): List { - val r = ImmutableArrayList(array as Array, 0, length) - array = emptyArray - length = 0 - return r + if (length == 0) { + return emptyImmutableArrayList as List + } + else { + val r = ImmutableArrayList(array as Array, 0, length) + array = emptyArray + length = 0 + return r + } } public fun ensureCapacity(capacity: Int) {