diff --git a/libraries/kotlin-java/src/test/java/test/kotlin/jtests/CollectionTest.java b/libraries/kotlin-java/src/test/java/test/kotlin/jtests/CollectionTest.java index 3530409ff15..21bbf49de2f 100644 --- a/libraries/kotlin-java/src/test/java/test/kotlin/jtests/CollectionTest.java +++ b/libraries/kotlin-java/src/test/java/test/kotlin/jtests/CollectionTest.java @@ -17,7 +17,7 @@ public class CollectionTest extends TestCase { public void testCollections() throws Exception { List list = arrayList("foo", "bar"); - String text = makeString(list, ",", "(", ")"); + String text = makeString(list, ",", "(", ")", -1); System.out.println("Have text: " + text); assertEquals("(foo,bar)", text); @@ -29,6 +29,6 @@ public class CollectionTest extends TestCase { }); System.out.println("Filtered list is " + actual); - assertEquals("(bar)", makeString(actual, ",", "(", ")")); + assertEquals("(bar)", makeString(actual, ",", "(", ")", -1)); } } diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt index 2343caa6fcc..d8e2e51a4d1 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt @@ -5,6 +5,16 @@ import kotlin.util.* import java.util.* +/** + * Returns *true* if all elements match the given *predicate* + * + * @includeFunctionBody ../../test/CollectionTest.kt all + */ +public inline fun Array.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* * @@ -16,13 +26,22 @@ public inline fun Array.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 Array.all(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (!predicate(element)) return false - return true +public inline fun Array.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { + buffer.append(prefix) + 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 Array.groupBy(result: Map> = HashMap Array.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* */ public inline fun > Array.takeWhileTo(result: L, predicate: (T) -> Boolean) : L { for (element in this) if (predicate(element)) result.add(element) else break diff --git a/libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt b/libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt index a6d1338e08a..7acb05ebde7 100644 --- a/libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/JUnilIteratorsFromJLangIterables.kt @@ -3,6 +3,16 @@ package kotlin import java.util.* +/** + * Returns *true* if all elements match the given *predicate* + * + * @includeFunctionBody ../../test/CollectionTest.kt all + */ +public inline fun java.util.Iterator.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* * @@ -14,13 +24,22 @@ public inline fun java.util.Iterator.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 java.util.Iterator.all(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (!predicate(element)) return false - return true +public inline fun java.util.Iterator.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { + buffer.append(prefix) + 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 java.util.Iterator.groupBy(result: Map> = 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 java.util.Iterator.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* */ public inline fun > java.util.Iterator.takeWhileTo(result: L, predicate: (T) -> Boolean) : L { for (element in this) if (predicate(element)) result.add(element) else break diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt index 72224ca65eb..6ca1df36585 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt @@ -5,6 +5,16 @@ import kotlin.util.* import java.util.* +/** + * Returns *true* if all elements match the given *predicate* + * + * @includeFunctionBody ../../test/CollectionTest.kt all + */ +public inline fun Iterable.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* * @@ -16,13 +26,22 @@ public inline fun Iterable.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 Iterable.all(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (!predicate(element)) return false - return true +public inline fun Iterable.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit { + buffer.append(prefix) + 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 Iterable.groupBy(result: Map> = HashMap Iterable.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* */ public inline fun > Iterable.takeWhileTo(result: L, predicate: (T) -> Boolean) : L { for (element in this) if (predicate(element)) result.add(element) else break