initial psike of #KT-39 on collections (iterators required too), though hit #KT-1710

This commit is contained in:
James Strachan
2012-04-03 09:20:11 +01:00
parent dd29cb0509
commit 28b0792191
3 changed files with 107 additions and 49 deletions
+12 -9
View File
@@ -177,22 +177,25 @@ public inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <T, C: Collection<T>> java.lang.Iterable<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> java.lang.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> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
/** 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 [[List]] */
public inline fun <T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<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 [[Set]] */
public inline fun <T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<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 [[SortedSet]] */
public inline fun <T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<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 [[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>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
+47 -10
View File
@@ -39,12 +39,58 @@ public inline fun <T> java.util.Collection<T>.toArray() : Array<T> {
return answer as Array<T>
}
/** TODO these functions don't work when they generate the Array<T> versions when they are in JavaIterables */
/** Returns true if the collection is not empty */
public inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
/** Returns the Collection if its not null otherwise it returns the empty list */
public inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>
/** TODO these functions don't work when they generate the Array<T> versions when they are in JLIterables */
public inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList() : List<T> = toList().sort()
public inline fun <in T: java.lang.Comparable<T>> java.lang.Iterable<T>.toSortedList(comparator: java.util.Comparator<T>) : List<T> = toList().sort(comparator)
/**
* Creates a new [[List]] with the element added at the end
*
* @includeFunctionBody ../../test/CollectionTest.kt plus
*/
public inline fun <in T> java.util.Collection<T>.plus(element: T): List<in T> {
val list = to(ArrayList<T>())
list.add(element)
return list
}
/**
* Adds the element to this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt plusAssign
*/
public inline fun <in T, C: java.util.Collection<in T>> C.plusAssign(element: T): C {
add(element)
return this
}
/**
* Creates a new [[List]] with the element added at the end
*/
/*
public inline fun <in T> java.util.Collection<T>.plus(elements: java.lang.Iterable<T>): List<T> = toList().plusAssign(elements)
*/
/**
* Adds all the elements to this collection
*/
/*
public inline fun <in T, C: Collection<in T>> C.plusAssign(elements: java.lang.Iterable<T>): C {
addAll(elements.toCollection())
return this
}
*/
// List APIs
@@ -98,12 +144,3 @@ val <T> List<T>.tail : T?
val <T> List<T>.last : T?
get() = this.tail
/** Returns true if the collection is not empty */
public inline fun <T> java.util.Collection<T>.notEmpty() : Boolean = !this.isEmpty()
/** Returns the Collection if its not null otherwise it returns the empty list */
public inline fun <T> java.util.Collection<T>?.orEmpty() : Collection<T>
= if (this != null) this else Collections.EMPTY_LIST as Collection<T>