refactored the standard library so most of the collection APIs in the standard library can be compiled to JS

This commit is contained in:
James Strachan
2012-06-21 15:53:39 +01:00
parent da31a9696c
commit 8a6b752357
51 changed files with 1010 additions and 533 deletions
+4
View File
@@ -0,0 +1,4 @@
package java.io
library
class IOException(message: String = "") : Exception() {}
+12
View File
@@ -1,5 +1,6 @@
package java.lang
import java.io.IOException
import java.util.Iterator
import js.library
@@ -28,3 +29,14 @@ class UnsupportedOperationException(message: String = "") : Exception() {}
library
class NumberFormatException(message: String = "") : Exception() {}
public trait Comparable<T> {
fun compareTo(that: T): Int
}
public trait Appendable {
open fun append(csq: CharSequence?): Appendable?
open fun append(csq: CharSequence?, start: Int, end: Int): Appendable?
open fun append(c: Char): Appendable?
}
+46 -3
View File
@@ -28,9 +28,36 @@ val Collections = object {
// TODO should be immutable!
private val emptyList = ArrayList<Any>()
private val emptyMap = HashMap<Any,Any>()
public val <T> EMPTY_LIST: List<T>
get() = emptyList as List<T>
get() = emptyList<T>()
public val <K,V> EMPTY_MAP: Map<K,V>
get() = emptyMap<K,V>()
public fun <T> emptyList(): List<T> = emptyList as List<T>
public fun <K,V> emptyMap(): Map<K,V> = emptyMap as Map<K,V>
public fun <in T> sort(list: List<T>): Unit {
throw UnsupportedOperationException()
}
public fun <in T> sort(list: List<T>, comparator: java.util.Comparator<T>): Unit {
throw UnsupportedOperationException()
}
public fun <T> reverse(list: List<T>): Unit {
val size = list.size()
for (i in 0.upto(size / 2)) {
val i2 = size - i
val tmp = list[i]
list[i] = list[i2]
list[i2] = tmp
}
}
}
library
@@ -163,6 +190,18 @@ public trait Map<K, V> {
open fun clear() : Unit
open fun keySet() : java.util.Set<K>
open fun values() : java.util.Collection<V>
open fun entrySet() : java.util.Set<Entry<K, V>>
// open fun equals(o : Any?) : Boolean
// open fun hashCode() : Int
trait Entry<K, V> {
open fun getKey() : K
open fun getValue() : V
open fun setValue(value : V) : V
// open fun equals(o : Any?) : Boolean
// open fun hashCode() : Int
}
}
library
@@ -178,6 +217,7 @@ public open class HashMap<K, V>() : java.util.Map<K, V> {
public override fun containsValue(value : Any?) : Boolean = js.noImpl
public override fun keySet() : java.util.Set<K> = js.noImpl
public override fun values() : java.util.Collection<V> = js.noImpl
public override fun entrySet() : java.util.Set<Map.Entry<K, V>> = js.noImpl
}
library
@@ -200,8 +240,11 @@ public open class LinkedList<E>() : List<E> {
}
library
public class StringBuilder() {
public fun append(obj : Any) : StringBuilder = js.noImpl
public class StringBuilder() : Appendable {
override fun append(c: Char): Appendable? = js.noImpl
override fun append(csq: CharSequence?): Appendable? = js.noImpl
override fun append(csq: CharSequence?, start: Int, end: Int): Appendable? = js.noImpl
public fun append(obj : Any?) : StringBuilder = js.noImpl
public fun toString() : String = js.noImpl
}
+42
View File
@@ -0,0 +1,42 @@
package kotlin
import java.util.*
/** Returns a new ArrayList with a variable number of initial elements */
public inline fun arrayList<T>(vararg values: T) : ArrayList<T> {
val list = ArrayList<T>()
for (value in values) {
list.add(value)
}
return list
}
/**
* Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <K,V,R> java.util.Map<K,V>.map(transform: (java.util.Map.Entry<K,V>) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}
/**
* Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/MapTest.kt mapValues
*/
public inline fun <K,V,R> java.util.Map<K,V>.mapValues(transform : (java.util.Map.Entry<K,V>) -> R): java.util.Map<K,R> {
return mapValuesTo(java.util.HashMap<K,R>(), transform)
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}