switch to using an enum
This commit is contained in:
@@ -2,25 +2,26 @@ package kotlin.support
|
|||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
// TODO should we use enums?
|
enum class State {
|
||||||
private val Ready = 0
|
Ready
|
||||||
private val NotReady = 1
|
NotReady
|
||||||
private val Done = 2
|
Done
|
||||||
private val Failed = 3
|
Failed
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base class to simplify implementing iterators so that implementations only have to implement [[#computeNext()]]
|
* 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.
|
* to implement the iterator, calling [[done()]] when the iteration is complete.
|
||||||
*/
|
*/
|
||||||
abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
||||||
var state = NotReady
|
var state: State = State.NotReady
|
||||||
var next: T? = null
|
var next: T? = null
|
||||||
|
|
||||||
override fun hasNext(): Boolean {
|
override fun hasNext(): Boolean {
|
||||||
require(state != Failed)
|
require(state != State.Failed)
|
||||||
return when (state) {
|
return when (state) {
|
||||||
Done -> false
|
State.Done -> false
|
||||||
Ready -> true
|
State.Ready -> true
|
||||||
else -> tryToComputeNext()
|
else -> tryToComputeNext()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +30,7 @@ abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
|||||||
if (!hasNext()) {
|
if (!hasNext()) {
|
||||||
throw NoSuchElementException();
|
throw NoSuchElementException();
|
||||||
} else {
|
} else {
|
||||||
state = NotReady
|
state = State.NotReady
|
||||||
return next.sure()
|
return next.sure()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,10 +50,10 @@ abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun tryToComputeNext(): Boolean {
|
private fun tryToComputeNext(): Boolean {
|
||||||
state = Failed
|
state = State.Failed
|
||||||
next = computeNext();
|
next = computeNext();
|
||||||
return if (state != Done) {
|
return if (state != State.Done) {
|
||||||
state = Ready
|
state = State.Ready
|
||||||
true
|
true
|
||||||
} else false
|
} else false
|
||||||
}
|
}
|
||||||
@@ -67,6 +68,6 @@ abstract class AbstractIterator<T>: java.util.Iterator<T> {
|
|||||||
* Sets the state to done so that the iteration terminates
|
* Sets the state to done so that the iteration terminates
|
||||||
*/
|
*/
|
||||||
protected fun done() {
|
protected fun done() {
|
||||||
state = Done
|
state = State.Done
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user