WIP and improved version of https://stackoverflow.com/questions/67381862/consume-paginated-not-really-api-in-scala-with-unknown-page-count-and-unknow

Home   »   WIP and improved version of https://stackoverflow.com/questions/67381862/consume-paginated-not-really-api-in-scala-with-unknown-page-count-and-unknow

package io.doerfler

/**
  * As an alternative consider flatMapConcat and mapConcat of Akka Streams.
  */
abstract class IteratorThatKeepsOnGiving[T] extends Iterator[T] {
  private var currentIter: Iterator[T] = Iterator.empty[T]
  private var currentElement: Option[T] = None

  def nextIterator(lastElement: Option[T]): Iterator[T]

  def fetchNewBatch(lastElement: Option[T]) = {
    currentIter = nextIterator(lastElement)
  }

  def hasNext: Boolean = currentIter.hasNext || {
    fetchNewBatch(currentElement)
    currentIter.hasNext
  }

  def next(): T = {
    currentIter.nextOption() match {
      case v @ Some(value) =>
        currentElement = v
        value
      case None =>
        fetchNewBatch(currentElement)
        next()
    }
  }
}

object IteratorThatKeepsOnGiving {
  def apply[T](f: Option[T] => Iterator[T]) = new IteratorThatKeepsOnGiving[T] {
    def nextIterator(lastElement: Option[T]): Iterator[T] = f(lastElement)
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *