improved the docs and test cases

This commit is contained in:
James Strachan
2012-04-03 19:05:49 +01:00
parent ef3305ac96
commit 3ed819bfe1
4 changed files with 33 additions and 16 deletions
@@ -28,9 +28,12 @@ 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 * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* *
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString * @includeFunctionBody ../../test/CollectionTest.kt appendString
*/ */
public inline fun <T> Array<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { public inline fun <T> Array<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix) buffer.append(prefix)
var count = 0 var count = 0
for (element in this) { for (element in this) {
@@ -40,7 +43,7 @@ public inline fun <T> Array<T>.appendString(buffer: Appendable, separator: Strin
buffer.append(text) buffer.append(text)
} else break } else break
} }
if (limit >= 0 && count > limit) buffer.append("...") if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix) buffer.append(postfix)
} }
@@ -152,13 +155,16 @@ public inline fun <T, K> Array<T>.groupBy(result: Map<K, List<T>> = HashMap<K, L
} }
/** /**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
* *
* @includeFunctionBody ../../test/CollectionTest.kt appendString * @includeFunctionBody ../../test/CollectionTest.kt appendString
*/ */
public inline fun <T> Array<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String { public inline fun <T> Array<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder() val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit) appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString().sure() return buffer.toString().sure()
} }
@@ -26,9 +26,12 @@ 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 * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* *
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString * @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 { public inline fun <T> java.util.Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix) buffer.append(prefix)
var count = 0 var count = 0
for (element in this) { for (element in this) {
@@ -38,7 +41,7 @@ public inline fun <T> java.util.Iterator<T>.appendString(buffer: Appendable, sep
buffer.append(text) buffer.append(text)
} else break } else break
} }
if (limit >= 0 && count > limit) buffer.append("...") if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix) buffer.append(postfix)
} }
@@ -150,13 +153,16 @@ public inline fun <T, K> java.util.Iterator<T>.groupBy(result: Map<K, List<T>> =
} }
/** /**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
* *
* @includeFunctionBody ../../test/CollectionTest.kt appendString * @includeFunctionBody ../../test/CollectionTest.kt appendString
*/ */
public inline fun <T> java.util.Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String { public inline fun <T> java.util.Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder() val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit) appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString().sure() return buffer.toString().sure()
} }
@@ -28,9 +28,12 @@ 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 * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* *
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString * @includeFunctionBody ../../test/CollectionTest.kt appendString
*/ */
public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix) buffer.append(prefix)
var count = 0 var count = 0
for (element in this) { for (element in this) {
@@ -40,7 +43,7 @@ public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: St
buffer.append(text) buffer.append(text)
} else break } else break
} }
if (limit >= 0 && count > limit) buffer.append("...") if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix) buffer.append(postfix)
} }
@@ -152,13 +155,16 @@ public inline fun <T, K> Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K
} }
/** /**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
* *
* @includeFunctionBody ../../test/CollectionTest.kt appendString * @includeFunctionBody ../../test/CollectionTest.kt appendString
*/ */
public inline fun <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String { public inline fun <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder() val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit) appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString().sure() return buffer.toString().sure()
} }
-1
View File
@@ -231,7 +231,6 @@ class CollectionTest {
} }
test fun makeString() { test fun makeString() {
val data = arrayList("foo", "bar") val data = arrayList("foo", "bar")
val text = data.makeString("-", "<", ">") val text = data.makeString("-", "<", ">")