#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
*