#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:01:24 +01:00
parent fc683e8141
commit 1d342978d0
17 changed files with 76 additions and 100 deletions
@@ -766,7 +766,7 @@ class KPackage(model: KModel, val descriptor: NamespaceDescriptor,
/** Returns a relative path like ../.. for each path in the name */
public val nameAsRelativePath: String
get() {
val answer = namePaths.map{ ".." }.join("/")
val answer = namePaths.map{ ".." }.makeString("/")
return if (answer.length == 0) "" else answer + "/"
}
@@ -922,7 +922,7 @@ class KFunction(val descriptor: CallableDescriptor, val owner: KClassOrPackage,
var annotations: List<KAnnotation> = arrayList<KAnnotation>(),
var sourceLine: Int = 2): KAnnotated(owner.model, descriptor), Comparable<KFunction> {
public val parameterTypeText: String = parameters.map{ it.aType.name }.join(", ")
public val parameterTypeText: String = parameters.map{ it.aType.name }.makeString(", ")
override fun compareTo(other: KFunction): Int {
var answer = name.compareTo(other.name)
@@ -946,7 +946,7 @@ class KFunction(val descriptor: CallableDescriptor, val owner: KClassOrPackage,
/** Returns a list of generic type parameter names kinds like "A, I" */
public val typeParametersText: String
get() = typeParameters.map{ it.name }.join(", ")
get() = typeParameters.map{ it.name }.makeString(", ")
}
class KProperty(val owner: KClassOrPackage, val descriptor: PropertyDescriptor, val name: String,
@@ -113,7 +113,7 @@ abstract class KDocTemplate() : TextTemplate() {
}
open fun typeArguments(arguments: List<KType>, val prefix: String = "&lt;", val postfix: String = "&gt;", val empty: String = ""): String {
val text = arguments.map<KType, String>() { link(it) }.join(", ")
val text = arguments.map<KType, String>() { link(it) }.makeString(", ")
return if (text.length() == 0) empty else "$prefix$text$postfix"
}
@@ -124,7 +124,7 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
println("""<PRE>""")
println("""<FONT SIZE="-1">""")
printAnnotations(function.annotations)
print("""</FONT>${function.modifiers.join(" ")} $funKeyword""")
print("""</FONT>${function.modifiers.makeString(" ")} $funKeyword""")
printTypeParameters(function, " ")
printReceiverType(function, " ", ".", " ")
+1 -1
View File
@@ -133,7 +133,7 @@
<artifactId>dartc</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<file>${kotlin-sdk}/lib/js/dartc.jar</file>
<file>${kotlin-sdk}/lib/js/${dart.name}.jar</file>
<createChecksum>true</createChecksum>
<generatePom>true</generatePom>
</configuration>
@@ -17,7 +17,7 @@ public class CollectionTest extends TestCase {
public void testCollections() throws Exception {
List<String> list = arrayList("foo", "bar");
String text = join(list, ",", "(", ")");
String text = makeString(list, ",", "(", ")");
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)", join(actual, ",", "(", ")"));
assertEquals("(bar)", makeString(actual, ",", "(", ")"));
}
}
+6 -4
View File
@@ -12,17 +12,19 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project-root>${project.basedir}/..</project-root>
<asm.version>3.3.1</asm.version>
<dart.name>dart-r3300</dart.name>
<guava.version>11.0.2</guava.version>
<junit-version>4.10</junit-version>
<kotlin-maven-plugin.version>0.2.3.8</kotlin-maven-plugin.version>
<project-root>${project.basedir}/..</project-root>
<kotlin-sdk>${project-root}/../dist/kotlinc</kotlin-sdk>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<pegdown.version>1.1.0</pegdown.version>
<surefire-version>2.5</surefire-version>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<asm.version>3.3.1</asm.version>
<guava.version>11.0.2</guava.version>
</properties>
@@ -61,19 +61,3 @@ public inline fun <T> Array<T>.take(n: Int): List<T> {
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
*/
public inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/CollectionTest.kt join
*/
public inline fun <T> Array<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (element in this) {
if (first) first = false else buffer.append(separator)
buffer.append(element)
}
buffer.append(postfix)
return buffer.toString().sure()
}
@@ -61,19 +61,3 @@ public inline fun <T> Iterable<T>.take(n: Int): List<T> {
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
*/
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/CollectionTest.kt join
*/
public inline fun <T> Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (element in this) {
if (first) first = false else buffer.append(separator)
buffer.append(element)
}
buffer.append(postfix)
return buffer.toString().sure()
}
-17
View File
@@ -142,20 +142,3 @@ private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val pred
done()
}
}
/**
* Creates a string from the first *n (= limit)* elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt joinConcatenatesTheFirstNElementsAboveAThreshold
*/
public inline fun <T> java.util.Iterator<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int? = null) : String {
val buffer = StringBuilder(prefix)
var first = true; var count = 0
for (element in this) {
if (first) first = false else buffer.append(separator)
if (limit == null || ++count <= limit) buffer.append(element) else break
}
if (limit != null && count > limit) buffer.append("...")
return buffer.append(postfix).toString().sure()
}
+35 -5
View File
@@ -2,6 +2,16 @@ package kotlin
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> java.lang.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*
*
@@ -13,13 +23,22 @@ public inline fun <T> java.lang.Iterable<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.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
public inline fun <T> java.lang.Iterable<T>.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)
}
/**
@@ -129,6 +148,17 @@ public inline fun <T, K> java.lang.Iterable<T>.groupBy(result: Map<K, List<T>> =
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.lang.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* */
public inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
for (element in this) if (predicate(element)) result.add(element) else break
@@ -58,19 +58,3 @@ public inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
*/
public inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* @includeFunctionBody ../../test/CollectionTest.kt join
*/
public inline fun <T> java.lang.Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
val buffer = StringBuilder(prefix)
var first = true
for (element in this) {
if (first) first = false else buffer.append(separator)
buffer.append(element)
}
buffer.append(postfix)
return buffer.toString().sure()
}
+1 -1
View File
@@ -89,7 +89,7 @@ get() {
return answer
}
set(value) {
this.classes = value.join(" ")
this.classes = value.makeString(" ")
}
+1 -1
View File
@@ -56,7 +56,7 @@ public fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit {
/** Converts the collection of nodes to an XML String */
public fun nodesToXmlString(nodes: Iterable<Node>, xmlDeclaration: Boolean = false): String {
// TODO this should work...
// return this.map<Node,String>{it.toXmlString()}.join("")
// return this.map<Node,String>{it.toXmlString()}.makeString("")
val builder = StringBuilder()
for (n in nodes) {
builder.append(n.toXmlString(xmlDeclaration))
@@ -122,7 +122,7 @@ public inline fun <T, K> T?.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>
/** Creates a String from the nullable or item with the given prefix and postfix if supplied */
public inline fun <T> T?.join(separator: String, prefix: String = "", postfix: String = ""): String {
public inline fun <T> T?.makeString(separator: String = ", ", prefix: String = "", postfix: String = ""): String {
val buffer = StringBuilder(prefix)
var first = true
if (this != null) {
+19 -10
View File
@@ -8,6 +8,16 @@ import org.junit.Test
class CollectionTest {
Test fun all() {
val data = arrayList("foo", "bar")
assertTrue {
data.all{it.length == 3}
}
assertNot {
data.all{s -> s.startsWith("b")}
}
}
Test fun any() {
val data = arrayList("foo", "bar")
assertTrue {
@@ -18,14 +28,12 @@ class CollectionTest {
}
}
Test fun all() {
Test fun appendString() {
val data = arrayList("foo", "bar")
assertTrue {
data.all{it.length == 3}
}
assertNot {
data.all{s -> s.startsWith("b")}
}
val buffer = StringBuilder()
val text = data.appendString(buffer, "-", "{", "}")
assertEquals("{foo-bar}", buffer.toString())
}
Test fun count() {
@@ -157,7 +165,7 @@ class CollectionTest {
val characters = data.flatMap<String,Character>{ it.toCharList() }
println("Got list of characters ${characters}")
assertEquals(7, characters.size())
val text = characters.join("")
val text = characters.makeString("")
assertEquals("foobarx", text)
}
@@ -223,9 +231,10 @@ class CollectionTest {
}
Test fun join() {
Test fun makeString() {
val data = arrayList("foo", "bar")
val text = data.join("-", "<", ">")
val text = data.makeString("-", "<", ">")
assertEquals("<foo-bar>", text)
}
@@ -48,12 +48,12 @@ class IteratorsTest {
}
Test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.join(separator = ", ", limit = 5))
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
}
Test fun toStringJoinsNoMoreThanTheFirstTenElements() {
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().join(limit = 10))
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.join(limit = 10))
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.join())
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().makeString(limit = 10))
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.makeString(limit = 10))
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.makeString())
}
}
@@ -17,7 +17,7 @@ fun customerTemplate(customer: Customer) = """
<body>
<h1>Hello ${customer.name}</h1>
<ul>
${customer.products.map<Product,String>{ productSnippet(it) }.join("\n")}
${customer.products.map<Product,String>{ productSnippet(it) }.makeString("\n")}
</ul>
<p>lets do some kool stuff</p>
</body>