link more documentation to test cases and filled out the test cases a little

This commit is contained in:
James Strachan
2012-03-27 12:26:20 +01:00
parent 31fd665913
commit 133afa8327
9 changed files with 106 additions and 58 deletions
+11
View File
@@ -4,6 +4,8 @@ import java.io.ByteArrayInputStream
import java.util.Arrays
import java.nio.charset.Charset
import java.util.List
import java.util.ArrayList
// Array "constructor"
inline fun <T> array(vararg t : T) : Array<T> = t
@@ -121,3 +123,12 @@ inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
/** Returns the array if its not null or else returns an empty array */
inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
inline fun CharArray.toList(): List<Character> {
val list = ArrayList<Character>(this.size)
for (c in this) {
if (c != null) {
list.add(Character(c))
}
}
return list
}
@@ -2,7 +2,11 @@ package kotlin
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 function to each element in this collection
*
* @includeFunction ../../test/CollectionTest.kt map
*/
inline fun <T, R> java.util.Collection<T>.map(transform : (T) -> R) : java.util.List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -9,19 +9,33 @@ import java.util.*
// See [[GenerateStandardLib.kt]] for more details
//
/** Returns a new List containing all elements in this collection which match the given predicate */
/**
* Returns a new List containing all elements in this collection which match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filter
*/
inline fun <T> java.lang.Iterable<T>.filter(predicate: (T)-> Boolean) : Collection<T> = filterTo(java.util.ArrayList<T>(), predicate)
/** Returns a List containing all the non null elements in this collection */
/**
* Returns a List containing all the non null elements in this collection
*
* @includeFunction ../../test/CollectionTest.kt filterNotNull
*/
inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : Collection<T> = filterNotNullTo<T, java.util.ArrayList<T>>(java.util.ArrayList<T>())
/** Returns a new collection containing all elements in this collection which do not match the given predicate */
/**
* Returns a new collection containing all elements in this collection which do not match the given predicate
*
* @includeFunction ../../test/CollectionTest.kt filterNot
*/
inline fun <T> java.lang.Iterable<T>.filterNot(predicate: (T)-> Boolean) : Collection<T> = filterNotTo(ArrayList<T>(), predicate)
/**
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
*/
* Returns the result of transforming each item in the collection to a one or more values which
* are concatenated together into a single collection
*
* @includeFunction ../../test/CollectionTest.kt flatMap
*/
inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> {
return flatMapTo(ArrayList<R>(), transform)
}
@@ -28,8 +28,8 @@ fun <T> java.lang.Iterable<T>.count() : Int {
* Get the first element in the collection.
*
* Will throw an exception if there are no elements
* TODO: Specify type of the exception
*/
// TODO: Specify type of the exception
inline fun <T> java.lang.Iterable<T>.first() : T {
if (this is AbstractList<T>) {
return this.get(0)
@@ -46,8 +46,10 @@ inline fun <T> java.lang.Iterable<T>.first() : T {
* last item by iterating through the all items.
*
* Will throw an exception if there are no elements.
* TODO: Specify type of the exception
*
* @includeFunction ../../test/CollectionTest.kt last
*/
// TODO: Specify type of the exception
fun <T> java.lang.Iterable<T>.last() : T {
if (this is List<T>) {
return this.get(this.size() - 1);
@@ -71,7 +73,6 @@ fun <T> java.lang.Iterable<T>.last() : T {
* method will be used.
*/
fun <T> java.lang.Iterable<T>.contains(item : T) : Boolean {
kotlin.io.println("!!!!!")
if (this is java.util.AbstractCollection<T>) {
return this.contains(item);
}
+11 -3
View File
@@ -20,7 +20,7 @@ val JMap<*,*>.empty : Boolean
/** Provides [] access to maps */
fun <K, V> JMap<K, V>.set(key : K, value : V) = this.put(key, value)
/** Returns the Mao if its not null otherwise it returns the empty Map */
/** Returns the [[Map]] if its not null otherwise it returns the empty [[Map]] */
inline fun <K,V> java.util.Map<K,V>?.orEmpty() : java.util.Map<K,V>
= if (this != null) this else Collections.EMPTY_MAP as java.util.Map<K,V>
@@ -35,7 +35,11 @@ inline fun <K,V> java.util.Map<K,V>?.orEmpty() : java.util.Map<K,V>
//val <K,V> JEntry<K,V>.value : V
// get() = getValue().sure()
/** Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key */
/**
* Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key
*
* @includeFunction ../../test/MapTest.kt getOrElse
*/
inline fun <K,V> java.util.Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
@@ -45,7 +49,11 @@ inline fun <K,V> java.util.Map<K,V>.getOrElse(key: K, defaultValue: ()-> V) : V
}
}
/** Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned */
/**
* Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned
*
* @includeFunction ../../test/MapTest.kt getOrElse
*/
inline fun <K,V> java.util.Map<K,V>.getOrPut(key: K, defaultValue: ()-> V) : V {
val current = this.get(key)
if (current != null) {
+4
View File
@@ -1,6 +1,8 @@
package kotlin
import java.io.StringReader
import java.util.List
import java.util.ArrayList
inline fun String.lastIndexOf(str: String) = (this as java.lang.String).lastIndexOf(str)
@@ -52,6 +54,8 @@ inline fun String.getBytes() = (this as java.lang.String).getBytes().sure()
inline fun String.toCharArray() = (this as java.lang.String).toCharArray().sure()
inline fun String.toCharList(): List<Character> = toCharArray().toList()
inline fun String.format(format : String, vararg args : Any?) = java.lang.String.format(format, args).sure()
inline fun String.split(regex : String) = (this as java.lang.String).split(regex)