commonMain.pro.felixo.protobuf.util.PeekableIterator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protobuf-kotlin-protoscope Show documentation
Show all versions of protobuf-kotlin-protoscope Show documentation
Protocol Buffers 3 support for Kotlin Multiplatform
The newest version!
package pro.felixo.protobuf.util
class PeekableIterator(
private val base: Iterator
) : Iterator {
private var peeked = ArrayDeque()
override fun hasNext(): Boolean = peeked.isNotEmpty() || base.hasNext()
override fun next(): T = peeked.removeFirstOrNull() ?: base.next()
/**
* Each call to this method peeks ahead one additional time without affecting the result of subsequent [next] calls.
*/
fun peek(): T? = if (!base.hasNext()) null else base.next().also { peeked.add(it) }
}