From 43346a256d5d302522bd52a53c02b10f8ae2ef20 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 24 Mar 2017 12:26:25 -0700 Subject: [PATCH] Better sequence. --- .../com/beust/kobalt/VerifyKobaltZipTest.kt | 31 ++----------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/src/test/kotlin/com/beust/kobalt/VerifyKobaltZipTest.kt b/src/test/kotlin/com/beust/kobalt/VerifyKobaltZipTest.kt index 2a604339..ba3a61c4 100644 --- a/src/test/kotlin/com/beust/kobalt/VerifyKobaltZipTest.kt +++ b/src/test/kotlin/com/beust/kobalt/VerifyKobaltZipTest.kt @@ -117,32 +117,7 @@ class VerifyKobaltZipTest : KobaltTest() { } } - private fun toSequence(ins: InputStream) = Sequence { JarInputStreamIterator(JarInputStream(ins)) } -} - -/** - * I don't want to hold the whole content of the jar file in memory to run tests on its content, - * so this iterator allows me to create a sequence out of it, so each entry in the jar file can - * be verified lazily - */ -class JarInputStreamIterator(val ins: JarInputStream) : Iterator { - var next: JarEntry? = null - - override fun next(): JarEntry { - if (next != null) { - next?.let { - val result = it - next = null - return result - } - } else { - return ins.nextJarEntry - } - throw IllegalArgumentException("Should never happen") - } - - override fun hasNext(): Boolean { - if (next == null) next = ins.nextJarEntry - return next != null - } + fun JarInputStream.asSequence() = generateSequence { nextJarEntry } + + private fun toSequence(ins: InputStream): Sequence = JarInputStream(ins).asSequence() }