#KT-1391 Fixed

This commit is contained in:
James Strachan
2012-04-03 10:40:21 +01:00
parent 28b0792191
commit 153b668d3e
13 changed files with 143 additions and 42 deletions
@@ -28,7 +28,7 @@ public inline fun <T> Array<T>.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 <T> Array<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
buffer.append(prefix)
@@ -180,22 +180,25 @@ public inline fun <T> Array<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> Array<T>.to(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <T> Array<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> Array<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> Array<T>.toCollection() : Collection<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -42,6 +42,23 @@ public inline fun <T> Array<T?>?.filterNotNull() : List<T> = filterNotNullTo<T,
*/
public inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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 <in T> Array<T?>?.requireNoNulls() : List<T> {
val list = ArrayList<T>()
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
*
@@ -26,7 +26,7 @@ public inline fun <T> java.util.Iterator<T>.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 <T> java.util.Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
buffer.append(prefix)
@@ -178,22 +178,25 @@ public inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> java.util.Iterator<T>.to(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> java.util.Iterator<T>.toCollection() : Collection<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -28,7 +28,7 @@ public inline fun <T> Iterable<T>.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 <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
buffer.append(prefix)
@@ -180,22 +180,25 @@ public inline fun <T> Iterable<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> Iterable<T>.to(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> Iterable<T>.toCollection() : Collection<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -42,6 +42,23 @@ public inline fun <T> Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<
*/
public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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 <in T> Iterable<T?>?.requireNoNulls() : List<T> {
val list = ArrayList<T>()
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
*
+13
View File
@@ -110,6 +110,19 @@ private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, 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 <in T> java.util.Iterator<T?>.requireNoNulls(): java.util.Iterator<T> {
return map<T?, T>{
if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
}
}
/**
* Returns an iterator restricted to the first *n* elements
*
+10 -10
View File
@@ -182,20 +182,20 @@ public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.to(result: C
return result
}
/** Copies all elements into a [[LinkedList]] if its not already a [[LinkedList]] */
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = if (this is LinkedList<T>) this else to(LinkedList<T>())
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** Copies all elements into a [[List]] if its not already a [[List]] */
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = if (this is List<T>) this else to(ArrayList<T>())
/** Copies all elements into a [[List]] */
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>())
/** Copies all elements into a [[List]] if it is not already a [[Collection]] */
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = if (this is Collection<T>) this else to(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = to(ArrayList<T>())
/** Copies all elements into a [[Set]] if its not already a [[Set]] */
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = if (this is Set<T>) this else to(HashSet<T>())
/** Copies all elements into a [[Set]] */
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
/** Copies all elements into a [[SortedSet]] if its not already a [[SortedSet]] */
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = if (this is SortedSet<T>) this else to(TreeSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -39,6 +39,23 @@ public inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : List<T> = filter
*/
public inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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 <in T> java.lang.Iterable<T?>?.requireNoNulls() : List<T> {
val list = ArrayList<T>()
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
*
+3 -1
View File
@@ -101,13 +101,15 @@ public fun fails(block: ()-> Unit): Throwable? {
}
/** Asserts that a block fails with a specific exception being thrown */
public fun <T: Throwable> failsWith(block: ()-> Unit) {
public fun <T: Throwable> 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
}
}