JS stdlib: added missed constructors for HashSet, LinkedHashSet, HashMap and LinkedHashMap.

This commit is contained in:
Zalim Bashorov
2014-06-25 18:30:36 +04:00
parent 444932d4c1
commit 28228d23b1
6 changed files with 143 additions and 31 deletions
+16 -6
View File
@@ -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<T> {
fun compare(obj1: T, obj2: T): Int;
@@ -67,8 +73,9 @@ public open class LinkedList<E>() : AbstractList<E>() {
}
library
public open class HashSet<E>(capacity: Int = 0) : AbstractCollection<E>(), MutableSet<E> {
}
public open class HashSet<E>(
initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR
) : AbstractCollection<E>(), MutableSet<E>
library
public trait SortedSet<E> : Set<E> {
@@ -79,11 +86,12 @@ public open class TreeSet<E>() : AbstractCollection<E>(), MutableSet<E>, SortedS
}
library
public open class LinkedHashSet<E>() : AbstractCollection<E>(), MutableSet<E> {
}
public open class LinkedHashSet<E>(
initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR
) : HashSet<E>(initialCapacity, loadFactor), MutableSet<E>
library
public open class HashMap<K, V>(capacity: Int = 0) : MutableMap<K, V> {
public open class HashMap<K, V>(initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR) : MutableMap<K, V> {
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<K, V>(capacity: Int = 0) : MutableMap<K, V> {
}
library
public open class LinkedHashMap<K, V>(capacity: Int = 0) : HashMap<K, V>(capacity)
public open class LinkedHashMap<K, V>(
initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR, accessOrder: Boolean = false
) : HashMap<K, V>(initialCapacity, loadFactor)
library
public class NoSuchElementException(message: String? = null) : Exception() {}
+41
View File
@@ -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<E>(c: Collection<E>): HashSet<E> {
val set: HashSet<E> = HashSet(c.size())
set.addAll(c)
return set
}
public fun LinkedHashSet<E>(c: Collection<E>): HashSet<E> {
val set: LinkedHashSet<E> = LinkedHashSet(c.size())
set.addAll(c)
return set
}
public fun HashMap<K, V>(m: Map<K, V>): HashMap<K, V> {
val map: HashMap<K, V> = HashMap(m.size())
map.putAll(m)
return map
}
public fun LinkedHashMap<K, V>(m: Map<K, V>): LinkedHashMap<K, V> {
val map: LinkedHashMap<K, V> = LinkedHashMap(m.size())
map.putAll(m)
return map
}
@@ -101,6 +101,7 @@ public abstract class Config {
public static final List<String> LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList(
"/core/stringsCode.kt",
"/stdlib/domCode.kt",
"/core/javautilCode.kt",
"/stdlib/jutilCode.kt",
"/stdlib/testCode.kt"
);
+10 -25
View File
@@ -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.<Key, Value>} 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;
+38
View File
@@ -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<K : kotlin.Comparable<K>>() {
HashMap<K, Int>()
HashMap<K, Int>(3)
HashMap<K, Int>(3, 0.5f)
val map = HashMap<K, Int>(createTestMap() as HashMap<K, Int>)
assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList())
assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList())
}
test override fun constructors() {
doTest<String>()
}
override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toSortedList()
// hashMapOf returns ComlpexHashMap because it is Generic
override fun emptyMutableMap(): MutableMap<String, Int> = hashMapOf()
}
class PrimitiveMapJsTest : MapJsTest() {
test override fun constructors() {
HashMap<String, Int>()
HashMap<String, Int>(3)
HashMap<String, Int>(3, 0.5f)
val map = HashMap<String, Int>(createTestMap())
assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList())
assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList())
}
override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toSortedList()
override fun emptyMutableMap(): MutableMap<String, Int> = HashMap()
}
class LinkedHashMapTest : MapJsTest() {
test override fun constructors() {
LinkedHashMap<String, Int>()
LinkedHashMap<String, Int>(3)
LinkedHashMap<String, Int>(3, 0.5f)
val map = LinkedHashMap<String, Int>(createTestMap())
assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList())
assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList())
}
override fun <T : kotlin.Comparable<T>> Collection<T>.toNormalizedList(): List<T> = this.toList()
override fun emptyMutableMap(): MutableMap<String, Int> = 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)
+37
View File
@@ -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<T>() {
HashSet<T>()
HashSet<T>(3)
HashSet<T>(3, 0.5f)
val set = HashSet<T>(data as HashSet<T>)
assertEquals(data, set)
}
test override fun constructors() {
doTest<String>()
}
// hashSetOf returns ComlpexHashSet because it is Generic
override fun createEmptyMutableSet(): MutableSet<String> = hashSetOf<String>()
}
class PrimitiveSetJsTest : SetJsTest() {
test override fun constructors() {
HashSet<String>()
HashSet<String>(3)
HashSet<String>(3, 0.5f)
val set = HashSet<String>(data)
assertEquals(data, set)
}
override fun createEmptyMutableSet(): MutableSet<String> = HashSet<String>()
}
class LinkedHashSetTest : SetJsTest() {
test override fun constructors() {
LinkedHashSet<String>()
LinkedHashSet<String>(3)
LinkedHashSet<String>(3, 0.5f)
val set = LinkedHashSet<String>(data)
assertEquals(data, set)
}
override fun createEmptyMutableSet(): MutableSet<String> = LinkedHashSet<String>()
}
@@ -150,6 +185,8 @@ abstract class SetJsTest {
}
}
test abstract fun constructors()
//Helpers
abstract fun createEmptyMutableSet(): MutableSet<String>