Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to resolve #117 - returning an iterator pointing to the reader head everytime the iterator() is called. #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/scala/com/github/tototoshi/csv/CSVReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import scala.io.Source

class CSVReader protected (private val lineReader: LineReader)(implicit format: CSVFormat) extends Closeable with CSVReaderCompat {

private[this] val parser = new CSVParser(format)
private val parser = new CSVParser(format)
private var maybeNextIterator: Option[Iterator[Seq[String]]] = None

def readNext(): Option[List[String]] = {

Expand Down Expand Up @@ -53,7 +54,13 @@ class CSVReader protected (private val lineReader: LineReader)(implicit format:

def foreach(f: Seq[String] => Unit): Unit = iterator.foreach(f)

def iterator: Iterator[Seq[String]] = new Iterator[Seq[String]] {
def iterator: Iterator[Seq[String]] = {
val duplicates = maybeNextIterator.getOrElse(_iterator).duplicate
maybeNextIterator = Some(duplicates._2)
duplicates._1
}

private def _iterator: Iterator[Seq[String]] = new Iterator[Seq[String]] {

private[this] var _next: Option[Seq[String]] = None

Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ class CSVReaderSpec extends AnyFunSpec with Matchers with Using {
lineCount should be(2)
}

describe("When the iterator method is called") {
it("should return the new iterator pointing to the CSV head everytime") {
using(CSVReader.open("src/test/resources/simple.csv")) { reader =>
Range(0, (new scala.util.Random).nextInt(9) + 2).foreach { iteratorX =>
reader.iterator.toList.size should be(2)
}
}
}
}

describe("When the file to be parsed is huge") {
it("should iterate all lines without any trouble") {
val tmpfile: File = File.createTempFile("csv", "test")
Expand Down