From 89388f1f83e61101a673d8204b63a38f41293bc5 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 24 Aug 2016 16:19:44 +0300 Subject: [PATCH] Fixes in ArrayList after review. Provide better implementations for some methods of AbstractList. #KT-12386 --- .../src/core/collections/AbstractList.kt | 5 +++++ .../src/core/collections/ArrayList.kt | 18 +++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/js/js.libraries/src/core/collections/AbstractList.kt b/js/js.libraries/src/core/collections/AbstractList.kt index c84bc926e94..df82a0dfe95 100644 --- a/js/js.libraries/src/core/collections/AbstractList.kt +++ b/js/js.libraries/src/core/collections/AbstractList.kt @@ -50,9 +50,14 @@ public abstract class AbstractList protected constructor() : AbstractCollecti removeRange(0, size) } + override fun removeAll(elements: Collection): Boolean = removeAll { it in elements } + override fun retainAll(elements: Collection): Boolean = removeAll { it !in elements } + override fun iterator(): MutableIterator = IteratorImpl() + override fun contains(element: E): Boolean = indexOf(element) >= 0 + override fun indexOf(element: E): Int { for (index in 0..lastIndex) { if (get(index) == element) { diff --git a/js/js.libraries/src/core/collections/ArrayList.kt b/js/js.libraries/src/core/collections/ArrayList.kt index 42b53ad46b7..cf132428e2b 100644 --- a/js/js.libraries/src/core/collections/ArrayList.kt +++ b/js/js.libraries/src/core/collections/ArrayList.kt @@ -16,8 +16,6 @@ package kotlin.collections -//TODO: should be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects) - public open class ArrayList internal constructor(private var array: Array) : AbstractList(), RandomAccess { public constructor(capacity: Int = 0) : this(emptyArray()) {} @@ -55,8 +53,12 @@ public open class ArrayList internal constructor(private var array: Array return addAll(elements) + 0 -> array = elements.toTypedArray() + array + else -> array = array.copyOfRange(0, index).asDynamic().concat(elements.toTypedArray(), array.copyOfRange(index, size)) + } - array = array.copyOfRange(0, index) + elements.toTypedArray() + array.copyOfRange(index, size) modCount++ return true } @@ -64,7 +66,7 @@ public open class ArrayList internal constructor(private var array: Array internal constructor(private var array: Array= 0 override fun indexOf(element: E): Int = array.indexOf(element) @@ -100,13 +101,12 @@ public open class ArrayList internal constructor(private var array: Array = array.copyOf() + private fun rangeCheck(index: Int) = index.apply { - if (index !in array.indices) - throw IndexOutOfBoundsException("Index: $index, Size: $size") + checkElementIndex(index, size) } private fun insertionRangeCheck(index: Int) = index.apply { - if (index !in 0..size) - throw IndexOutOfBoundsException("Index: $index, Size: $size") + checkPositionIndex(index, size) } } \ No newline at end of file