From 28228d23b1c0b2bcbf18867e764c58aa11aa85c0 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Wed, 25 Jun 2014 18:30:36 +0400 Subject: [PATCH] JS stdlib: added missed constructors for HashSet, LinkedHashSet, HashMap and LinkedHashMap. --- js/js.libraries/src/core/javautil.kt | 22 +++++++--- js/js.libraries/src/core/javautilCode.kt | 41 +++++++++++++++++++ .../src/org/jetbrains/k2js/config/Config.java | 1 + js/js.translator/testData/maps.js | 35 +++++----------- libraries/stdlib/test/js/MapJsTest.kt | 38 +++++++++++++++++ libraries/stdlib/test/js/SetJsTest.kt | 37 +++++++++++++++++ 6 files changed, 143 insertions(+), 31 deletions(-) create mode 100644 js/js.libraries/src/core/javautilCode.kt diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 9113fb56c59..2713cadedbc 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -1,5 +1,11 @@ package java.util +native +private val DEFAULT_INITIAL_CAPACITY = 16 + +native +private val DEFAULT_LOAD_FACTOR = 0.75f + library public trait Comparator { fun compare(obj1: T, obj2: T): Int; @@ -67,8 +73,9 @@ public open class LinkedList() : AbstractList() { } library -public open class HashSet(capacity: Int = 0) : AbstractCollection(), MutableSet { -} +public open class HashSet( + initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR +) : AbstractCollection(), MutableSet library public trait SortedSet : Set { @@ -79,11 +86,12 @@ public open class TreeSet() : AbstractCollection(), MutableSet, SortedS } library -public open class LinkedHashSet() : AbstractCollection(), MutableSet { -} +public open class LinkedHashSet( + initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR +) : HashSet(initialCapacity, loadFactor), MutableSet library -public open class HashMap(capacity: Int = 0) : MutableMap { +public open class HashMap(initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR) : MutableMap { public override fun size(): Int = js.noImpl public override fun isEmpty(): Boolean = js.noImpl public override fun get(key: Any?): V? = js.noImpl @@ -99,7 +107,9 @@ public open class HashMap(capacity: Int = 0) : MutableMap { } library -public open class LinkedHashMap(capacity: Int = 0) : HashMap(capacity) +public open class LinkedHashMap( + initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR, accessOrder: Boolean = false +) : HashMap(initialCapacity, loadFactor) library public class NoSuchElementException(message: String? = null) : Exception() {} diff --git a/js/js.libraries/src/core/javautilCode.kt b/js/js.libraries/src/core/javautilCode.kt new file mode 100644 index 00000000000..92121fda205 --- /dev/null +++ b/js/js.libraries/src/core/javautilCode.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2010-2014 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 java.util + +public fun HashSet(c: Collection): HashSet { + val set: HashSet = HashSet(c.size()) + set.addAll(c) + return set +} + +public fun LinkedHashSet(c: Collection): HashSet { + val set: LinkedHashSet = LinkedHashSet(c.size()) + set.addAll(c) + return set +} + +public fun HashMap(m: Map): HashMap { + val map: HashMap = HashMap(m.size()) + map.putAll(m) + return map +} + +public fun LinkedHashMap(m: Map): LinkedHashMap { + val map: LinkedHashMap = LinkedHashMap(m.size()) + map.putAll(m) + return map +} diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index aea3607710f..fdf9cc33357 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -101,6 +101,7 @@ public abstract class Config { public static final List LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList( "/core/stringsCode.kt", "/stdlib/domCode.kt", + "/core/javautilCode.kt", "/stdlib/jutilCode.kt", "/stdlib/testCode.kt" ); diff --git a/js/js.translator/testData/maps.js b/js/js.translator/testData/maps.js index 565bc253777..61e02737d8c 100644 --- a/js/js.translator/testData/maps.js +++ b/js/js.translator/testData/maps.js @@ -37,6 +37,14 @@ return this.value; }; + function hashMapPutAll (fromMap) { + var entries = fromMap.entrySet(); + var it = entries.iterator(); + while (it.hasNext()) { + var e = it.next(); + this.put_wn2jw4$(e.getKey(), e.getValue()); + } + } /** @const */ var FUNCTION = "function"; @@ -387,24 +395,8 @@ /** * @param {Hashtable.} hashtable - * @param {(function(Key, Value, Value): Value)=} conflictCallback */ - this.putAll_za3j1t$ = function (hashtable, conflictCallback) { - var entries = hashtable._entries(); - var entry, key, value, thisValue, i = entries.length; - var hasConflictCallback = (typeof conflictCallback == FUNCTION); - while (i--) { - entry = entries[i]; - key = entry[0]; - value = entry[1]; - - // Check for a conflict. The default behaviour is to overwrite the value for an existing key - if (hasConflictCallback && (thisValue = that.get(key))) { - value = conflictCallback(key, thisValue, value); - } - that.put_wn2jw4$(key, value); - } - }; + this.putAll_za3j1t$ = hashMapPutAll; this.clone = function () { var clone = new Hashtable(hashingFunctionParam, equalityFunctionParam); @@ -554,14 +546,7 @@ this.$size = 0; this.map = {}; }, - putAll_za3j1t$: function (fromMap) { - var map = fromMap.map; - for (var key in map) { - //noinspection JSUnfilteredForInLoop - this.map[key] = map[key]; - this.$size++; - } - }, + putAll_za3j1t$: hashMapPutAll, entrySet: function () { var result = new Kotlin.ComplexHashSet(); var map = this.map; diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index 0ee938152ba..9e976453f7a 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt @@ -6,17 +6,53 @@ import java.util.* import org.junit.Test as test class ComplexMapJsTest : MapJsTest() { + // Helper function with generic parameter to force to use ComlpexHashMap + fun doTest>() { + HashMap() + HashMap(3) + HashMap(3, 0.5f) + val map = HashMap(createTestMap() as HashMap) + + assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList()) + assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList()) + } + test override fun constructors() { + doTest() + } + override fun > Collection.toNormalizedList(): List = this.toSortedList() // hashMapOf returns ComlpexHashMap because it is Generic override fun emptyMutableMap(): MutableMap = hashMapOf() } class PrimitiveMapJsTest : MapJsTest() { + test override fun constructors() { + HashMap() + HashMap(3) + HashMap(3, 0.5f) + + val map = HashMap(createTestMap()) + + assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList()) + assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList()) + } + override fun > Collection.toNormalizedList(): List = this.toSortedList() override fun emptyMutableMap(): MutableMap = HashMap() } class LinkedHashMapTest : MapJsTest() { + test override fun constructors() { + LinkedHashMap() + LinkedHashMap(3) + LinkedHashMap(3, 0.5f) + + val map = LinkedHashMap(createTestMap()) + + assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList()) + assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList()) + } + override fun > Collection.toNormalizedList(): List = this.toList() override fun emptyMutableMap(): MutableMap = LinkedHashMap() } @@ -284,6 +320,8 @@ abstract class MapJsTest { } } + test abstract fun constructors() + /* test fun createLinkedMap() { val map = linkedMapOf("c" to 3, "b" to 2, "a" to 1) diff --git a/libraries/stdlib/test/js/SetJsTest.kt b/libraries/stdlib/test/js/SetJsTest.kt index 0d2f8719823..6cf3a28a4a3 100644 --- a/libraries/stdlib/test/js/SetJsTest.kt +++ b/libraries/stdlib/test/js/SetJsTest.kt @@ -6,15 +6,50 @@ import java.util.HashSet import java.util.LinkedHashSet class ComplexSetJsTest : SetJsTest() { + // Helper function with generic parameter to force to use ComlpexHashMap + fun doTest() { + HashSet() + HashSet(3) + HashSet(3, 0.5f) + + val set = HashSet(data as HashSet) + + assertEquals(data, set) + } + + test override fun constructors() { + doTest() + } + // hashSetOf returns ComlpexHashSet because it is Generic override fun createEmptyMutableSet(): MutableSet = hashSetOf() } class PrimitiveSetJsTest : SetJsTest() { + test override fun constructors() { + HashSet() + HashSet(3) + HashSet(3, 0.5f) + + val set = HashSet(data) + + assertEquals(data, set) + } + override fun createEmptyMutableSet(): MutableSet = HashSet() } class LinkedHashSetTest : SetJsTest() { + test override fun constructors() { + LinkedHashSet() + LinkedHashSet(3) + LinkedHashSet(3, 0.5f) + + val set = LinkedHashSet(data) + + assertEquals(data, set) + } + override fun createEmptyMutableSet(): MutableSet = LinkedHashSet() } @@ -150,6 +185,8 @@ abstract class SetJsTest { } } + test abstract fun constructors() + //Helpers abstract fun createEmptyMutableSet(): MutableSet