io.datalbry.jetbrains.space.client.PaginationIterator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetbrains-client Show documentation
Show all versions of jetbrains-client Show documentation
Client implementation for Jetbrains Space
package io.datalbry.jetbrains.space.client
import space.jetbrains.api.runtime.Batch
import space.jetbrains.api.runtime.BatchInfo
/**
* PaginationIterator allows to simply iterate over the batches returned by the JetBrains Space client with the default
* batch size of 100.
*
* @param fetchNextBatch: A function which takes a BatchInfo object and produces a new batch.
* @param transformer: A function which transforms the objects from the batch.
*/
class PaginationIterator(
private val fetchNextBatch: (BatchInfo) -> Batch,
private val transformer: (T) -> S
) : Iterator {
private var batchInfo = BatchInfo("0", 100)
private var currentBatch: Batch = fetchNextBatch(batchInfo)
private var currentBatchElements = currentBatch.data.map(transformer).iterator()
private fun loadNextBatch() {
batchInfo = BatchInfo(currentBatch.next, 100)
val currentBatchTmp = fetchNextBatch(batchInfo)
if (currentBatchTmp.next != "") {
currentBatch = currentBatchTmp
currentBatchElements = currentBatch.data.map(transformer).iterator()
}
}
override fun hasNext(): Boolean {
if (!currentBatchElements.hasNext()) {
loadNextBatch()
}
return currentBatchElements.hasNext()
}
override fun next(): S {
if (!currentBatchElements.hasNext()) {
loadNextBatch()
}
return currentBatchElements.next()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy