From dadd288c40bc4275edd6f0ed4effcc80579856ae Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 23 Mar 2012 15:31:12 +0000 Subject: [PATCH] switch to using an enum --- .../stdlib/src/support/AbstractIterator.kt | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/libraries/stdlib/src/support/AbstractIterator.kt b/libraries/stdlib/src/support/AbstractIterator.kt index 34c88549b35..440301390d1 100644 --- a/libraries/stdlib/src/support/AbstractIterator.kt +++ b/libraries/stdlib/src/support/AbstractIterator.kt @@ -2,25 +2,26 @@ package kotlin.support import java.util.* -// TODO should we use enums? -private val Ready = 0 -private val NotReady = 1 -private val Done = 2 -private val Failed = 3 +enum class State { + Ready + NotReady + Done + Failed +} /** * A base class to simplify implementing iterators so that implementations only have to implement [[#computeNext()]] * to implement the iterator, calling [[done()]] when the iteration is complete. */ abstract class AbstractIterator: java.util.Iterator { - var state = NotReady + var state: State = State.NotReady var next: T? = null override fun hasNext(): Boolean { - require(state != Failed) + require(state != State.Failed) return when (state) { - Done -> false - Ready -> true + State.Done -> false + State.Ready -> true else -> tryToComputeNext() } } @@ -29,7 +30,7 @@ abstract class AbstractIterator: java.util.Iterator { if (!hasNext()) { throw NoSuchElementException(); } else { - state = NotReady + state = State.NotReady return next.sure() } } @@ -49,10 +50,10 @@ abstract class AbstractIterator: java.util.Iterator { } private fun tryToComputeNext(): Boolean { - state = Failed + state = State.Failed next = computeNext(); - return if (state != Done) { - state = Ready + return if (state != State.Done) { + state = State.Ready true } else false } @@ -67,6 +68,6 @@ abstract class AbstractIterator: java.util.Iterator { * Sets the state to done so that the iteration terminates */ protected fun done() { - state = Done + state = State.Done } } \ No newline at end of file