From 71b1aee908bcaabdc3798a41b09b3732f0f01fe0 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Thu, 22 Dec 2011 08:59:43 +0000 Subject: [PATCH] added a little test case showing how we can iterate through lines in a file; also started adding helper methods to the standard kotlin Iterator; though probably need to rename the to* methods iterators, maybe to collect* as Stepan suggested. Could maybe use some extension function syntax making it easy to add helper methods consistently to Array, Iterator, Iterable, java.util.Iterator, java.util.Iterable at the same time, we're very inconsistent currently :) --- stdlib/ktSrc/JavaIo.kt | 16 ++++---- stdlib/ktSrc/Standard.kt | 47 ++++++++++++++++++++++ testlib/test/IoTest.kt | 8 +++- testlib/test/test/collections/TestAll.java | 2 +- 4 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 stdlib/ktSrc/Standard.kt diff --git a/stdlib/ktSrc/JavaIo.kt b/stdlib/ktSrc/JavaIo.kt index 93abd9471e6..03efc943a1a 100644 --- a/stdlib/ktSrc/JavaIo.kt +++ b/stdlib/ktSrc/JavaIo.kt @@ -67,8 +67,7 @@ private val stdin : BufferedReader = BufferedReader(InputStreamReader(object : I inline fun readLine() : String? = stdin.readLine() /** Uses the given resource then closes it to ensure its closed again */ -/* -inline fun T.use(block: fun(T): R) : R { +inline fun T.use(block: (T)-> R) : R { var closed = false try { return block(this) @@ -86,7 +85,6 @@ inline fun T.use(block: fun(T): R) : R { } } } -*/ fun InputStream.iterator() : ByteIterator = object: ByteIterator() { @@ -120,19 +118,23 @@ inline fun Reader.buffered() = BufferedReader(this) inline fun Reader.buffered(bufferSize: Int) = BufferedReader(this, bufferSize) -/* inline fun BufferedReader.lineIterator() : Iterator = LineIterator(this) protected class LineIterator(val reader: BufferedReader) : Iterator { - var nextLine: String? = null + private var nextLine: String? = null + private var closed = false override val hasNext: Boolean get() { nextLine = reader.readLine() - return nextLine != null + val answer = nextLine != null + if (! answer && !closed) { + closed = true + reader.close() + } + return answer } override fun next(): String = nextLine.sure() } -*/ diff --git a/stdlib/ktSrc/Standard.kt b/stdlib/ktSrc/Standard.kt new file mode 100644 index 00000000000..45818cd3fb5 --- /dev/null +++ b/stdlib/ktSrc/Standard.kt @@ -0,0 +1,47 @@ +namespace std + +import java.util.Collection +import java.util.ArrayList +import java.util.LinkedList +import java.util.HashSet +import java.util.LinkedHashSet +import java.util.TreeSet + +/* + * Extension functions on the standard Kotlin types to behave like the java.lang.* and java.util.* collections + */ + +/* +Add iterated elements to given container +*/ +fun > Iterator.to(container: U) : U { + while(hasNext) + container.add(next()) + return container +} + +/* +Add iterated elements to java.util.ArrayList +*/ +inline fun Iterator.toArrayList() = to(ArrayList()) + +/* +Add iterated elements to java.util.LinkedList +*/ +inline fun Iterator.toLinkedList() = to(LinkedList()) + +/* +Add iterated elements to java.util.HashSet +*/ +inline fun Iterator.toHashSet() = to(HashSet()) + +/* +Add iterated elements to java.util.LinkedHashSet +*/ +inline fun Iterator.toLinkedHashSet() = to(LinkedHashSet()) + +/* +Add iterated elements to java.util.TreeSet +*/ +inline fun Iterator.toTreeSet() = to(TreeSet()) + diff --git a/testlib/test/IoTest.kt b/testlib/test/IoTest.kt index 63d4ba925f5..380d31a0b51 100644 --- a/testlib/test/IoTest.kt +++ b/testlib/test/IoTest.kt @@ -10,6 +10,11 @@ import java.util.* class IoTest() : TestSupport() { val file = File("test/HelloWorld.txt") + fun testLineIterator() { + val list = FileReader(file).buffered().lineIterator().toArrayList() + assertEquals(arrayList("Hello", "World"), list) + } + fun testUse() { val list = ArrayList() @@ -18,7 +23,7 @@ class IoTest() : TestSupport() { /** TODO compiler error? - reader.use{ + reader.use{ val line = it.readLine() if (line != null) { list.add(line) @@ -28,5 +33,4 @@ class IoTest() : TestSupport() { assertEquals(arrayList("Hello", "World"), list) */ } - } \ No newline at end of file diff --git a/testlib/test/test/collections/TestAll.java b/testlib/test/test/collections/TestAll.java index d939036d986..7b6a0e689a5 100644 --- a/testlib/test/test/collections/TestAll.java +++ b/testlib/test/test/collections/TestAll.java @@ -6,6 +6,6 @@ import junit.framework.TestSuite; */ public class TestAll { public static TestSuite suite() { - return new TestSuite(CollectionTest.class, SetTest.class, ListTest.class, IoTest.class, MapTest.class); + return new TestSuite(CollectionTest.class, IoTest.class, ListTest.class, MapTest.class, SetTest.class); } } \ No newline at end of file