com.github.mmauro94.mkvtoolnix_wrapper.utils.CachedSequence.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mkvtoolnix-wrapper Show documentation
Show all versions of mkvtoolnix-wrapper Show documentation
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)