Created
May 31, 2018 18:01
-
-
Save Tagakov/e7f35fb9b7da7d4a8ed7f081668c168d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StupidIterator(val source: Iterator<*>): Iterator<String> { | |
private var nextElement: Any? = null | |
init { | |
acquireNextElement() | |
} | |
private fun acquireNextElement() { | |
nextElement = if (source.hasNext()) source.next().let { if (it is Iterator<*>) StupidIterator(it) else it } else null | |
} | |
override fun hasNext(): Boolean { | |
if (nextElement != null) { | |
if (nextElement is Iterator<*>) { | |
if ((nextElement as Iterator<*>).hasNext()) { | |
return true | |
} | |
acquireNextElement() | |
return hasNext() | |
} | |
return true | |
} | |
return false | |
} | |
override fun next(): String { | |
if (nextElement is Iterator<*>) { | |
val iterator = nextElement as Iterator<*> | |
return iterator.next().also { if (!iterator.hasNext()) acquireNextElement() } as String | |
} | |
return nextElement.also { acquireNextElement() } as String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment