fixed mapping sort and max functions from java.util.Collections
This commit is contained in:
@@ -1,48 +0,0 @@
|
|||||||
package java.util
|
|
||||||
|
|
||||||
import java.lang.*
|
|
||||||
|
|
||||||
public object Collections {
|
|
||||||
library("collectionsMax")
|
|
||||||
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = js.noImpl
|
|
||||||
|
|
||||||
// TODO should be immutable!
|
|
||||||
library
|
|
||||||
public val emptyList: List<Any> = ArrayList<Any>()
|
|
||||||
library
|
|
||||||
public val emptyMap: Map<Any, Any> = HashMap<Any,Any>()
|
|
||||||
|
|
||||||
library
|
|
||||||
public val <T> EMPTY_LIST: List<T>
|
|
||||||
get() = emptyList<T>()
|
|
||||||
|
|
||||||
library
|
|
||||||
public val <K,V> EMPTY_MAP: Map<K,V>
|
|
||||||
get() = emptyMap<K,V>()
|
|
||||||
|
|
||||||
library
|
|
||||||
public fun <T> emptyList(): List<T> = emptyList as List<T>
|
|
||||||
library
|
|
||||||
public fun <K,V> emptyMap(): Map<K,V> = emptyMap as Map<K,V>
|
|
||||||
|
|
||||||
library
|
|
||||||
public fun <T> sort(list: MutableList<T>): Unit {
|
|
||||||
throw UnsupportedOperationException()
|
|
||||||
}
|
|
||||||
|
|
||||||
library("sortWithComp")
|
|
||||||
public fun <T> sort(list: MutableList<T>, comparator: java.util.Comparator<T>): Unit {
|
|
||||||
throw UnsupportedOperationException()
|
|
||||||
}
|
|
||||||
|
|
||||||
library
|
|
||||||
public fun <T> reverse(list: MutableList<T>): Unit {
|
|
||||||
val size = list.size()
|
|
||||||
for (i in 0.rangeTo(size / 2)) {
|
|
||||||
val i2 = size - i
|
|
||||||
val tmp = list[i]
|
|
||||||
list[i] = list[i2]
|
|
||||||
list[i2] = tmp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package java.util.Collections
|
||||||
|
|
||||||
|
import java.lang.*
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
library("collectionsMax")
|
||||||
|
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = js.noImpl
|
||||||
|
|
||||||
|
library("collectionsSort")
|
||||||
|
public fun <T> sort(list: MutableList<T>): Unit = js.noImpl
|
||||||
|
|
||||||
|
library("collectionsSort")
|
||||||
|
public fun <T> sort(list: MutableList<T>, comparator: java.util.Comparator<T>): Unit = js.noImpl
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package java.util.Collections
|
||||||
|
|
||||||
|
import java.lang.*
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
// TODO should be immutable!
|
||||||
|
library
|
||||||
|
public val emptyList: List<Any> = ArrayList<Any>()
|
||||||
|
library
|
||||||
|
public val emptyMap: Map<Any, Any> = HashMap<Any,Any>()
|
||||||
|
|
||||||
|
library
|
||||||
|
public val <T> EMPTY_LIST: List<T>
|
||||||
|
get() = emptyList<T>()
|
||||||
|
|
||||||
|
library
|
||||||
|
public val <K,V> EMPTY_MAP: Map<K,V>
|
||||||
|
get() = emptyMap<K,V>()
|
||||||
|
|
||||||
|
library
|
||||||
|
public fun <T> emptyList(): List<T> = emptyList as List<T>
|
||||||
|
library
|
||||||
|
public fun <K,V> emptyMap(): Map<K,V> = emptyMap as Map<K,V>
|
||||||
|
|
||||||
|
library
|
||||||
|
public fun <T> reverse(list: MutableList<T>): Unit {
|
||||||
|
val size = list.size()
|
||||||
|
for (i in 0.rangeTo(size / 2)) {
|
||||||
|
val i2 = size - i
|
||||||
|
val tmp = list[i]
|
||||||
|
list[i] = list[i2]
|
||||||
|
list[i2] = tmp
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.k2js.test.semantics;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
|
||||||
|
@SuppressWarnings("JUnitTestCaseWithNoTests")
|
||||||
|
public final class StdLibJavaUtilCollectionsTest extends JsUnitTestBase {
|
||||||
|
public static Test suite() throws Exception {
|
||||||
|
return createTestSuiteForFile("libraries/stdlib/test/js/javautilCollectionsTest.kt");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -65,6 +65,7 @@ public abstract class Config {
|
|||||||
"/core/javaio.kt",
|
"/core/javaio.kt",
|
||||||
"/core/javalang.kt",
|
"/core/javalang.kt",
|
||||||
"/core/javautil.kt",
|
"/core/javautil.kt",
|
||||||
|
"/core/javautilCollections.kt",
|
||||||
"/core/json.kt",
|
"/core/json.kt",
|
||||||
"/core/kotlin.kt",
|
"/core/kotlin.kt",
|
||||||
"/core/math.kt",
|
"/core/math.kt",
|
||||||
@@ -83,7 +84,7 @@ public abstract class Config {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static final List<String> LIB_FILES_WITH_CODE = Arrays.asList(
|
public static final List<String> LIB_FILES_WITH_CODE = Arrays.asList(
|
||||||
"/stdlib/TuplesCode.kt",
|
"/stdlib/TuplesCode.kt",
|
||||||
"/core/javautilCode.kt"
|
"/core/javautilCollectionsCode.kt"
|
||||||
);
|
);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -107,8 +107,6 @@ public final class StandardClasses {
|
|||||||
.methods("iterator", "contains").properties("start", "end", "increment");
|
.methods("iterator", "contains").properties("start", "end", "increment");
|
||||||
|
|
||||||
standardClasses.declare().forFQ("jet.Any.toString").kotlinFunction("toString");
|
standardClasses.declare().forFQ("jet.Any.toString").kotlinFunction("toString");
|
||||||
|
|
||||||
standardClasses.declare().forFQ("java.util.Collections.<no name provided>.max").kotlinFunction("collectionsMax");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -198,6 +198,7 @@ var kotlin = {set:function (receiver, key, value) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//TODO: should be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects)
|
||||||
Kotlin.ArrayList = Kotlin.$createClass(Kotlin.AbstractList, {
|
Kotlin.ArrayList = Kotlin.$createClass(Kotlin.AbstractList, {
|
||||||
initialize: function () {
|
initialize: function () {
|
||||||
this.array = [];
|
this.array = [];
|
||||||
@@ -364,7 +365,7 @@ var kotlin = {set:function (receiver, key, value) {
|
|||||||
},
|
},
|
||||||
next: function () {
|
next: function () {
|
||||||
var value = this.$i;
|
var value = this.$i;
|
||||||
this.set_i(this.$i + this.$increment)
|
this.set_i(this.$i + this.$increment);
|
||||||
return value;
|
return value;
|
||||||
},
|
},
|
||||||
get_hasNext: function () {
|
get_hasNext: function () {
|
||||||
@@ -446,6 +447,31 @@ var kotlin = {set:function (receiver, key, value) {
|
|||||||
return max;
|
return max;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Kotlin.collectionsSort = function (mutableList, comparator) {
|
||||||
|
var boundComparator = undefined;
|
||||||
|
if (comparator !== undefined) {
|
||||||
|
boundComparator = comparator.compare.bind(comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutableList instanceof Array) {
|
||||||
|
mutableList.sort(boundComparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: should be deleted when List will be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects)
|
||||||
|
var array = [];
|
||||||
|
var it = mutableList.iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
array.push(it.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
array.sort(boundComparator);
|
||||||
|
|
||||||
|
for (var i = 0, n = array.length; i < n; i++) {
|
||||||
|
mutableList.set(i, array[i]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
Kotlin.StringBuilder = Kotlin.$createClass(
|
Kotlin.StringBuilder = Kotlin.$createClass(
|
||||||
{
|
{
|
||||||
initialize:function () {
|
initialize:function () {
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ class MapJsTest {
|
|||||||
|
|
||||||
test fun hashMapValues() {
|
test fun hashMapValues() {
|
||||||
val map = createTestHashMap()
|
val map = createTestHashMap()
|
||||||
//todo: fixme after sort() will be fixed for JS
|
assertEquals(VALUES.toList(), map.values().toSortedList())
|
||||||
assertEquals(VALUES, map.values())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createTestHashMap(): HashMap<String, Int> {
|
fun createTestHashMap(): HashMap<String, Int> {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package testPackage
|
||||||
|
|
||||||
|
import java.util.Collections
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
|
import kotlin.test.*
|
||||||
|
import org.junit.Test as test
|
||||||
|
|
||||||
|
fun <T> List<T>.toArrayList() = this.toCollection(ArrayList<T>())
|
||||||
|
|
||||||
|
class JavautilCollectionsTest {
|
||||||
|
val TEST_LIST = array(2, 0, 9, 7, 1).toList()
|
||||||
|
val SORTED_TEST_LIST = array(0, 1, 2, 7, 9).toList()
|
||||||
|
val MAX_ELEMENT = 9
|
||||||
|
val COMPARATOR = comparator { (x: Int, y: Int) -> if (x > y) 1 else if (x < y) -1 else 0 }
|
||||||
|
|
||||||
|
test fun maxWithComparator() {
|
||||||
|
assertEquals(MAX_ELEMENT, Collections.max(TEST_LIST, COMPARATOR))
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun sort() {
|
||||||
|
val list = TEST_LIST.toArrayList()
|
||||||
|
Collections.sort(list)
|
||||||
|
assertEquals(SORTED_TEST_LIST, list)
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun sortWithComparator() {
|
||||||
|
val list = TEST_LIST.toArrayList()
|
||||||
|
Collections.sort(list, COMPARATOR)
|
||||||
|
assertEquals(SORTED_TEST_LIST, list)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user