diff --git a/js/js.libraries/src/core/javaio.kt b/js/js.libraries/src/core/javaio.kt new file mode 100644 index 00000000000..c529fa0fa65 --- /dev/null +++ b/js/js.libraries/src/core/javaio.kt @@ -0,0 +1,4 @@ +package java.io + +library +class IOException(message: String = "") : Exception() {} diff --git a/js/js.libraries/src/core/javalang.kt b/js/js.libraries/src/core/javalang.kt index 5f0325f9e63..a2d8dadb7c8 100644 --- a/js/js.libraries/src/core/javalang.kt +++ b/js/js.libraries/src/core/javalang.kt @@ -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 { + 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? +} + diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 44b6c7f9b06..b5716f84446 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -28,9 +28,36 @@ val Collections = object { // TODO should be immutable! private val emptyList = ArrayList() + private val emptyMap = HashMap() public val EMPTY_LIST: List - get() = emptyList as List + get() = emptyList() + + public val EMPTY_MAP: Map + get() = emptyMap() + + public fun emptyList(): List = emptyList as List + public fun emptyMap(): Map = emptyMap as Map + + public fun sort(list: List): Unit { + throw UnsupportedOperationException() + } + + public fun sort(list: List, comparator: java.util.Comparator): Unit { + throw UnsupportedOperationException() + } + + public fun reverse(list: List): 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 { open fun clear() : Unit open fun keySet() : java.util.Set open fun values() : java.util.Collection + + open fun entrySet() : java.util.Set> +// open fun equals(o : Any?) : Boolean +// open fun hashCode() : Int + + trait Entry { + 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() : java.util.Map { public override fun containsValue(value : Any?) : Boolean = js.noImpl public override fun keySet() : java.util.Set = js.noImpl public override fun values() : java.util.Collection = js.noImpl + public override fun entrySet() : java.util.Set> = js.noImpl } library @@ -200,8 +240,11 @@ public open class LinkedList() : List { } 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 } diff --git a/js/js.libraries/src/stdlib/jutil.kt b/js/js.libraries/src/stdlib/jutil.kt new file mode 100644 index 00000000000..7575941199d --- /dev/null +++ b/js/js.libraries/src/stdlib/jutil.kt @@ -0,0 +1,42 @@ +package kotlin + +import java.util.* + +/** Returns a new ArrayList with a variable number of initial elements */ +public inline fun arrayList(vararg values: T) : ArrayList { + val list = ArrayList() + 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 java.util.Map.map(transform: (java.util.Map.Entry) -> R) : java.util.List { + return mapTo(java.util.ArrayList(), 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 java.util.Map.mapValues(transform : (java.util.Map.Entry) -> R): java.util.Map { + return mapValuesTo(java.util.HashMap(), 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 java.util.Collection.map(transform : (T) -> R) : java.util.List { + return mapTo(java.util.ArrayList(), transform) +} 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 290d05cf416..add122e82a2 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -57,6 +57,7 @@ public abstract class Config { "/jquery/ui.kt", "/core/javautil.kt", "/core/javalang.kt", + "/core/javaio.kt", "/core/date.kt", "/core/core.kt", "/core/math.kt", @@ -78,6 +79,10 @@ public abstract class Config { */ @NotNull public static final List LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList( + /** + TODO include ASAP... + "/stdlib/jutil.kt", + */ "/stdlib/test.kt", "/core/stringDefs.kt", "/core/strings.kt" @@ -91,6 +96,17 @@ public abstract class Config { @NotNull public static final List STDLIB_FILE_NAMES = Arrays.asList( "/kotlin/Preconditions.kt", + /* + TODO include ASAP - right now it generates an issue + "/kotlin/Iterators.kt", + "/kotlin/JUtil.kt", + "/kotlin/JUtilCollections.kt", + "/kotlin/JUtilMaps.kt", + "/kotlin/JLangIterables.kt", + "/kotlin/JLangIterablesLazy.kt", + "/kotlin/JLangIterablesSpecial.kt", + "/kotlin/support/AbstractIterator.kt" + */ "/kotlin/Strings.kt", "/kotlin/test/Test.kt" ); diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index 0f29bec6638..9d929114313 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -37,6 +37,7 @@ Kotlin.Exceptions.IllegalStateException = Kotlin.$createClass(Kotlin.Exception); Kotlin.Exceptions.IndexOutOfBoundsException = Kotlin.$createClass(Kotlin.Exception); Kotlin.Exceptions.UnsupportedOperationException = Kotlin.$createClass(Kotlin.Exception); + Kotlin.Exceptions.IOException = Kotlin.$createClass(Kotlin.Exception); Kotlin.throwNPE = function() { throw Kotlin.$new(Kotlin.Exceptions.NullPointerException)(); diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt index cdcd498c19c..119144eba7f 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > Array.takeWhileTo(result: C, predi return result } +/** Copies all elements into the given collection */ +public inline fun > Array.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun Array.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > Array.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun Array.toList() : List = toCollection(ArrayList /** Copies all elements into a [[List] */ public inline fun Array.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun Array.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun Array.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun Array.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun Array.toSortedList(transform: fun(T) : java.lang.Com return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun Array.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun Array.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt index 8e2d471496d..0283e602155 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/ArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/ArraysFromJUtilCollections.kt index d4b95aa3132..577a824017c 100644 --- a/libraries/stdlib/src/generated/ArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/ArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun Array.map(transform : (T) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt index f292457ba9a..90267714087 100644 --- a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > BooleanArray.takeWhileTo(result: C, p return result } +/** Copies all elements into the given collection */ +public inline fun > BooleanArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun BooleanArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > BooleanArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun BooleanArray.toList() : List = toCollection(ArrayLis /** Copies all elements into a [[List] */ public inline fun BooleanArray.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun BooleanArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun BooleanArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun BooleanArray.toSortedList(transform: fun(Boolean) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun BooleanArray.toSortedList(transform: fun(Boolean) : java.lang return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun BooleanArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun BooleanArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt index 7331440db24..1cc25372d29 100644 --- a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt index a1c356e5280..97b7a0a50f8 100644 --- a/libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun BooleanArray.map(transform : (Boolean) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt index 7967d0763ef..c9e7e87ee31 100644 --- a/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > ByteArray.takeWhileTo(result: C, predica return result } +/** Copies all elements into the given collection */ +public inline fun > ByteArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun ByteArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > ByteArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun ByteArray.toList() : List = toCollection(ArrayList = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun ByteArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun ByteArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun ByteArray.toSortedList(transform: fun(Byte) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun ByteArray.toSortedList(transform: fun(Byte) : java.lang.Compa return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun ByteArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun ByteArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt index 15aa3837e86..62fad694c76 100644 --- a/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt index fbba332497c..6081f3b358e 100644 --- a/libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > ByteArray.mapTo(result: C, transform : (Byte) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun ByteArray.map(transform : (Byte) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > ByteArray.mapTo(result: C, transform : (Byte) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt index a41ece2b3dc..d82f20af2c3 100644 --- a/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > CharArray.takeWhileTo(result: C, predica return result } +/** Copies all elements into the given collection */ +public inline fun > CharArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun CharArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > CharArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun CharArray.toList() : List = toCollection(ArrayList = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun CharArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun CharArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun CharArray.toSortedList(transform: fun(Char) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun CharArray.toSortedList(transform: fun(Char) : java.lang.Compa return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun CharArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun CharArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt index 3449500b286..89e83a26999 100644 --- a/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt index 75bb55556f6..1bf0a1a3c3a 100644 --- a/libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > CharArray.mapTo(result: C, transform : (Char) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun CharArray.map(transform : (Char) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > CharArray.mapTo(result: C, transform : (Char) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt index 9cd7d285d7f..255028138b7 100644 --- a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > DoubleArray.takeWhileTo(result: C, pre return result } +/** Copies all elements into the given collection */ +public inline fun > DoubleArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun DoubleArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > DoubleArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun DoubleArray.toList() : List = toCollection(ArrayList< /** Copies all elements into a [[List] */ public inline fun DoubleArray.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun DoubleArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun DoubleArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun DoubleArray.toSortedList(transform: fun(Double) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun DoubleArray.toSortedList(transform: fun(Double) : java.lang.C return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun DoubleArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun DoubleArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt index b57415e3ba7..96d4d993b99 100644 --- a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt index b182c5f1db4..a60c4183d1f 100644 --- a/libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > DoubleArray.mapTo(result: C, transform : (Double) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun DoubleArray.map(transform : (Double) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > DoubleArray.mapTo(result: C, transform : (Double) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt index 4682062829f..2e2244b2479 100644 --- a/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > FloatArray.takeWhileTo(result: C, predi return result } +/** Copies all elements into the given collection */ +public inline fun > FloatArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun FloatArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > FloatArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun FloatArray.toList() : List = toCollection(ArrayList = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun FloatArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun FloatArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun FloatArray.toSortedList(transform: fun(Float) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun FloatArray.toSortedList(transform: fun(Float) : java.lang.Com return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun FloatArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun FloatArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt index c56e59e89be..bbd7452767b 100644 --- a/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt index 2c13a246384..3f48ca12a83 100644 --- a/libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > FloatArray.mapTo(result: C, transform : (Float) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun FloatArray.map(transform : (Float) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > FloatArray.mapTo(result: C, transform : (Float) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt index 1958da8c110..1cceeded2fe 100644 --- a/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > IntArray.takeWhileTo(result: C, predicate return result } +/** Copies all elements into the given collection */ +public inline fun > IntArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun IntArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > IntArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun IntArray.toList() : List = toCollection(ArrayList() /** Copies all elements into a [[List] */ public inline fun IntArray.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun IntArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun IntArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun IntArray.toSortedList(transform: fun(Int) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun IntArray.toSortedList(transform: fun(Int) : java.lang.Compara return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun IntArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun IntArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt index e3c9eaff3d6..822c84a041f 100644 --- a/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt index 12cfac0c1d3..af07262266b 100644 --- a/libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > IntArray.mapTo(result: C, transform : (Int) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun IntArray.map(transform : (Int) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > IntArray.mapTo(result: C, transform : (Int) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/JUtilIterablesFromJUtilCollections.kt b/libraries/stdlib/src/generated/JUtilIterablesFromJUtilCollections.kt index 1458a2c16d2..582943aa31e 100644 --- a/libraries/stdlib/src/generated/JUtilIterablesFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/JUtilIterablesFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > java.lang.Iterable.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun java.lang.Iterable.map(transform : (T) -> R) : java.util.List { return mapTo(java.util.ArrayList(), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > java.lang.Iterable.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt index 575b828ed9f..7b6470913f7 100644 --- a/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin import java.util.* @@ -230,21 +230,20 @@ public inline fun > java.util.Iterator.takeWhileTo(res return result } +/** Copies all elements into the given collection */ +public inline fun > java.util.Iterator.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun java.util.Iterator.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > java.util.Iterator.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -256,12 +255,6 @@ public inline fun java.util.Iterator.toList() : List = toCollection /** Copies all elements into a [[List] */ public inline fun java.util.Iterator.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun java.util.Iterator.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun java.util.Iterator.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun java.util.Iterator.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { @@ -270,3 +263,20 @@ public inline fun java.util.Iterator.toSortedList(transform: fun(T) : return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun java.util.Iterator.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun java.util.Iterator.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt index 9611b341340..dcb5693d07f 100644 --- a/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > LongArray.takeWhileTo(result: C, predica return result } +/** Copies all elements into the given collection */ +public inline fun > LongArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun LongArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > LongArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun LongArray.toList() : List = toCollection(ArrayList = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun LongArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun LongArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun LongArray.toSortedList(transform: fun(Long) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun LongArray.toSortedList(transform: fun(Long) : java.lang.Compa return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun LongArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun LongArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt index ad0fc0c1a9f..993e253ffc5 100644 --- a/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt index 1713752d989..6c87ed391c9 100644 --- a/libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > LongArray.mapTo(result: C, transform : (Long) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun LongArray.map(transform : (Long) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > LongArray.mapTo(result: C, transform : (Long) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt index 6de8195cbd2..c017562d7ee 100644 --- a/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > ShortArray.takeWhileTo(result: C, predi return result } +/** Copies all elements into the given collection */ +public inline fun > ShortArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun ShortArray.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > ShortArray.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun ShortArray.toList() : List = toCollection(ArrayList = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun ShortArray.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun ShortArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun ShortArray.toSortedList(transform: fun(Short) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun ShortArray.toSortedList(transform: fun(Short) : java.lang.Com return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun ShortArray.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun ShortArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt index ea6e6498dba..c974b448b78 100644 --- a/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt index 895363bc9cf..c39e05ec95b 100644 --- a/libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun ShortArray.map(transform : (Short) -> R) : java.util.List { return mapTo(java.util.ArrayList(this.size), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt index c626ff7f513..7fcd57a9e13 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterables.kt // -package kotlin - -import kotlin.util.* import java.util.* @@ -232,21 +232,20 @@ public inline fun > Iterable.takeWhileTo(result: C, pr return result } +/** Copies all elements into the given collection */ +public inline fun > Iterable.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun Iterable.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > Iterable.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -258,12 +257,6 @@ public inline fun Iterable.toList() : List = toCollection(ArrayList /** Copies all elements into a [[List] */ public inline fun Iterable.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun Iterable.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun Iterable.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { @@ -272,3 +265,20 @@ public inline fun Iterable.toSortedList(transform: fun(T) : java.lang. return answer } */ + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JLangIterablesJVM.kt +// + + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun Iterable.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun Iterable.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt index ee5fb7e4a0f..c37e08dcf93 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt @@ -1,3 +1,6 @@ +package kotlin + +import kotlin.util.* // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,9 +9,6 @@ // Generated from input file: src/kotlin/JLangIterablesLazy.kt // -package kotlin - -import kotlin.util.* import java.util.ArrayList import java.util.Collection diff --git a/libraries/stdlib/src/generated/StandardFromJUtilCollections.kt b/libraries/stdlib/src/generated/StandardFromJUtilCollections.kt index 84afabd11aa..339ad392afe 100644 --- a/libraries/stdlib/src/generated/StandardFromJUtilCollections.kt +++ b/libraries/stdlib/src/generated/StandardFromJUtilCollections.kt @@ -1,3 +1,4 @@ +package kotlin // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt @@ -6,7 +7,33 @@ // Generated from input file: src/kotlin/JUtilCollections.kt // -package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > Iterable.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +// +// Generated from input file: src/kotlin/JUtilCollectionsJVM.kt +// + import java.util.* @@ -25,13 +52,3 @@ import java.util.* public inline fun Iterable.map(transform : (T) -> R) : java.util.List { return mapTo(java.util.ArrayList(), transform) } - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Iterable.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/kotlin/Iterators.kt b/libraries/stdlib/src/kotlin/Iterators.kt index 38b3bf5a942..507ef5d71d0 100644 --- a/libraries/stdlib/src/kotlin/Iterators.kt +++ b/libraries/stdlib/src/kotlin/Iterators.kt @@ -10,22 +10,6 @@ import java.util.Collections */ public inline fun iterate(nextFunction: () -> T?) : java.util.Iterator = FunctionIterator(nextFunction) -/** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */ -public inline fun java.util.Iterator.filterIsInstance(klass: Class): java.util.Iterator = FilterIsIterator(this, klass) - -private class FilterIsIterator(val iterator : java.util.Iterator, val klass: Class) : AbstractIterator() { - override protected fun computeNext(): Unit { - while (iterator.hasNext()) { - val next = iterator.next() - if (klass.isInstance(next)) { - setNext(next as R) - return - } - } - done() - } -} - /** * Returns an iterator over elements which match the given *predicate* * diff --git a/libraries/stdlib/src/kotlin/IteratorsJVM.kt b/libraries/stdlib/src/kotlin/IteratorsJVM.kt new file mode 100644 index 00000000000..7ba4b5dcafb --- /dev/null +++ b/libraries/stdlib/src/kotlin/IteratorsJVM.kt @@ -0,0 +1,21 @@ +package kotlin + +import kotlin.support.* +import java.util.Collections + +/** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */ +public inline fun java.util.Iterator.filterIsInstance(klass: Class): java.util.Iterator = FilterIsIterator(this, klass) + +private class FilterIsIterator(val iterator : java.util.Iterator, val klass: Class) : AbstractIterator() { + override protected fun computeNext(): Unit { + while (iterator.hasNext()) { + val next = iterator.next() + if (klass.isInstance(next)) { + setNext(next as R) + return + } + } + done() + } +} + diff --git a/libraries/stdlib/src/kotlin/JLangIterables.kt b/libraries/stdlib/src/kotlin/JLangIterables.kt index 13cff107023..7ff0c93f501 100644 --- a/libraries/stdlib/src/kotlin/JLangIterables.kt +++ b/libraries/stdlib/src/kotlin/JLangIterables.kt @@ -222,21 +222,20 @@ public inline fun > java.lang.Iterable.takeWhileTo(res return result } +/** Copies all elements into the given collection */ +public inline fun > java.lang.Iterable.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + /** * Reverses the order the elements into a list * * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun java.lang.Iterable.reverse() : List { - val answer = LinkedList() - for (element in this) answer.addFirst(element) - return answer -} - -/** Copies all elements into the given collection */ -public inline fun > java.lang.Iterable.toCollection(result: C) : C { - for (element in this) result.add(element) - return result + val list = toList() + return list.reverse() } /** Copies all elements into a [[LinkedList]] */ @@ -248,12 +247,6 @@ public inline fun java.lang.Iterable.toList() : List = toCollection /** Copies all elements into a [[List] */ public inline fun java.lang.Iterable.toCollection() : Collection = toCollection(ArrayList()) -/** Copies all elements into a [[Set]] */ -public inline fun java.lang.Iterable.toSet() : Set = toCollection(HashSet()) - -/** Copies all elements into a [[SortedSet]] */ -public inline fun java.lang.Iterable.toSortedSet() : SortedSet = toCollection(TreeSet()) - /** TODO figure out necessary variance/generics ninja stuff... :) public inline fun java.lang.Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { diff --git a/libraries/stdlib/src/kotlin/JLangIterablesJVM.kt b/libraries/stdlib/src/kotlin/JLangIterablesJVM.kt new file mode 100644 index 00000000000..673adc01b52 --- /dev/null +++ b/libraries/stdlib/src/kotlin/JLangIterablesJVM.kt @@ -0,0 +1,10 @@ +package kotlin + +import java.util.* + +/** Copies all elements into a [[Set]] */ +public inline fun java.lang.Iterable.toSet() : Set = toCollection(HashSet()) + +/** Copies all elements into a [[SortedSet]] */ +public inline fun java.lang.Iterable.toSortedSet() : SortedSet = toCollection(TreeSet()) + diff --git a/libraries/stdlib/src/kotlin/JLang.kt b/libraries/stdlib/src/kotlin/JLangJVM.kt similarity index 100% rename from libraries/stdlib/src/kotlin/JLang.kt rename to libraries/stdlib/src/kotlin/JLangJVM.kt diff --git a/libraries/stdlib/src/kotlin/JUtil.kt b/libraries/stdlib/src/kotlin/JUtil.kt index 60137a14966..3b0c743de9d 100644 --- a/libraries/stdlib/src/kotlin/JUtil.kt +++ b/libraries/stdlib/src/kotlin/JUtil.kt @@ -1,7 +1,6 @@ package kotlin import java.util.* -import java.util.regex.Pattern /** Returns the size of the collection */ val Collection<*>.size : Int @@ -11,81 +10,6 @@ val Collection<*>.size : Int val Collection<*>.empty : Boolean get() = isEmpty() -/** Returns a new ArrayList with a variable number of initial elements */ -public inline fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) - -/** Returns a new LinkedList with a variable number of initial elements */ -public inline fun linkedList(vararg values: T) : LinkedList = values.toCollection(LinkedList()) - -/** Returns a new HashSet with a variable number of initial elements */ -public inline fun hashSet(vararg values: T) : HashSet = values.toCollection(HashSet(values.size)) - -/** - * Returns a new [[SortedSet]] with the initial elements - */ -public inline fun sortedSet(vararg values: T) : TreeSet = values.toCollection(TreeSet()) - -/** - * Returns a new [[SortedSet]] with the given *comparator* and the initial elements - */ -public inline fun sortedSet(comparator: Comparator, vararg values: T) : TreeSet = values.toCollection(TreeSet(comparator)) - -/** - * Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple - * is the key and the second value is the value - * - * @includeFunctionBody ../../test/MapTest.kt createUsingTuples - */ -public inline fun hashMap(vararg values: #(K,V)): HashMap { - val answer = HashMap(values.size) - /** - TODO replace by this simpler call when we can pass vararg values into other methods - answer.putAll(values) - */ - for (v in values) { - answer.put(v._1, v._2) - } - return answer -} - -/** - * Returns a new [[SortedMap]] populated with the given tuple values where the first value in each tuple - * is the key and the second value is the value - * - * @includeFunctionBody ../../test/MapTest.kt createSortedMap - */ -public inline fun sortedMap(vararg values: #(K,V)): SortedMap { - val answer = TreeMap() - /** - TODO replace by this simpler call when we can pass vararg values into other methods - answer.putAll(values) - */ - for (v in values) { - answer.put(v._1, v._2) - } - return answer -} - -/** - * Returns a new [[LinkedHashMap]] populated with the given tuple values where the first value in each tuple - * is the key and the second value is the value. This map preserves insertion order so iterating through - * the map's entries will be in the same order - * - * @includeFunctionBody ../../test/MapTest.kt createLinkedMap - */ -public inline fun linkedMap(vararg values: #(K,V)): LinkedHashMap { - val answer = LinkedHashMap(values.size) - /** - TODO replace by this simpler call when we can pass vararg values into other methods - answer.putAll(values) - */ - for (v in values) { - answer.put(v._1, v._2) - } - return answer -} - - val Collection<*>.indices : IntRange get() = 0..size-1 @@ -105,7 +29,7 @@ public inline fun java.util.Collection.notEmpty() : Boolean = !this.isEmp /** Returns the Collection if its not null otherwise it returns the empty list */ public inline fun java.util.Collection?.orEmpty() : Collection - = if (this != null) this else Collections.EMPTY_LIST as Collection + = if (this != null) this else Collections.emptyList() as Collection /** TODO these functions don't work when they generate the Array versions when they are in JLIterables */ @@ -116,6 +40,16 @@ public inline fun > java.lang.Iterable.toSorted // List APIs +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun List.reverse() : List { + Collections.reverse(this) + return this +} + public inline fun > List.sort() : List { Collections.sort(this) return this @@ -128,11 +62,7 @@ public inline fun List.sort(comparator: java.util.Comparator) : Lis /** Returns the List if its not null otherwise returns the empty list */ public inline fun java.util.List?.orEmpty() : java.util.List - = if (this != null) this else Collections.EMPTY_LIST as java.util.List - -/** Returns the Set if its not null otherwise returns the empty set */ -public inline fun java.util.Set?.orEmpty() : java.util.Set - = if (this != null) this else Collections.EMPTY_SET as java.util.Set + = if (this != null) this else Collections.emptyList() as java.util.List /** TODO figure out necessary variance/generics ninja stuff... :) diff --git a/libraries/stdlib/src/kotlin/JUtilCollections.kt b/libraries/stdlib/src/kotlin/JUtilCollections.kt index 72a49f3dca6..9d6b3abb55a 100644 --- a/libraries/stdlib/src/kotlin/JUtilCollections.kt +++ b/libraries/stdlib/src/kotlin/JUtilCollections.kt @@ -9,15 +9,6 @@ import java.util.* // See [[GenerateStandardLib.kt]] for more details // -/** - * 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 java.util.Collection.map(transform : (T) -> R) : java.util.List { - return mapTo(java.util.ArrayList(this.size), transform) -} - /** * Transforms each element of this collection with the given *transform* function and * adds each return value to the given *results* collection diff --git a/libraries/stdlib/src/kotlin/JUtilCollectionsJVM.kt b/libraries/stdlib/src/kotlin/JUtilCollectionsJVM.kt new file mode 100644 index 00000000000..b080136e8a4 --- /dev/null +++ b/libraries/stdlib/src/kotlin/JUtilCollectionsJVM.kt @@ -0,0 +1,19 @@ +package kotlin + +import java.util.* + +// +// This file contains methods which are optimised for working on Collection / Array collections where the size +// could be used to implement a more optimal solution +// +// See [[GenerateStandardLib.kt]] for more details +// + +/** + * 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 java.util.Collection.map(transform : (T) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), transform) +} diff --git a/libraries/stdlib/src/kotlin/JUtilJVM.kt b/libraries/stdlib/src/kotlin/JUtilJVM.kt new file mode 100644 index 00000000000..03bf201ab86 --- /dev/null +++ b/libraries/stdlib/src/kotlin/JUtilJVM.kt @@ -0,0 +1,85 @@ +package kotlin + +import java.util.* + +/** Returns a new LinkedList with a variable number of initial elements */ +public inline fun linkedList(vararg values: T) : LinkedList = values.toCollection(LinkedList()) + +/** Returns a new HashSet with a variable number of initial elements */ +public inline fun hashSet(vararg values: T) : HashSet = values.toCollection(HashSet(values.size)) + +/** + * Returns a new [[SortedSet]] with the initial elements + */ +public inline fun sortedSet(vararg values: T) : TreeSet = values.toCollection(TreeSet()) + +/** + * Returns a new [[SortedSet]] with the given *comparator* and the initial elements + */ +public inline fun sortedSet(comparator: Comparator, vararg values: T) : TreeSet = values.toCollection(TreeSet(comparator)) + +/** + * Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value + * + * @includeFunctionBody ../../test/MapTest.kt createUsingTuples + */ +public inline fun hashMap(vararg values: #(K,V)): HashMap { + val answer = HashMap(values.size) + /** + TODO replace by this simpler call when we can pass vararg values into other methods + answer.putAll(values) + */ + for (v in values) { + answer.put(v._1, v._2) + } + return answer +} + +/** + * Returns a new [[SortedMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value + * + * @includeFunctionBody ../../test/MapTest.kt createSortedMap + */ +public inline fun sortedMap(vararg values: #(K,V)): SortedMap { + val answer = TreeMap() + /** + TODO replace by this simpler call when we can pass vararg values into other methods + answer.putAll(values) + */ + for (v in values) { + answer.put(v._1, v._2) + } + return answer +} + +/** + * Returns a new [[LinkedHashMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value. This map preserves insertion order so iterating through + * the map's entries will be in the same order + * + * @includeFunctionBody ../../test/MapTest.kt createLinkedMap + */ +public inline fun linkedMap(vararg values: #(K,V)): LinkedHashMap { + val answer = LinkedHashMap(values.size) + /** + TODO replace by this simpler call when we can pass vararg values into other methods + answer.putAll(values) + */ + for (v in values) { + answer.put(v._1, v._2) + } + return answer +} + + +/** Returns the Set if its not null otherwise returns the empty set */ +public inline fun java.util.Set?.orEmpty() : java.util.Set + = if (this != null) this else Collections.EMPTY_SET as java.util.Set + +/** Returns a new ArrayList with a variable number of initial elements */ +public inline fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) + + + diff --git a/libraries/stdlib/src/kotlin/JUtilMaps.kt b/libraries/stdlib/src/kotlin/JUtilMaps.kt index fb2012a788d..85a6ae84aa8 100644 --- a/libraries/stdlib/src/kotlin/JUtilMaps.kt +++ b/libraries/stdlib/src/kotlin/JUtilMaps.kt @@ -1,16 +1,11 @@ package kotlin -import java.util.Map as JMap -import java.util.Map.Entry as JEntry import java.util.Collection import java.util.Collections import java.util.HashMap import java.util.List -import java.util.LinkedHashMap +import java.util.Map as JMap import java.util.Map -import java.util.TreeMap -import java.util.SortedMap -import java.util.Comparator // Map APIs @@ -27,15 +22,15 @@ public fun JMap.set(key : K, value : V) : V? = this.put(key, value) /** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */ public inline fun java.util.Map?.orEmpty() : java.util.Map -= if (this != null) this else Collections.EMPTY_MAP as java.util.Map += if (this != null) this else Collections.emptyMap() as java.util.Map /** Returns the key of the entry */ -val JEntry.key : K +val Map.Entry.key : K get() = getKey().sure() /** Returns the value of the entry */ -val JEntry.value : V +val Map.Entry.value : V get() = getValue().sure() /** @@ -79,15 +74,6 @@ public inline fun java.util.Map.iterator(): java.util.Iterator java.util.Map.map(transform: (java.util.Map.Entry) -> R) : java.util.List { - return mapTo(java.util.ArrayList(this.size), transform) -} - /** * Transforms each [[Map.Entry]] in this [[Map]] with the given *transform* function and * adds each return value to the given *results* collection @@ -98,16 +84,6 @@ public inline fun > java.util.Map.mapTo(result: return result } - -/** - * 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 java.util.Map.mapValues(transform : (java.util.Map.Entry) -> R): java.util.Map { - return mapValuesTo(java.util.HashMap(this.size), transform) -} - /** * Populates the given *result* [[Map]] with the value returned by applying the *transform* function on each [[Map.Entry]] in this [[Map]] */ @@ -128,26 +104,6 @@ public inline fun java.util.Map.putAll(vararg values: #(K,V)): Unit { } } -/** - * Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained - */ -public inline fun java.util.Map.toLinkedMap(): LinkedHashMap = toMap(LinkedHashMap(size)) as LinkedHashMap - -/** - * Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order - * - * @includeFunctionBody ../../test/MapTest.kt toSortedMap - */ -public inline fun java.util.Map.toSortedMap(): SortedMap = toMap(TreeMap()) as SortedMap - -/** - * Converts this [[Map]] to a [[SortedMap]] using the given *comparator* so that iteration order will be in the order - * defined by the comparator - * - * @includeFunctionBody ../../test/MapTest.kt toSortedMapWithComparator - */ -public inline fun java.util.Map.toSortedMap(comparator: Comparator): SortedMap = toMap(TreeMap(comparator)) as SortedMap - /** * Copies the entries in this [[Map]] to the given *map* */ diff --git a/libraries/stdlib/src/kotlin/JUtilMapsJVM.kt b/libraries/stdlib/src/kotlin/JUtilMapsJVM.kt new file mode 100644 index 00000000000..586bbb8f722 --- /dev/null +++ b/libraries/stdlib/src/kotlin/JUtilMapsJVM.kt @@ -0,0 +1,52 @@ +package kotlin + +import java.util.Comparator +import java.util.LinkedHashMap +import java.util.Map as JMap +import java.util.Map +import java.util.Map.Entry as JEntry +import java.util.SortedMap +import java.util.TreeMap + +// Map APIs + +/** + * Converts this [[Map]] to a [[LinkedHashMap]] so future insertion orders are maintained + */ +public inline fun java.util.Map.toLinkedMap(): LinkedHashMap = toMap(LinkedHashMap(size)) as LinkedHashMap + +/** + * Converts this [[Map]] to a [[SortedMap]] so iteration order will be in key order + * + * @includeFunctionBody ../../test/MapTest.kt toSortedMap + */ +public inline fun java.util.Map.toSortedMap(): SortedMap = toMap(TreeMap()) as SortedMap + +/** + * Converts this [[Map]] to a [[SortedMap]] using the given *comparator* so that iteration order will be in the order + * defined by the comparator + * + * @includeFunctionBody ../../test/MapTest.kt toSortedMapWithComparator + */ +public inline fun java.util.Map.toSortedMap(comparator: Comparator): SortedMap = toMap(TreeMap(comparator)) as SortedMap + + +/** + * 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 java.util.Map.map(transform: (java.util.Map.Entry) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 java.util.Map.mapValues(transform : (java.util.Map.Entry) -> R): java.util.Map { + return mapValuesTo(java.util.HashMap(this.size), transform) +} + diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt index af1eb23df92..eb24787714b 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt +++ b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt @@ -1,24 +1,34 @@ package org.jetbrains.kotlin.tools import java.io.* +import java.util.List fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> String) { - println("Parsing $inputFile and writing $outFile") + generateFile(outFile, header, arrayList(inputFile), f) +} +fun generateFile(outFile: File, header: String, inputFile: File, jvmFile: File, f: (String)-> String) { + generateFile(outFile, header, arrayList(inputFile, jvmFile), f) +} + +fun generateFile(outFile: File, header: String, inputFiles: List, f: (String)-> String) { outFile.getParentFile()?.mkdirs() val writer = PrintWriter(FileWriter(outFile)) try { - writer.println(""" + writer.println(header) + + for (file in inputFiles) { + writer.println(""" // // NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt // See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib // -// Generated from input file: $inputFile +// Generated from input file: $file // """) - writer.println(header) - val reader = FileReader(inputFile).buffered() + println("Parsing $file and writing $outFile") + val reader = FileReader(file).buffered() try { // TODO ideally we'd use a filterNot() here :) val iter = reader.lineIterator() @@ -34,6 +44,7 @@ fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> S reader.close() reader.close() } + } } finally { writer.close() } @@ -62,7 +73,7 @@ fun main(args: Array) { val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") // JLangIterables - Generic iterable stuff - generateFile(File(outDir, "ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) { + generateFile(File(outDir, "ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt"), File(srcDir, "JLangIterablesJVM.kt")) { it.replaceAll("java.lang.Iterable) { replaceAll("iterator.hasNext\\(\\)", "iterator.hasNext") } - generateFile(File(outDir, "${arrayName}ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) { + generateFile(File(outDir, "${arrayName}ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt"), File(srcDir, "JLangIterablesJVM.kt")) { replace(it) } generateFile(File(outDir, "${arrayName}ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) { @@ -87,7 +98,7 @@ fun main(args: Array) { } } - generateFile(File(outDir, "StandardFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) { + generateFile(File(outDir, "StandardFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt"), File(srcDir, "JLangIterablesJVM.kt")) { it.replaceAll("java.lang.Iterable) { it.replaceAll("java.lang.Iterable java.util.Collection", "${arrayName}Array"). replaceAll("java.util.Collection", "${arrayName}Array")) } } - generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) { + generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt"), File(srcDir, "JUtilCollectionsJVM.kt")) { it.replaceAll("java.util.Collection + @@ -67,6 +68,15 @@ +