From a7cd3af5f225eeb8ee8beebc6b03442de2d3064d Mon Sep 17 00:00:00 2001 From: sh0hei Date: Sat, 7 Oct 2017 11:59:27 +0900 Subject: [PATCH] Use Seq[String] for interface instead of List[String] --- .../com/github/tototoshi/csv/CSVParser.scala | 8 +++--- .../com/github/tototoshi/csv/CSVReader.scala | 12 ++++----- .../github/tototoshi/csv/CSVReaderSpec.scala | 26 +++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/scala/com/github/tototoshi/csv/CSVParser.scala b/src/main/scala/com/github/tototoshi/csv/CSVParser.scala index d214c3c..26f65ad 100644 --- a/src/main/scala/com/github/tototoshi/csv/CSVParser.scala +++ b/src/main/scala/com/github/tototoshi/csv/CSVParser.scala @@ -31,13 +31,13 @@ object CSVParser { /** * {{{ * scala> com.github.tototoshi.csv.CSVParser.parse("a,b,c", '\\', ',', '"') - * res0: Option[List[String]] = Some(List(a, b, c)) + * res0: Option[Seq[String]] = Some(List(a, b, c)) * * scala> com.github.tototoshi.csv.CSVParser.parse("\"a\",\"b\",\"c\"", '\\', ',', '"') - * res1: Option[List[String]] = Some(List(a, b, c)) + * res1: Option[Seq[String]] = Some(List(a, b, c)) * }}} */ - def parse(input: String, escapeChar: Char, delimiter: Char, quoteChar: Char): Option[List[String]] = { + def parse(input: String, escapeChar: Char, delimiter: Char, quoteChar: Char): Option[Seq[String]] = { val buf: Array[Char] = input.toCharArray var fields: Vector[String] = Vector() var field = new StringBuilder @@ -299,7 +299,7 @@ object CSVParser { class CSVParser(format: CSVFormat) extends Serializable { - def parseLine(input: String): Option[List[String]] = { + def parseLine(input: String): Option[Seq[String]] = { val parsedResult = CSVParser.parse(input, format.escapeChar, format.delimiter, format.quoteChar) if (parsedResult == Some(List("")) && format.treatEmptyLineAsNil) Some(Nil) else parsedResult diff --git a/src/main/scala/com/github/tototoshi/csv/CSVReader.scala b/src/main/scala/com/github/tototoshi/csv/CSVReader.scala index 8ccf34c..90d7186 100644 --- a/src/main/scala/com/github/tototoshi/csv/CSVReader.scala +++ b/src/main/scala/com/github/tototoshi/csv/CSVReader.scala @@ -25,10 +25,10 @@ class CSVReader protected (private val lineReader: LineReader)(implicit format: private val parser = new CSVParser(format) - def readNext(): Option[List[String]] = { + def readNext(): Option[Seq[String]] = { @scala.annotation.tailrec - def parseNext(lineReader: LineReader, leftOver: Option[String] = None): Option[List[String]] = { + def parseNext(lineReader: LineReader, leftOver: Option[String] = None): Option[Seq[String]] = { val nextLine = lineReader.readLineWithTerminator() if (nextLine == null) { @@ -86,18 +86,18 @@ class CSVReader protected (private val lineReader: LineReader)(implicit format: def toStreamWithHeaders: Stream[Map[String, String]] = iteratorWithHeaders.toStream - def toStream: Stream[List[String]] = + def toStream: Stream[Seq[String]] = Stream.continually(readNext()).takeWhile(_.isDefined).map(_.get) - def all(): List[List[String]] = { + def all(): Seq[Seq[String]] = { toStream.toList } - def allWithHeaders(): List[Map[String, String]] = { + def allWithHeaders(): Seq[Map[String, String]] = { allWithOrderedHeaders._2 } - def allWithOrderedHeaders(): (List[String], List[Map[String, String]]) = { + def allWithOrderedHeaders(): (Seq[String], Seq[Map[String, String]]) = { val headers = readNext() val data = headers.map(headers => { val lines = all() diff --git a/src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala b/src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala index 3d34ac5..46bbb69 100644 --- a/src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala +++ b/src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala @@ -13,7 +13,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { describe("CSVReader") { it("should be constructed with java.io.File") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open(new File("src/test/resources/simple.csv"))) { reader => reader foreach { fields => res = res ++ fields @@ -23,7 +23,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("should be constructed with filename") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open("src/test/resources/simple.csv")) { reader => reader foreach { fields => res = res ++ fields @@ -83,7 +83,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("read simple CSV from file") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open(new FileReader("src/test/resources/simple.csv"))) { reader => reader foreach { fields => res = res ++ fields @@ -94,7 +94,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { it("read simple CSV string") { val csvString = "a,b,c\nd,e,f\n" - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open(new StringReader(csvString))) { reader => reader foreach { fields => res = res ++ fields @@ -104,7 +104,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("issue #22") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open("src/test/resources/issue22.csv")) { reader => reader foreach { fields => res = res ++ fields @@ -113,7 +113,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("issue #32") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open("src/test/resources/issue32.csv")(new DefaultCSVFormat { override val escapeChar: Char = '\\' })) { reader => @@ -124,7 +124,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("should read csv file whose escape char is backslash") { - var res: List[String] = Nil + var res: Seq[String] = Nil implicit val format = new DefaultCSVFormat { override val escapeChar: Char = '\\' } @@ -137,7 +137,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("read simple CSV file with empty quoted fields") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open("src/test/resources/issue30.csv")) { reader => reader foreach { fields => res = res ++ fields @@ -147,7 +147,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("should read a file starting with BOM") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open("src/test/resources/bom.csv")) { reader => reader foreach { fields => res = res ++ fields @@ -165,7 +165,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("read CSV file including escaped fields") { - var res: List[String] = Nil + var res: Seq[String] = Nil using(CSVReader.open(new FileReader("src/test/resources/escape.csv"))) { reader => reader foreach { fields => res = res ++ fields @@ -175,7 +175,7 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { } it("should correctly parse fields with line breaks enclosed in double quotes") { - var res: List[Seq[String]] = Nil + var res: Seq[Seq[String]] = Nil using(CSVReader.open(new FileReader("src/test/resources/line-breaks.csv"))) { reader => reader foreach { fields => res = res :+ fields @@ -187,10 +187,10 @@ class CSVReaderSpec extends FunSpec with Matchers with Using { it("read TSV from file") { implicit val format = new TSVFormat {} - var res: List[Seq[String]] = Nil + var res: Seq[Seq[String]] = Nil using(CSVReader.open(new FileReader("src/test/resources/simple.tsv"))(format)) { reader => reader.foreach { fields => - res = res ::: fields :: Nil + res = res :+ fields } } res(0) should be(List("a", "b", "c"))