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

com.github.mmauro94.mkvtoolnix_wrapper.utils.CachedSequence.kt Maven / Gradle / Ivy

Go to download

An easy to use light kotlin-jvm wrapper for most common mkvmerge and mkvpropedit CLI commands

The newest version!
package com.github.mmauro94.mkvtoolnix_wrapper.utils

internal class CachedSequence(sequence: Sequence) : Sequence {

    private val list = ArrayList()
    private val iterator = sequence.iterator()

    override fun iterator() = object : Iterator {
        var i = 0
        override fun hasNext(): Boolean {
            synchronized(iterator) {
                return if (i < list.size) {
                    true
                } else {
                    iterator.hasNext()
                }
            }
        }

        override fun next(): T {
            synchronized(iterator) {
                return if (i < list.size) {
                    list[i]
                } else {
                    iterator.next().also {
                        list.add(it)
                    }
                }.also { i++ }
            }
        }
    }
}

internal fun  Sequence.asCachedSequence() = CachedSequence(this)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy