Rewrite ArrayList, AbstractList, AbstractCollection in kotlin.

AbstractList is imported from GWT.
#KT-12386
This commit is contained in:
Ilya Gorbunov
2016-08-19 22:18:32 +03:00
parent 0abf94c2f4
commit 75069143c7
17 changed files with 535 additions and 418 deletions
+5 -332
View File
@@ -210,6 +210,7 @@
Kotlin.IllegalStateException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.UnsupportedOperationException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.IndexOutOfBoundsException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.ConcurrentModificationException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.ClassCastException = createClassNowWithMessage(Kotlin.RuntimeException);
Kotlin.IOException = createClassNowWithMessage(Kotlin.Exception);
Kotlin.AssertionError = createClassNowWithMessage(Kotlin.Error);
@@ -292,50 +293,6 @@
}
});
/**
* @class
* @extends {ArrayIterator.<T>}
*
* @constructor
* @param {Kotlin.AbstractList.<T>} list
* @template T
*/
lazyInitClasses.ListIterator = Kotlin.createClass(
function () {
return [Kotlin.kotlin.collections.ListIterator]; // TODO: MutableListIterator
},
/** @constructs */
function (list, index) {
this.list = list;
this.size = list.size;
this.index = (index === undefined) ? 0 : index;
}, {
hasNext: function () {
return this.index < this.size;
},
nextIndex: function () {
return this.index;
},
next: function () {
var index = this.index;
var result = this.list.get_za3lpa$(index);
this.index = index + 1;
return result;
},
hasPrevious: function() {
return this.index > 0;
},
previousIndex: function () {
return this.index - 1;
},
previous: function () {
var index = this.index - 1;
var result = this.list.get_za3lpa$(index);
this.index = index;
return result;
}
});
lazyInitClasses.Enum = Kotlin.createClass(
function() {
return [Kotlin.Comparable];
@@ -369,300 +326,12 @@
}
);
Kotlin.RandomAccess = Kotlin.createTraitNow(null);
Kotlin.PropertyMetadata = Kotlin.createClassNow(null,
function (name) {
this.name = name;
}
);
lazyInitClasses.AbstractCollection = Kotlin.createClass(
function () {
return [Kotlin.kotlin.collections.MutableCollection];
}, null, {
addAll_wtfk93$: function (collection) {
var modified = false;
var it = collection.iterator();
while (it.hasNext()) {
if (this.add_za3rmp$(it.next())) {
modified = true;
}
}
return modified
},
removeAll_wtfk93$: function (c) {
var modified = false;
var it = this.iterator();
while (it.hasNext()) {
if (c.contains_za3rmp$(it.next())) {
it.remove();
modified = true;
}
}
return modified
},
retainAll_wtfk93$: function (c) {
var modified = false;
var it = this.iterator();
while (it.hasNext()) {
if (!c.contains_za3rmp$(it.next())) {
it.remove();
modified = true;
}
}
return modified
},
clear: function () {
// TODO: implement with mutable iterator
throw new Kotlin.NotImplementedError("Not implemented yet, see KT-7809");
},
containsAll_wtfk93$: function (c) {
var it = c.iterator();
while (it.hasNext()) {
if (!this.contains_za3rmp$(it.next())) return false;
}
return true;
},
isEmpty: function () {
return this.size === 0;
},
iterator: function () {
// TODO: Do not implement mutable iterator() this way, make abstract
return new Kotlin.ArrayIterator(this.toArray());
},
equals_za3rmp$: function (o) {
if (this.size !== o.size) return false;
var iterator1 = this.iterator();
var iterator2 = o.iterator();
var i = this.size;
while (i-- > 0) {
if (!Kotlin.equals(iterator1.next(), iterator2.next())) {
return false;
}
}
return true;
},
toString: function () {
var builder = "[";
var iterator = this.iterator();
var first = true;
var i = this.size;
while (i-- > 0) {
if (first) {
first = false;
}
else {
builder += ", ";
}
builder += Kotlin.toString(iterator.next());
}
builder += "]";
return builder;
},
toJSON: function () {
return this.toArray();
}
});
/**
* @interface // actually it's abstract class
* @template T
*/
lazyInitClasses.AbstractList = Kotlin.createClass(
function () {
return [Kotlin.kotlin.collections.MutableList, Kotlin.AbstractCollection];
}, null, {
iterator: function () {
return new Kotlin.ListIterator(this);
},
listIterator: function() {
return new Kotlin.ListIterator(this);
},
listIterator_za3lpa$: function(index) {
if (index < 0 || index > this.size) {
throw new Kotlin.IndexOutOfBoundsException("Index: " + index + ", size: " + this.size);
}
return new Kotlin.ListIterator(this, index);
},
add_za3rmp$: function (element) {
this.add_vux3hl$(this.size, element);
return true;
},
addAll_j97iir$: function (index, collection) {
// TODO: implement
throw new Kotlin.NotImplementedError("Not implemented yet, see KT-7809");
},
remove_za3rmp$: function (o) {
var index = this.indexOf_za3rmp$(o);
if (index !== -1) {
this.removeAt_za3lpa$(index);
return true;
}
return false;
},
clear: function () {
// TODO: implement with remove range
throw new Kotlin.NotImplementedError("Not implemented yet, see KT-7809");
},
contains_za3rmp$: function (o) {
return this.indexOf_za3rmp$(o) !== -1;
},
indexOf_za3rmp$: function (o) {
var i = this.listIterator();
while (i.hasNext())
if (Kotlin.equals(i.next(), o))
return i.previousIndex();
return -1;
},
lastIndexOf_za3rmp$: function (o) {
var i = this.listIterator_za3lpa$(this.size);
while (i.hasPrevious())
if (Kotlin.equals(i.previous(), o))
return i.nextIndex();
return -1;
},
subList_vux9f0$: function(fromIndex, toIndex) {
if (fromIndex < 0 || toIndex > this.size)
throw new Kotlin.IndexOutOfBoundsException();
if (fromIndex > toIndex)
throw new Kotlin.IllegalArgumentException();
return new Kotlin.SubList(this, fromIndex, toIndex);
},
hashCode: function() {
var result = 1;
var i = this.iterator();
while (i.hasNext()) {
var obj = i.next();
result = (31*result + Kotlin.hashCode(obj)) | 0;
}
return result;
}
});
lazyInitClasses.SubList = Kotlin.createClass(
function () {
return [Kotlin.AbstractList];
},
function (list, fromIndex, toIndex) {
this.list = list;
this.offset = fromIndex;
this._size = toIndex - fromIndex;
}, {
get_za3lpa$: function (index) {
this.checkRange(index);
return this.list.get_za3lpa$(index + this.offset);
},
set_vux3hl$: function (index, value) {
this.checkRange(index);
this.list.set_vux3hl$(index + this.offset, value);
},
size: {
get: function () {
return this._size;
}
},
add_vux3hl$: function (index, element) {
if (index < 0 || index > this.size) {
throw new Kotlin.IndexOutOfBoundsException();
}
this.list.add_vux3hl$(index + this.offset, element);
},
removeAt_za3lpa$: function (index) {
this.checkRange(index);
var result = this.list.removeAt_za3lpa$(index + this.offset);
this._size--;
return result;
},
checkRange: function (index) {
if (index < 0 || index >= this._size) {
throw new Kotlin.IndexOutOfBoundsException();
}
}
});
//TODO: should be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects)
lazyInitClasses.ArrayList = Kotlin.createClass(
function () {
return [Kotlin.AbstractList, Kotlin.RandomAccess];
},
function () {
this.array = [];
}, {
get_za3lpa$: function (index) {
this.checkRange(index);
return this.array[index];
},
set_vux3hl$: function (index, value) {
this.checkRange(index);
this.array[index] = value;
},
size: {
get: function () {
return this.array.length;
}
},
iterator: function () {
return Kotlin.arrayIterator(this.array);
},
add_za3rmp$: function (element) {
this.array.push(element);
return true;
},
add_vux3hl$: function (index, element) {
this.array.splice(index, 0, element);
},
addAll_wtfk93$: function (collection) {
if (collection.size == 0) {
return false;
}
var it = collection.iterator();
for (var i = this.array.length, n = collection.size; n-- > 0;) {
this.array[i++] = it.next();
}
return true;
},
removeAt_za3lpa$: function (index) {
this.checkRange(index);
return this.array.splice(index, 1)[0];
},
clear: function () {
this.array.length = 0;
},
indexOf_za3rmp$: function (o) {
for (var i = 0; i < this.array.length; i++) {
if (Kotlin.equals(this.array[i], o)) {
return i;
}
}
return -1;
},
lastIndexOf_za3rmp$: function (o) {
for (var i = this.array.length - 1; i >= 0; i--) {
if (Kotlin.equals(this.array[i], o)) {
return i;
}
}
return -1;
},
toArray: function () {
return this.array.slice(0);
},
toString: function () {
return Kotlin.arrayToString(this.array);
},
toJSON: function () {
return this.array;
},
checkRange: function (index) {
if (index < 0 || index >= this.array.length) {
throw new Kotlin.IndexOutOfBoundsException();
}
}
});
Kotlin.Runnable = Kotlin.createTraitNow(null, null, {
run: throwAbstractFunctionInvocationError("Runnable#run")
@@ -1132,9 +801,13 @@
array.sort(Kotlin.primitiveCompareTo)
};
// TODO: Find out whether is it referenced
Kotlin.copyToArray = function (collection) {
if (typeof collection.toArray !== "undefined") return collection.toArray();
return Kotlin.copyToArrayImpl(collection);
};
Kotlin.copyToArrayImpl = function (collection) {
var array = [];
var it = collection.iterator();
while (it.hasNext()) {