Permalink
Browse files

Reverted to original fluent design, removed encoding (works with Read…

…er / Writer)
  • Loading branch information...
1 parent ccf556d commit 7cd715a966a0ed3ee0eca2f965da585686323484 @melezov committed Feb 18, 2012
@@ -1,286 +1,47 @@
package hr.element.doit.csv
-import java.io._
+import java.io.{ InputStream, Reader, OutputStream, Writer }
import java.nio.charset.Charset
-class CSVFactory(
- val quotes: String,
+object CSVFactory extends CSVFactory(";", "\"", "\n")
+
+class CSVFactory private (
val delimiter: String,
- val newLine: String,
- val encoding: Charset){
+ val quotes: String,
+ val newLine: String) {
- def getReader(iS: InputStream) =
- new CSVReader(this, iS)
+ // /===========================================================================
- def getReaderWithHeaders(iS: InputStream) =
- new CSVReaderWithHeaders(this, iS)
+ def getReader(reader: Reader) =
+ new CSVReader(this, reader)
- def getWriter(out: OutputStream) =
- new CSVWriter(this, out)
-}
+ def getReaderWithHeaders(reader: Reader) =
+ new CSVReaderWithHeaders(this, reader)
-trait DelimiterSetter { _: CSVFactory =>
- def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
+ def getWriter(writer: Writer) =
+ new CSVWriter(this, writer)
- def setDelimiter(delimiter: CharSequence): CSVFactory =
- setDelimiter(delimiter.toString)
+ // ===========================================================================
- def setDelimiter(delimiter: Array[Char]): CSVFactory =
- setDelimiter(new String(delimiter))
+ def setDelimiter(delimiter: CharSequence): CSVFactory =
+ new CSVFactory(quotes, delimiter.toString, newLine)
def setDelimiter(delimiter: Char): CSVFactory =
setDelimiter(delimiter.toString)
-}
-trait QuotesSetter { _: CSVFactory =>
- def setQuotes(quotes: String): CSVFactory =
- new CSVFactory(quotes, delimiter, newLine, encoding)
+ // ---------------------------------------------------------------------------
def setQuotes(quotes: CharSequence): CSVFactory =
- setQuotes(quotes)
-
- def setQuotes(quotes: Array[Char]): CSVFactory =
- setQuotes(new String(quotes))
+ new CSVFactory(quotes.toString, delimiter, newLine)
def setQuotes(quotes: Char): CSVFactory =
setQuotes(quotes.toString)
-}
-
-trait NewLineSetter { _: CSVFactory =>
- def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
+ // ---------------------------------------------------------------------------
def setNewLine(newLine: CharSequence): CSVFactory =
- setNewLine(newLine.toString)
-
- def setNewLine(newLine: Array[Char]): CSVFactory =
- setNewLine(new String(newLine))
+ new CSVFactory(quotes, delimiter, newLine.toString)
def setNewLine(newLine: Char): CSVFactory =
setNewLine(newLine.toString)
}
-
-trait EncodingSetter { _: CSVFactory =>
- def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
-
- def setEncoding(encoding: String): CSVFactory =
- setEncoding(Charset.forName(encoding))
-}
-
-object CSVFactory
- extends CSVFactory("\"", ";", "\n", Charset.forName("UTF-8"))
- with QuotesSetter
- with DelimiterSetter
- with NewLineSetter
- with EncodingSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- with NewLineSetter
- with EncodingSetter {
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- with EncodingSetter {
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with EncodingSetter
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- }
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- with EncodingSetter {
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with EncodingSetter
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- }
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- with NewLineSetter {
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- }
- }
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with NewLineSetter
- with EncodingSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- with EncodingSetter {
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with EncodingSetter
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- }
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with EncodingSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with EncodingSetter
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- }
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with NewLineSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- }
- }
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with DelimiterSetter
- with EncodingSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- with EncodingSetter {
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with EncodingSetter
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- }
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with EncodingSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with EncodingSetter
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- }
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with DelimiterSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- }
- }
- override def setEncoding(encoding: Charset) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with DelimiterSetter
- with NewLineSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- with NewLineSetter {
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- }
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with NewLineSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with NewLineSetter
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- }
- override def setNewLine(newLine: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- with DelimiterSetter {
- override def setQuotes(quotes: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with DelimiterSetter
- override def setDelimiter(delimiter: String) =
- new CSVFactory(quotes, delimiter, newLine, encoding)
- with QuotesSetter
- }
- }
-}
-
-/*
-object CSVFactoryIAmSaneEnoughNotToWriteThatByHandThankYouBuilder extends App {
- case class Setter(val name: String, val clazz: Class[_]) {
- val clazzString = clazz.getSimpleName
- val argument = name.head.toLower + name.tail
- }
-
- val setters = List(
- Setter("Quotes", classOf[String])
- , Setter("Delimiter", classOf[String])
- , Setter("NewLine", classOf[String])
- , Setter("Encoding", classOf[Charset])
- )
-
- val padding = " "
- val newLine = "\n"
-
- val constructorLine =
- "new CSVFactory(%s)".format(setters.map(_.argument).mkString(", "))
-
- def buildSetter(s: Setter, others: List[Setter], padCount: Int): String = {
- val defLine =
- "%soverride def set%s(%s: %s) =%s".format(padding * (padCount + 1), s.name, s.argument, s.clazzString, newLine)
-
- val cLine =
- "%s%s%s".format(padding * (padCount + 2), constructorLine, newLine)
-
- val mixins =
- others.map(o =>
- "%swith %sSetter".format(padding * (padCount + 4), o.name)
- ).mkString(newLine)
-
- val body =
- others match {
- case _ :: Nil =>
- newLine
-
- case _ =>
- " {%s%s%s}%s".format(newLine, buildSetters(others, padCount + 2), padding * (padCount + 2), newLine)
- }
-
- defLine + cLine + mixins + body
- }
-
- def buildSetters(setters: List[Setter], padCount: Int) =
- setters.map(s => buildSetter(s, setters.filterNot(s ==), padCount)).mkString("")
-
- println(buildSetters(setters, 0))
-}
-*/
@@ -5,13 +5,12 @@ import scala.collection.Traversable
import java.io._
-abstract class CSVReaderLike[+T <: LineReader](config: CSVFactory, iS: InputStream) extends Traversable[T]{
- val reader: Reader = new InputStreamReader(iS, config.encoding)
+abstract class CSVReaderLike[+T <: LineReader](config: CSVFactory, reader: Reader) extends Traversable[T]{
def readLn() = new LineReader(config, reader)
}
-class CSVReader( config: CSVFactory, iS: InputStream)
- extends CSVReaderLike[LineReader](config, iS) {
+class CSVReader(config: CSVFactory, reader: Reader)
+ extends CSVReaderLike[LineReader](config, reader) {
def foreach[U](f: LineReader => U) = {
@tailrec
@@ -27,8 +26,8 @@ class CSVReader( config: CSVFactory, iS: InputStream)
}
}
-class CSVReaderWithHeaders(config: CSVFactory, iS: InputStream)
- extends CSVReaderLike[LineReaderWithHeader](config, iS) {
+class CSVReaderWithHeaders(config: CSVFactory, reader: Reader)
+ extends CSVReaderLike[LineReaderWithHeader](config, reader) {
val header =
new LineReader(config, reader).words
@@ -4,21 +4,19 @@ import scala.annotation.tailrec
import java.io._
-class CSVWriter(config: CSVFactory, oS: OutputStream) {
- val w: Writer = new OutputStreamWriter(oS, config.encoding)
-
+class CSVWriter(config: CSVFactory, writer: Writer) {
val quoteLen = config.quotes.length
val escapes =
Seq(config.delimiter, config.quotes, config.newLine)
def quoteIfNecessary(l: String) {
if (escapes.exists(l.contains)) {
- w.write(config.quotes)
+ writer.write(config.quotes)
quote(l)
- w.write(config.quotes)
+ writer.write(config.quotes)
} else {
- w.write(l)
+ writer.write(l)
}
}
@@ -28,30 +26,28 @@ class CSVWriter(config: CSVFactory, oS: OutputStream) {
l.indexOf(config.quotes, i + quoteLen) match {
case oldValTail if oldValTail == -1 =>
- w.write(l substring oldValHead)
+ writer.write(l substring oldValHead)
case oldValTai =>
- w.write(l substring(oldValHead, oldValTai))
- w.write(config.quotes)
+ writer.write(l substring(oldValHead, oldValTai))
+ writer.write(config.quotes)
quote(l, oldValTai)
}
}
def write(line: Array[String]) {
-
if ((line == null) || line.isEmpty) {
this
}
else {
for (i <- line.indices) {
if (i != 0){
- w.write(config.delimiter)
+ writer.write(config.delimiter)
}
quoteIfNecessary(line(i))
}
- w.write(config.newLine)
- w.flush()
+ writer.write(config.newLine)
+ writer.flush()
this
}
}
- def close() { w.close() }
}
Oops, something went wrong.

0 comments on commit 7cd715a

Please sign in to comment.