diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt index d8e2e51a4d1..a4a77729aa4 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt @@ -28,7 +28,7 @@ public inline fun Array.any(predicate: (T) -> Boolean) : Boolean { /** * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied * - * @includeFunctionBody ../../test/CollectionTest.kt makeString + * @includeFunctionBody ../../test/CollectionTest.kt appendString */ public inline fun Array.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { buffer.append(prefix) @@ -180,22 +180,25 @@ public inline fun Array.reverse() : List { } /** Copies all elements into the given collection */ -public inline fun > Array.to(result: C) : C { +public inline fun > Array.to(result: C) : C { for (element in this) result.add(element) return result } -/** Copies all elements into a [[LinkedList]] */ -public inline fun Array.toLinkedList() : LinkedList = to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +public inline fun Array.toLinkedList() : LinkedList = to(LinkedList()) /** Copies all elements into a [[List]] */ -public inline fun Array.toList() : List = to(ArrayList()) +public inline fun Array.toList() : List = to(ArrayList()) + +/** Copies all elements into a [[List] */ +public inline fun Array.toCollection() : Collection = to(ArrayList()) /** Copies all elements into a [[Set]] */ -public inline fun Array.toSet() : Set = to(HashSet()) +public inline fun Array.toSet() : Set = to(HashSet()) /** Copies all elements into a [[SortedSet]] */ -public inline fun Array.toSortedSet() : SortedSet = to(TreeSet()) +public inline fun Array.toSortedSet() : SortedSet = to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt index 9e5b7f7d59c..f3bbcbc9cba 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterablesLazy.kt @@ -42,6 +42,23 @@ public inline fun Array?.filterNotNull() : List = filterNotNullTo Array.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) +/** + * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + * + * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls + */ +public inline fun Array?.requireNoNulls() : List { + val list = ArrayList() + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this") + } else { + list.add(element) + } + } + return list +} + /** * Returns a list containing the first *n* elements * diff --git a/libraries/stdlib/src/generated/JUnilIterablesFromJUtilCollections.kt b/libraries/stdlib/src/generated/JUtilIterablesFromJUtilCollections.kt similarity index 100% rename from libraries/stdlib/src/generated/JUnilIterablesFromJUtilCollections.kt rename to libraries/stdlib/src/generated/JUtilIterablesFromJUtilCollections.kt diff --git a/libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt similarity index 90% rename from libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt rename to libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt index 7acb05ebde7..a147f2efe82 100644 --- a/libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt @@ -26,7 +26,7 @@ public inline fun java.util.Iterator.any(predicate: (T) -> Boolean) : Boo /** * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied * - * @includeFunctionBody ../../test/CollectionTest.kt makeString + * @includeFunctionBody ../../test/CollectionTest.kt appendString */ public inline fun java.util.Iterator.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { buffer.append(prefix) @@ -178,22 +178,25 @@ public inline fun java.util.Iterator.reverse() : List { } /** Copies all elements into the given collection */ -public inline fun > java.util.Iterator.to(result: C) : C { +public inline fun > java.util.Iterator.to(result: C) : C { for (element in this) result.add(element) return result } -/** Copies all elements into a [[LinkedList]] */ -public inline fun java.util.Iterator.toLinkedList() : LinkedList = to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +public inline fun java.util.Iterator.toLinkedList() : LinkedList = to(LinkedList()) /** Copies all elements into a [[List]] */ -public inline fun java.util.Iterator.toList() : List = to(ArrayList()) +public inline fun java.util.Iterator.toList() : List = to(ArrayList()) + +/** Copies all elements into a [[List] */ +public inline fun java.util.Iterator.toCollection() : Collection = to(ArrayList()) /** Copies all elements into a [[Set]] */ -public inline fun java.util.Iterator.toSet() : Set = to(HashSet()) +public inline fun java.util.Iterator.toSet() : Set = to(HashSet()) /** Copies all elements into a [[SortedSet]] */ -public inline fun java.util.Iterator.toSortedSet() : SortedSet = to(TreeSet()) +public inline fun java.util.Iterator.toSortedSet() : SortedSet = to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt index 6ca1df36585..29a82738db7 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt @@ -28,7 +28,7 @@ public inline fun Iterable.any(predicate: (T) -> Boolean) : Boolean { /** * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied * - * @includeFunctionBody ../../test/CollectionTest.kt makeString + * @includeFunctionBody ../../test/CollectionTest.kt appendString */ public inline fun Iterable.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { buffer.append(prefix) @@ -180,22 +180,25 @@ public inline fun Iterable.reverse() : List { } /** Copies all elements into the given collection */ -public inline fun > Iterable.to(result: C) : C { +public inline fun > Iterable.to(result: C) : C { for (element in this) result.add(element) return result } -/** Copies all elements into a [[LinkedList]] */ -public inline fun Iterable.toLinkedList() : LinkedList = to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +public inline fun Iterable.toLinkedList() : LinkedList = to(LinkedList()) /** Copies all elements into a [[List]] */ -public inline fun Iterable.toList() : List = to(ArrayList()) +public inline fun Iterable.toList() : List = to(ArrayList()) + +/** Copies all elements into a [[List] */ +public inline fun Iterable.toCollection() : Collection = to(ArrayList()) /** Copies all elements into a [[Set]] */ -public inline fun Iterable.toSet() : Set = to(HashSet()) +public inline fun Iterable.toSet() : Set = to(HashSet()) /** Copies all elements into a [[SortedSet]] */ -public inline fun Iterable.toSortedSet() : SortedSet = to(TreeSet()) +public inline fun Iterable.toSortedSet() : SortedSet = to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt index b4c66b93d7a..3fbfefb60fd 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterablesLazy.kt @@ -42,6 +42,23 @@ public inline fun Iterable?.filterNotNull() : List = filterNotNullTo< */ public inline fun Iterable.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) +/** + * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + * + * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls + */ +public inline fun Iterable?.requireNoNulls() : List { + val list = ArrayList() + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this") + } else { + list.add(element) + } + } + return list +} + /** * Returns a list containing the first *n* elements * diff --git a/libraries/stdlib/src/kotlin/Iterators.kt b/libraries/stdlib/src/kotlin/Iterators.kt index 84e8435185d..53a5dd04765 100644 --- a/libraries/stdlib/src/kotlin/Iterators.kt +++ b/libraries/stdlib/src/kotlin/Iterators.kt @@ -110,6 +110,19 @@ private class FlatMapIterator(val iterator : java.util.Iterator, val tr } } +/** + * Returns an iterator containing all the non-*null* elements, lazily throwing an [[IllegalArgumentException]] + if there are any null elements + * + * @includeFunctionBody ../../test/iterators/IteratorsTest.kt requireNoNulls + */ +public inline fun java.util.Iterator.requireNoNulls(): java.util.Iterator { + return map{ + if (it == null) throw IllegalArgumentException("null element in iterator $this") else it + } +} + + /** * Returns an iterator restricted to the first *n* elements * diff --git a/libraries/stdlib/src/kotlin/JLangIterables.kt b/libraries/stdlib/src/kotlin/JLangIterables.kt index bbd0e979971..5f1bf8009a2 100644 --- a/libraries/stdlib/src/kotlin/JLangIterables.kt +++ b/libraries/stdlib/src/kotlin/JLangIterables.kt @@ -182,20 +182,20 @@ public inline fun > java.lang.Iterable.to(result: C return result } -/** Copies all elements into a [[LinkedList]] if its not already a [[LinkedList]] */ -public inline fun java.lang.Iterable.toLinkedList() : LinkedList = if (this is LinkedList) this else to(LinkedList()) +/** Copies all elements into a [[LinkedList]] */ +public inline fun java.lang.Iterable.toLinkedList() : LinkedList = to(LinkedList()) -/** Copies all elements into a [[List]] if its not already a [[List]] */ -public inline fun java.lang.Iterable.toList() : List = if (this is List) this else to(ArrayList()) +/** Copies all elements into a [[List]] */ +public inline fun java.lang.Iterable.toList() : List = to(ArrayList()) -/** Copies all elements into a [[List]] if it is not already a [[Collection]] */ -public inline fun java.lang.Iterable.toCollection() : Collection = if (this is Collection) this else to(ArrayList()) +/** Copies all elements into a [[List] */ +public inline fun java.lang.Iterable.toCollection() : Collection = to(ArrayList()) -/** Copies all elements into a [[Set]] if its not already a [[Set]] */ -public inline fun java.lang.Iterable.toSet() : Set = if (this is Set) this else to(HashSet()) +/** Copies all elements into a [[Set]] */ +public inline fun java.lang.Iterable.toSet() : Set = to(HashSet()) -/** Copies all elements into a [[SortedSet]] if its not already a [[SortedSet]] */ -public inline fun java.lang.Iterable.toSortedSet() : SortedSet = if (this is SortedSet) this else to(TreeSet()) +/** Copies all elements into a [[SortedSet]] */ +public inline fun java.lang.Iterable.toSortedSet() : SortedSet = to(TreeSet()) /** TODO figure out necessary variance/generics ninja stuff... :) diff --git a/libraries/stdlib/src/kotlin/JLangIterablesLazy.kt b/libraries/stdlib/src/kotlin/JLangIterablesLazy.kt index 5829abe2dd5..8804b1cb7d7 100644 --- a/libraries/stdlib/src/kotlin/JLangIterablesLazy.kt +++ b/libraries/stdlib/src/kotlin/JLangIterablesLazy.kt @@ -39,6 +39,23 @@ public inline fun java.lang.Iterable?.filterNotNull() : List = filter */ public inline fun java.lang.Iterable.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) +/** + * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + * + * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls + */ +public inline fun java.lang.Iterable?.requireNoNulls() : List { + val list = ArrayList() + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this") + } else { + list.add(element) + } + } + return list +} + /** * Returns a list containing the first *n* elements * diff --git a/libraries/stdlib/src/kotlin/test/Test.kt b/libraries/stdlib/src/kotlin/test/Test.kt index 5183ac193c4..9abd522b90e 100644 --- a/libraries/stdlib/src/kotlin/test/Test.kt +++ b/libraries/stdlib/src/kotlin/test/Test.kt @@ -101,13 +101,15 @@ public fun fails(block: ()-> Unit): Throwable? { } /** Asserts that a block fails with a specific exception being thrown */ -public fun failsWith(block: ()-> Unit) { +public fun failsWith(block: ()-> Any): T { try { block() asserter.fail("Expected an exception to be thrown") + throw IllegalStateException("Should have failed") } catch (e: T) { //println("Caught expected exception: $e") // OK + return e } } diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index b2f3aea751a..5d82b637a7b 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -271,6 +271,18 @@ class CollectionTest { */ } + test fun requireNoNulls() { + val data = arrayList("foo", "bar") + val notNull = data.requireNoNulls() + assertEquals(arrayList("foo", "bar"), notNull) + + val hasNulls = arrayList("foo", null, "bar") + failsWith { + // should throw an exception as we have a null + hasNulls.requireNoNulls() + } + } + test fun reverse() { val data = arrayList("foo", "bar") val rev = data.reverse() diff --git a/libraries/stdlib/test/GenerateStandardLib.kt b/libraries/stdlib/test/GenerateStandardLib.kt index bb238bc645f..5e9470c716d 100644 --- a/libraries/stdlib/test/GenerateStandardLib.kt +++ b/libraries/stdlib/test/GenerateStandardLib.kt @@ -63,7 +63,7 @@ fun main(args: Array) { it.replaceAll("java.lang.Iterable) { it.replaceAll("java.util.Collection { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } } } - Test fun filterAndTakeWhileExtractTheElementsWithinRange() { + test fun filterAndTakeWhileExtractTheElementsWithinRange() { assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList()) } - Test fun foldReducesTheFirstNElements() { + test fun foldReducesTheFirstNElements() { val sum = { (a: Int, b: Int) -> a + b } assertEquals(arrayList(13, 21, 34, 55, 89).fold(0, sum), fibonacci().filter { it > 10 }.take(5).fold(0, sum)) } - Test fun takeExtractsTheFirstNElements() { + test fun takeExtractsTheFirstNElements() { assertEquals(arrayList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList()) } - Test fun mapAndTakeWhileExtractTheTransformedElements() { + test fun mapAndTakeWhileExtractTheTransformedElements() { assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList()) } - Test fun flatMapAndTakeExtractTheTransformedElements() { + test fun flatMapAndTakeExtractTheTransformedElements() { fun intToBinaryDigits() = { (i: Int) -> val binary = Integer.toBinaryString(i).sure() var index = 0 @@ -47,11 +48,24 @@ class IteratorsTest { assertEquals(expected, fibonacci().flatMap(intToBinaryDigits()).take(10).toList()) } - Test fun joinConcatenatesTheFirstNElementsAboveAThreshold() { + test fun joinConcatenatesTheFirstNElementsAboveAThreshold() { assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5)) } - Test fun toStringJoinsNoMoreThanTheFirstTenElements() { + test fun requireNoNulls() { + val iter = arrayList("foo", "bar").iterator() + val notNull = iter.requireNoNulls() + assertEquals(arrayList("foo", "bar"), notNull.toList()) + + val iterWithNulls = arrayList("foo", null, "bar").iterator() + val notNull2 = iterWithNulls.requireNoNulls() + failsWith { + // should throw an exception as we have a null + notNull2.toList() + } + } + + test fun toStringJoinsNoMoreThanTheFirstTenElements() { assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().makeString(limit = 10)) assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.makeString(limit = 10)) assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.makeString())