Split AbstractCollection, List, Set in JS to readonly Abstract[Collection] and mutable AbstractMutable[Collection].

Update compatibility type aliases.
This commit is contained in:
Ilya Gorbunov
2016-09-01 00:30:39 +03:00
parent 2bb1d6d5b4
commit a5c0f11d60
15 changed files with 56 additions and 125 deletions
@@ -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)
+2 -2
View File
@@ -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>
@@ -1,15 +1,13 @@
// KT-2468 ArrayList<String> is List<String> or HashSet<String> is Set<String> fails in generated JS code
package foo
import java.util.*
class A
fun checkAbstractList(obj: Any) {
assertTrue(obj is AbstractList<*>, "checkAbstractList: is AbstractList")
assertTrue(obj is AbstractMutableList<*>, "checkAbstractList: is AbstractMutableList")
assertTrue(obj is MutableList<*>, "checkAbstractList: is MutableList")
assertTrue(obj is List<*>, "checkAbstractList: is List")
assertTrue(obj is AbstractCollection<*>, "checkAbstractList: is AbstractCollection")
assertTrue(obj is AbstractMutableCollection<*>, "checkAbstractList: is AbstractMutableCollection")
assertTrue(obj is MutableCollection<*>, "checkAbstractList: is MutableCollection")
assertTrue(obj is Collection<*>, "checkAbstractList: is Collection")
assertTrue(obj is MutableIterable<*>, "checkAbstractList: is MutableIterable")
@@ -25,7 +23,8 @@ fun checkArrayList(obj: Any) {
fun checkHashSet(obj: Any) {
assertTrue(obj is HashSet<*>, "checkHashSet: is HashSet")
assertTrue(obj is AbstractCollection<*>, "checkHashSet: is AbstractCollection")
assertTrue(obj is AbstractMutableSet<*>, "checkHashSet: is AbstractMutableSet")
assertTrue(obj is AbstractMutableCollection<*>, "checkHashSet: is AbstractMutableCollection")
assertTrue(obj is MutableCollection<*>, "checkHashSet: is MutableCollection")
assertTrue(obj is Collection<*>, "checkHashSet: is Collection")
assertTrue(obj is MutableIterable<*>, "checkHashSet: is MutableIterable")
@@ -15,14 +15,14 @@
*/
package kotlin.collections
public abstract class AbstractCollection<out E> : Collection<E> {
public abstract class AbstractCollection<out E> protected constructor() : Collection<E> {
abstract override val size: Int
abstract override fun iterator(): Iterator<E>
override fun contains(element: @UnsafeVariance E): Boolean = any { it == element }
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean =
elements.all(this::contains)
elements.all { contains(it) } // use when js will support bound refs: elements.all(this::contains)
override fun isEmpty(): Boolean = size == 0
@@ -20,7 +20,7 @@
package kotlin.collections
public abstract class AbstractList<out E> : AbstractCollection<E>(), List<E> {
public abstract class AbstractList<out E> protected constructor() : AbstractCollection<E>(), List<E> {
abstract override val size: Int
abstract override fun get(index: Int): E
@@ -62,7 +62,7 @@ public abstract class AbstractList<out E> : AbstractCollection<E>(), List<E> {
override fun hashCode(): Int = orderedHashCode(this)
internal open inner class IteratorImpl : Iterator<E> {
private open inner class IteratorImpl : Iterator<E> {
/** the index of the item that will be returned on the next call to [next]`()` */
protected var index = 0
/** the index of the item that was returned on the previous call to [next]`()`
@@ -83,7 +83,7 @@ public abstract class AbstractList<out E> : AbstractCollection<E>(), List<E> {
/**
* Implementation of `MutableListIterator` for abstract lists.
*/
internal open inner class ListIteratorImpl(index: Int) : IteratorImpl(), ListIterator<E> {
private open inner class ListIteratorImpl(index: Int) : IteratorImpl(), ListIterator<E> {
init {
checkPositionIndex(index, this@AbstractList.size)
@@ -104,7 +104,7 @@ public abstract class AbstractList<out E> : AbstractCollection<E>(), List<E> {
override fun previousIndex(): Int = index - 1
}
companion object {
internal companion object {
internal fun checkElementIndex(index: Int, size: Int) {
if (index < 0 || index >= size) {
throw IndexOutOfBoundsException("index: $index, size: $size")
@@ -20,16 +20,24 @@ public abstract class AbstractSet<out E> protected constructor() : AbstractColle
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 setEquals(this, other)
}
override fun hashCode(): Int {
var hashCode = 0
for (element in this) {
hashCode = (hashCode + (element?.hashCode() ?: 0)) or 0
override fun hashCode(): Int = unorderedHashCode(this)
internal companion object {
internal fun unorderedHashCode(c: Collection<*>): Int {
var hashCode = 0
for (element in c) {
hashCode = (hashCode + (element?.hashCode() ?: 0)) or 0
}
return hashCode
}
internal fun setEquals(c: Set<*>, other: Set<*>): Boolean {
if (c.size != other.size) return false
return c.containsAll(other)
}
return hashCode
}
}
@@ -67,7 +67,7 @@ public final class kotlin/_Assertions {
}
public abstract class kotlin/collections/AbstractCollection : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
public fun <init> ()V
protected fun <init> ()V
public fun add (Ljava/lang/Object;)Z
public fun addAll (Ljava/util/Collection;)Z
public fun clear ()V
@@ -99,7 +99,7 @@ public abstract class kotlin/collections/AbstractIterator : java/util/Iterator,
public abstract class kotlin/collections/AbstractList : kotlin/collections/AbstractCollection, java/util/List, kotlin/jvm/internal/markers/KMappedMarker {
public static final field Companion Lkotlin/collections/AbstractList$Companion;
public fun <init> ()V
protected fun <init> ()V
public fun add (ILjava/lang/Object;)V
public fun add (Ljava/lang/Object;)Z
public fun addAll (ILjava/util/Collection;)Z
@@ -125,10 +125,8 @@ public abstract class kotlin/collections/AbstractList : kotlin/collections/Abstr
public fun toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
}
public final class kotlin/collections/AbstractList$Companion {
}
public abstract class kotlin/collections/AbstractSet : kotlin/collections/AbstractCollection, java/util/Set, kotlin/jvm/internal/markers/KMappedMarker {
public static final field Companion Lkotlin/collections/AbstractSet$Companion;
protected fun <init> ()V
public fun add (Ljava/lang/Object;)Z
public fun addAll (Ljava/util/Collection;)Z