#KT-1675 Fixed, renaming join() -> makeString() and adding an appendString() which reduces the possible number of appendables being created

This commit is contained in:
James Strachan
2012-04-02 20:03:09 +01:00
parent 1d342978d0
commit f2bf17f548
4 changed files with 107 additions and 17 deletions
@@ -17,7 +17,7 @@ public class CollectionTest extends TestCase {
public void testCollections() throws Exception { public void testCollections() throws Exception {
List<String> list = arrayList("foo", "bar"); List<String> list = arrayList("foo", "bar");
String text = makeString(list, ",", "(", ")"); String text = makeString(list, ",", "(", ")", -1);
System.out.println("Have text: " + text); System.out.println("Have text: " + text);
assertEquals("(foo,bar)", text); assertEquals("(foo,bar)", text);
@@ -29,6 +29,6 @@ public class CollectionTest extends TestCase {
}); });
System.out.println("Filtered list is " + actual); System.out.println("Filtered list is " + actual);
assertEquals("(bar)", makeString(actual, ",", "(", ")")); assertEquals("(bar)", makeString(actual, ",", "(", ")", -1));
} }
} }
@@ -5,6 +5,16 @@ import kotlin.util.*
import java.util.* import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/** /**
* Returns *true* if any elements match the given *predicate* * Returns *true* if any elements match the given *predicate*
* *
@@ -16,13 +26,22 @@ public inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
} }
/** /**
* Returns *true* if all elements match the given *predicate* * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* *
* @includeFunctionBody ../../test/CollectionTest.kt all * @includeFunctionBody ../../test/CollectionTest.kt makeString
*/ */
public inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> Array<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
for (element in this) if (!predicate(element)) return false buffer.append(prefix)
return true var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append("...")
buffer.append(postfix)
} }
/** /**
@@ -132,6 +151,17 @@ public inline fun <T, K> Array<T>.groupBy(result: Map<K, List<T>> = HashMap<K, L
return result return result
} }
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun <T> Array<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit)
return buffer.toString().sure()
}
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <T, L: List<in T>> Array<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L { public inline fun <T, L: List<in T>> Array<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break
@@ -3,6 +3,16 @@ package kotlin
import java.util.* import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/** /**
* Returns *true* if any elements match the given *predicate* * Returns *true* if any elements match the given *predicate*
* *
@@ -14,13 +24,22 @@ public inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boo
} }
/** /**
* Returns *true* if all elements match the given *predicate* * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* *
* @includeFunctionBody ../../test/CollectionTest.kt all * @includeFunctionBody ../../test/CollectionTest.kt makeString
*/ */
public inline fun <T> java.util.Iterator<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> java.util.Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
for (element in this) if (!predicate(element)) return false buffer.append(prefix)
return true var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append("...")
buffer.append(postfix)
} }
/** /**
@@ -130,6 +149,17 @@ public inline fun <T, K> java.util.Iterator<T>.groupBy(result: Map<K, List<T>> =
return result return result
} }
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun <T> java.util.Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit)
return buffer.toString().sure()
}
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <T, L: List<in T>> java.util.Iterator<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L { public inline fun <T, L: List<in T>> java.util.Iterator<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break
@@ -5,6 +5,16 @@ import kotlin.util.*
import java.util.* import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/** /**
* Returns *true* if any elements match the given *predicate* * Returns *true* if any elements match the given *predicate*
* *
@@ -16,13 +26,22 @@ public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
} }
/** /**
* Returns *true* if all elements match the given *predicate* * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* *
* @includeFunctionBody ../../test/CollectionTest.kt all * @includeFunctionBody ../../test/CollectionTest.kt makeString
*/ */
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean { public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
for (element in this) if (!predicate(element)) return false buffer.append(prefix)
return true var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append("...")
buffer.append(postfix)
} }
/** /**
@@ -132,6 +151,17 @@ public inline fun <T, K> Iterable<T>.groupBy(result: Map<K, List<T>> = HashMap<K
return result return result
} }
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit)
return buffer.toString().sure()
}
/** Returns a list containing the first elements that satisfy the given *predicate* */ /** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <T, L: List<in T>> Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L { public inline fun <T, L: List<in T>> Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break for (element in this) if (predicate(element)) result.add(element) else break