All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.pro.felixo.protobuf.util.PeekableIterator.kt Maven / Gradle / Ivy

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) }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy