Split AbstractCollection, List, Set in JS to readonly Abstract[Collection] and mutable AbstractMutable[Collection].
Update compatibility type aliases.
This commit is contained in:
@@ -17,10 +17,7 @@
|
||||
package kotlin.collections
|
||||
|
||||
|
||||
public abstract class AbstractCollection<E> protected constructor() : MutableCollection<E> {
|
||||
|
||||
abstract override val size: Int
|
||||
abstract override fun iterator(): MutableIterator<E>
|
||||
public abstract class AbstractMutableCollection<E> protected constructor() : AbstractCollection<E>(), MutableCollection<E> {
|
||||
|
||||
override fun add(element: E): Boolean = throw UnsupportedOperationException("Add not supported on this collection")
|
||||
|
||||
@@ -37,16 +34,6 @@ public abstract class AbstractCollection<E> protected constructor() : MutableCol
|
||||
|
||||
override fun isEmpty(): Boolean = size == 0
|
||||
|
||||
override fun contains(element: E): Boolean {
|
||||
for (e in this) {
|
||||
if (e == element) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
override fun containsAll(elements: Collection<E>): Boolean = elements.all { contains(it) }
|
||||
|
||||
|
||||
override fun addAll(elements: Collection<E>): Boolean {
|
||||
var modified = false
|
||||
for (element in elements) {
|
||||
@@ -66,13 +53,7 @@ public abstract class AbstractCollection<E> protected constructor() : MutableCol
|
||||
}
|
||||
}
|
||||
|
||||
abstract override fun hashCode(): Int
|
||||
|
||||
abstract override fun equals(other: Any?): Boolean
|
||||
|
||||
override fun toString(): String = joinToString(", ", "[", "]") {
|
||||
if (it === this) "(this Collection)" else it.toString()
|
||||
}
|
||||
// TODO: move somehow to AbstractCollection
|
||||
|
||||
protected open fun toArray(): Array<Any?> = copyToArrayImpl(this)
|
||||
|
||||
|
||||
@@ -21,10 +21,7 @@
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
public abstract class AbstractList<E> protected constructor() : AbstractCollection<E>(), MutableList<E> {
|
||||
abstract override val size: Int
|
||||
abstract override fun get(index: Int): E
|
||||
|
||||
public abstract class AbstractMutableList<E> protected constructor() : AbstractMutableCollection<E>(), MutableList<E> {
|
||||
protected var modCount: Int = 0
|
||||
|
||||
override fun add(index: Int, element: E): Unit = throw UnsupportedOperationException("Add not supported on this list")
|
||||
@@ -94,10 +91,10 @@ public abstract class AbstractList<E> protected constructor() : AbstractCollecti
|
||||
if (other === this) return true
|
||||
if (other !is List<*>) return false
|
||||
|
||||
return orderedEquals(this, other)
|
||||
return AbstractList.orderedEquals(this, other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int = orderedHashCode(this)
|
||||
override fun hashCode(): Int = AbstractList.orderedHashCode(this)
|
||||
|
||||
|
||||
private open inner class IteratorImpl : MutableIterator<E> {
|
||||
@@ -132,7 +129,7 @@ public abstract class AbstractList<E> protected constructor() : AbstractCollecti
|
||||
private inner class ListIteratorImpl(index: Int) : IteratorImpl(), MutableListIterator<E> {
|
||||
|
||||
init {
|
||||
checkPositionIndex(index, this@AbstractList.size)
|
||||
AbstractList.checkPositionIndex(index, this@AbstractMutableList.size)
|
||||
this.index = index
|
||||
}
|
||||
|
||||
@@ -157,33 +154,33 @@ public abstract class AbstractList<E> protected constructor() : AbstractCollecti
|
||||
|
||||
override fun set(element: E) {
|
||||
require(last != -1)
|
||||
this@AbstractList[last] = element
|
||||
this@AbstractMutableList[last] = element
|
||||
}
|
||||
}
|
||||
|
||||
private class SubList<E>(private val list: AbstractList<E>, private val fromIndex: Int, toIndex: Int) : AbstractList<E>() {
|
||||
private class SubList<E>(private val list: AbstractMutableList<E>, private val fromIndex: Int, toIndex: Int) : AbstractMutableList<E>() {
|
||||
private var _size: Int = 0
|
||||
|
||||
init {
|
||||
checkRangeIndexes(fromIndex, toIndex, list.size)
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, list.size)
|
||||
this._size = toIndex - fromIndex
|
||||
}
|
||||
|
||||
override fun add(index: Int, element: E) {
|
||||
checkPositionIndex(index, _size)
|
||||
AbstractList.checkPositionIndex(index, _size)
|
||||
|
||||
list.add(fromIndex + index, element)
|
||||
_size++
|
||||
}
|
||||
|
||||
override fun get(index: Int): E {
|
||||
checkElementIndex(index, _size)
|
||||
AbstractList.checkElementIndex(index, _size)
|
||||
|
||||
return list[fromIndex + index]
|
||||
}
|
||||
|
||||
override fun removeAt(index: Int): E {
|
||||
checkElementIndex(index, _size)
|
||||
AbstractList.checkElementIndex(index, _size)
|
||||
|
||||
val result = list.removeAt(fromIndex + index)
|
||||
_size--
|
||||
@@ -191,7 +188,7 @@ public abstract class AbstractList<E> protected constructor() : AbstractCollecti
|
||||
}
|
||||
|
||||
override fun set(index: Int, element: E): E {
|
||||
checkElementIndex(index, _size)
|
||||
AbstractList.checkElementIndex(index, _size)
|
||||
|
||||
return list.set(fromIndex + index, element)
|
||||
}
|
||||
@@ -199,49 +196,4 @@ public abstract class AbstractList<E> protected constructor() : AbstractCollecti
|
||||
override val size: Int get() = _size
|
||||
}
|
||||
|
||||
companion object {
|
||||
internal fun checkElementIndex(index: Int, size: Int) {
|
||||
if (index < 0 || index >= size) {
|
||||
throw IndexOutOfBoundsException("index: $index, size: $size")
|
||||
}
|
||||
}
|
||||
|
||||
internal fun checkPositionIndex(index: Int, size: Int) {
|
||||
if (index < 0 || index > size) {
|
||||
throw IndexOutOfBoundsException("index: $index, size: $size")
|
||||
}
|
||||
}
|
||||
|
||||
internal fun checkRangeIndexes(start: Int, end: Int, size: Int) {
|
||||
if (start < 0 || end > size) {
|
||||
throw IndexOutOfBoundsException("fromIndex: $start, toIndex: $end, size: $size")
|
||||
}
|
||||
if (start > end) {
|
||||
throw IllegalArgumentException("fromIndex: $start > toIndex: $end")
|
||||
}
|
||||
}
|
||||
|
||||
internal fun orderedHashCode(c: Collection<*>): Int {
|
||||
var hashCode = 1
|
||||
for (e in c) {
|
||||
hashCode = 31 * hashCode + (e?.hashCode() ?: 0)
|
||||
hashCode = hashCode or 0 // make sure we don't overflow
|
||||
}
|
||||
return hashCode
|
||||
}
|
||||
|
||||
internal fun orderedEquals(c: Collection<*>, other: Collection<*>): Boolean {
|
||||
if (c.size != other.size) return false
|
||||
|
||||
val otherIterator = other.iterator()
|
||||
for (elem in c) {
|
||||
val elemOther = otherIterator.next()
|
||||
if (elem != elemOther) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
abstract class AbstractMap<K, V> protected constructor() : MutableMap<K, V> {
|
||||
public abstract class AbstractMap<K, V> protected constructor() : MutableMap<K, V> {
|
||||
|
||||
/**
|
||||
* A mutable [Map.Entry] shared by several [Map] implementations.
|
||||
@@ -102,7 +102,7 @@ abstract class AbstractMap<K, V> protected constructor() : MutableMap<K, V> {
|
||||
|
||||
|
||||
override val keys: MutableSet<K> get() {
|
||||
return object : AbstractSet<K>() {
|
||||
return object : AbstractMutableSet<K>() {
|
||||
override fun clear() {
|
||||
this@AbstractMap.clear()
|
||||
}
|
||||
@@ -149,7 +149,7 @@ abstract class AbstractMap<K, V> protected constructor() : MutableMap<K, V> {
|
||||
private fun toString(o: Any?): String = if (o === this) "(this Map)" else o.toString()
|
||||
|
||||
override val values: MutableCollection<V> get() {
|
||||
return object : AbstractCollection<V>() {
|
||||
return object : AbstractMutableCollection<V>() {
|
||||
override fun clear() = this@AbstractMap.clear()
|
||||
|
||||
override operator fun contains(element: V): Boolean = containsValue(element)
|
||||
|
||||
@@ -15,21 +15,14 @@
|
||||
*/
|
||||
package kotlin.collections
|
||||
|
||||
abstract class AbstractSet<E> protected constructor() : AbstractCollection<E>(), MutableSet<E> {
|
||||
public abstract class AbstractMutableSet<E> protected constructor() : AbstractMutableCollection<E>(), MutableSet<E> {
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other === this) return true
|
||||
if (other !is Set<*>) return false
|
||||
if (other.size != size) return false
|
||||
return containsAll(other)
|
||||
return AbstractSet.setEquals(this, other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var hashCode = 0
|
||||
for (element in this) {
|
||||
hashCode = (hashCode + (element?.hashCode() ?: 0)) or 0
|
||||
}
|
||||
return hashCode
|
||||
}
|
||||
override fun hashCode(): Int = AbstractSet.unorderedHashCode(this)
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
public open class ArrayList<E> internal constructor(private var array: Array<Any?>) : AbstractList<E>(), RandomAccess {
|
||||
public open class ArrayList<E> internal constructor(private var array: Array<Any?>) : AbstractMutableList<E>(), RandomAccess {
|
||||
|
||||
public constructor(capacity: Int = 0) : this(emptyArray()) {}
|
||||
public constructor(elements: Collection<E>) : this(elements.toTypedArray<Any?>()) {}
|
||||
@@ -103,10 +103,10 @@ public open class ArrayList<E> internal constructor(private var array: Array<Any
|
||||
|
||||
|
||||
private fun rangeCheck(index: Int) = index.apply {
|
||||
checkElementIndex(index, size)
|
||||
AbstractList.checkElementIndex(index, size)
|
||||
}
|
||||
|
||||
private fun insertionRangeCheck(index: Int) = index.apply {
|
||||
checkPositionIndex(index, size)
|
||||
AbstractList.checkPositionIndex(index, size)
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import kotlin.collections.MutableMap.MutableEntry
|
||||
|
||||
open class HashMap<K, V> : AbstractMap<K, V> {
|
||||
|
||||
private inner class EntrySet : AbstractSet<MutableEntry<K, V>>() {
|
||||
private inner class EntrySet : AbstractMutableSet<MutableEntry<K, V>>() {
|
||||
|
||||
override fun clear() {
|
||||
this@HashMap.clear()
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
package kotlin.collections
|
||||
|
||||
|
||||
open class HashSet<E> : AbstractSet<E> {
|
||||
public open class HashSet<E> : AbstractMutableSet<E> {
|
||||
|
||||
private val map: HashMap<E, Any>
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ open class LinkedHashMap<K, V> : HashMap<K, V>, Map<K, V> {
|
||||
internal var prev: ChainEntry<K, V>? = null
|
||||
}
|
||||
|
||||
private inner class EntrySet : AbstractSet<MutableEntry<K, V>>() {
|
||||
private inner class EntrySet : AbstractMutableSet<MutableEntry<K, V>>() {
|
||||
|
||||
private inner class EntryIterator : MutableIterator<MutableEntry<K, V>> {
|
||||
// The last entry that was returned from this iterator.
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
open class LinkedHashSet<E> : HashSet<E> {
|
||||
public open class LinkedHashSet<E> : HashSet<E> {
|
||||
|
||||
internal constructor(map: LinkedHashMap<E, Any>) : super(map)
|
||||
|
||||
|
||||
@@ -34,8 +34,8 @@ public class Date() {
|
||||
|
||||
// TODO: Deprecate with replacement
|
||||
public typealias RandomAccess = kotlin.collections.RandomAccess
|
||||
public typealias AbstractCollection<E> = kotlin.collections.AbstractCollection<E>
|
||||
public typealias AbstractList<E> = kotlin.collections.AbstractList<E>
|
||||
public typealias AbstractCollection<E> = kotlin.collections.AbstractMutableCollection<E>
|
||||
public typealias AbstractList<E> = kotlin.collections.AbstractMutableList<E>
|
||||
public typealias ArrayList<E> = kotlin.collections.ArrayList<E>
|
||||
public typealias HashSet<E> = kotlin.collections.HashSet<E>
|
||||
public typealias LinkedHashSet<E> = kotlin.collections.LinkedHashSet<E>
|
||||
|
||||
Reference in New Issue
Block a user