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;