refactored the to(Collection) methods to be called toCollection(Collection) to avoid clashing with a potentially new method called to(T) for making a pair/tuple/Map.Entry

This commit is contained in:
James Strachan
2012-04-16 19:59:12 +01:00
parent 89eb7ba4ac
commit 87e613a8c2
13 changed files with 62 additions and 53 deletions
@@ -193,25 +193,25 @@ public inline fun <T> Array<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <in T, C: Collection<in T>> Array<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> Array<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
public inline fun <in T> Array<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <in T> Array<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> Array<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> Array<T>.toCollection() : Collection<T> = to(ArrayList<T>())
public inline fun <in T> Array<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <in T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> Array<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> Array<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -48,7 +48,7 @@ public inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Coll
* @includeFunctionBody ../../test/CollectionTest.kt plus
*/
public inline fun <in T> Array<T>.plus(element: T): List<in T> {
val list = to(ArrayList<T>())
val list = toCollection(ArrayList<T>())
list.add(element)
return list
}
@@ -60,7 +60,7 @@ public inline fun <in T> Array<T>.plus(element: T): List<in T> {
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
*/
public inline fun <in T> Array<T>.plus(elements: Array<T>): List<T> {
val list = to(ArrayList<T>())
val list = toCollection(ArrayList<T>())
list.addAll(elements.toCollection())
return list
}
@@ -11,7 +11,7 @@ import java.util.*
//
/**
* Returns a new List containing the results of applying the given function to each element in this collection
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
@@ -19,7 +19,10 @@ public inline fun <T, R> Array<T>.map(transform : (T) -> R) : java.util.List<R>
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: Collection<in R>> Array<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
@@ -11,7 +11,7 @@ import java.util.*
//
/**
* Returns a new List containing the results of applying the given function to each element in this collection
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
@@ -19,7 +19,10 @@ public inline fun <T, R> java.lang.Iterable<T>.map(transform : (T) -> R) : java.
return mapTo(java.util.ArrayList<R>(), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: Collection<in R>> java.lang.Iterable<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
@@ -191,25 +191,25 @@ public inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <in T, C: Collection<in T>> java.util.Iterator<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> java.util.Iterator<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
public inline fun <in T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <in T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> java.util.Iterator<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> java.util.Iterator<T>.toCollection() : Collection<T> = to(ArrayList<T>())
public inline fun <in T> java.util.Iterator<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <in T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> java.util.Iterator<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -193,25 +193,25 @@ public inline fun <T> Iterable<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <in T, C: Collection<in T>> Iterable<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> Iterable<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
public inline fun <in T> Iterable<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <in T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> Iterable<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> Iterable<T>.toCollection() : Collection<T> = to(ArrayList<T>())
public inline fun <in T> Iterable<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <in T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> Iterable<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> Iterable<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -48,7 +48,7 @@ public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : C
* @includeFunctionBody ../../test/CollectionTest.kt plus
*/
public inline fun <in T> Iterable<T>.plus(element: T): List<in T> {
val list = to(ArrayList<T>())
val list = toCollection(ArrayList<T>())
list.add(element)
return list
}
@@ -60,7 +60,7 @@ public inline fun <in T> Iterable<T>.plus(element: T): List<in T> {
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
*/
public inline fun <in T> Iterable<T>.plus(elements: Iterable<T>): List<T> {
val list = to(ArrayList<T>())
val list = toCollection(ArrayList<T>())
list.addAll(elements.toCollection())
return list
}
@@ -11,7 +11,7 @@ import java.util.*
//
/**
* Returns a new List containing the results of applying the given function to each element in this collection
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
@@ -19,7 +19,10 @@ public inline fun <T, R> Iterable<T>.map(transform : (T) -> R) : java.util.List<
return mapTo(java.util.ArrayList<R>(), transform)
}
/** Transforms each element of this collection with the given function then adds the results to the given collection */
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: Collection<in R>> Iterable<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
@@ -190,25 +190,25 @@ public inline fun <T> java.lang.Iterable<T>.reverse() : List<T> {
}
/** Copies all elements into the given collection */
public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.to(result: C) : C {
public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>())
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = to(ArrayList<T>())
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/** Copies all elements into a [[SortedSet]] */
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
@@ -45,7 +45,7 @@ public inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collecti
* @includeFunctionBody ../../test/CollectionTest.kt plus
*/
public inline fun <in T> java.lang.Iterable<T>.plus(element: T): List<in T> {
val list = to(ArrayList<T>())
val list = toCollection(ArrayList<T>())
list.add(element)
return list
}
@@ -57,7 +57,7 @@ public inline fun <in T> java.lang.Iterable<T>.plus(element: T): List<in T> {
* @includeFunctionBody ../../test/CollectionTest.kt plusCollection
*/
public inline fun <in T> java.lang.Iterable<T>.plus(elements: java.lang.Iterable<T>): List<T> {
val list = to(ArrayList<T>())
val list = toCollection(ArrayList<T>())
list.addAll(elements.toCollection())
return list
}
+5 -5
View File
@@ -12,23 +12,23 @@ val Collection<*>.empty : Boolean
get() = isEmpty()
/** Returns a new ArrayList with a variable number of initial elements */
public inline fun arrayList<T>(vararg values: T) : ArrayList<T> = values.to(ArrayList<T>(values.size))
public inline fun arrayList<T>(vararg values: T) : ArrayList<T> = values.toCollection(ArrayList<T>(values.size))
/** Returns a new LinkedList with a variable number of initial elements */
public inline fun linkedList<T>(vararg values: T) : LinkedList<T> = values.to(LinkedList<T>())
public inline fun linkedList<T>(vararg values: T) : LinkedList<T> = values.toCollection(LinkedList<T>())
/** Returns a new HashSet with a variable number of initial elements */
public inline fun hashSet<T>(vararg values: T) : HashSet<T> = values.to(HashSet<T>(values.size))
public inline fun hashSet<T>(vararg values: T) : HashSet<T> = values.toCollection(HashSet<T>(values.size))
/**
* Returns a new [[SortedSet]] with the initial elements
*/
public inline fun sortedSet<T>(vararg values: T) : TreeSet<T> = values.to(TreeSet<T>())
public inline fun sortedSet<T>(vararg values: T) : TreeSet<T> = values.toCollection(TreeSet<T>())
/**
* Returns a new [[SortedSet]] with the given *comparator* and the initial elements
*/
public inline fun sortedSet<T>(comparator: Comparator<T>, vararg values: T) : TreeSet<T> = values.to(TreeSet<T>(comparator))
public inline fun sortedSet<T>(comparator: Comparator<T>, vararg values: T) : TreeSet<T> = values.toCollection(TreeSet<T>(comparator))
/**
* Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple
+7 -7
View File
@@ -36,7 +36,7 @@ public fun <erased T> java.util.Enumeration<T>.iterator(): Iterator<T> = object:
/**
Add iterated elements to given container
*/
public fun <T,U: Collection<in T>> Iterator<T>.to(container: U) : U {
public fun <T,U: Collection<in T>> Iterator<T>.toCollection(container: U) : U {
while(hasNext)
container.add(next())
return container
@@ -45,33 +45,33 @@ public fun <T,U: Collection<in T>> Iterator<T>.to(container: U) : U {
/**
Add iterated elements to java.util.ArrayList
*/
public inline fun <T> Iterator<T>.toArrayList() : ArrayList<T> = to(ArrayList<T>())
public inline fun <T> Iterator<T>.toArrayList() : ArrayList<T> = toCollection(ArrayList<T>())
/**
Add iterated elements to java.util.LinkedList
*/
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/**
Add iterated elements to java.util.HashSet
*/
public inline fun <T> Iterator<T>.toHashSet() : HashSet<T> = to(HashSet<T>())
public inline fun <T> Iterator<T>.toHashSet() : HashSet<T> = toCollection(HashSet<T>())
/**
* Add iterated elements to a [[LinkedHashSet]] to preserve insertion order
*/
public inline fun <T> Iterator<T>.toLinkedSet() : LinkedHashSet<T> = to(LinkedHashSet<T>())
public inline fun <T> Iterator<T>.toLinkedSet() : LinkedHashSet<T> = toCollection(LinkedHashSet<T>())
/**
* Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator
* for the type
*/
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
/**
* Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator
*/
public inline fun <T> Iterator<T>.toSortedSet(comparator: Comparator<T>) : SortedSet<T> = to(TreeSet<T>(comparator))
public inline fun <T> Iterator<T>.toSortedSet(comparator: Comparator<T>) : SortedSet<T> = toCollection(TreeSet<T>(comparator))
/**
Run function f
@@ -156,23 +156,23 @@ public inline fun <T> T?.reverse(): T? {
}
/** Copies the collection into the given collection */
public inline fun <T, C: Collection<T>> T?.to(result: C): C {
public inline fun <T, C: Collection<T>> T?.toCollection(result: C): C {
if (this != null)
result.add(this)
return result
}
/** Converts the collection into a LinkedList */
public inline fun <T> T?.toLinkedList(): LinkedList<T> = this.to(LinkedList<T>())
public inline fun <T> T?.toLinkedList(): LinkedList<T> = this.toCollection(LinkedList<T>())
/** Converts the collection into a List */
public inline fun <T> T?.toList(): List<T> = this.to(ArrayList<T>())
public inline fun <T> T?.toList(): List<T> = this.toCollection(ArrayList<T>())
/** Converts the collection into a Set */
public inline fun <T> T?.toSet(): Set<T> = this.to(HashSet<T>())
public inline fun <T> T?.toSet(): Set<T> = this.toCollection(HashSet<T>())
/** Converts the collection into a SortedSet */
public inline fun <T> T?.toSortedSet(): SortedSet<T> = this.to(TreeSet<T>())
public inline fun <T> T?.toSortedSet(): SortedSet<T> = this.toCollection(TreeSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun <in T> T?.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {