Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
minor refactoring wrt #40
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 16, 2014
1 parent 6b77212 commit 5190aaf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,18 @@ private Feature(boolean defaultState) {
public CsvGenerator(IOContext ctxt, int jsonFeatures, int csvFeatures,
ObjectCodec codec, Writer out,
char columnSeparator, char quoteChar, char[] linefeed)
{
this(ctxt, jsonFeatures, csvFeatures, codec,
new CsvWriter(ctxt, out, columnSeparator, quoteChar, linefeed));
}

public CsvGenerator(IOContext ctxt, int jsonFeatures, int csvFeatures,
ObjectCodec codec, CsvWriter csvWriter)
{
super(jsonFeatures, codec);
_ioContext = ctxt;
_csvFeatures = csvFeatures;
_writer = new CsvWriter(ctxt, out, columnSeparator, quoteChar, linefeed);
_writer = csvWriter;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Low-level helper class that handles actual output of CSV, purely
* based on indexes given without worrying about reordering etc.
*/
public final class CsvWriter
public class CsvWriter
{
/* As an optimization we try coalescing short writes into
* buffer; but pass longer directly.
Expand Down Expand Up @@ -50,6 +50,8 @@ public final class CsvWriter

final protected int _cfgLineSeparatorLength;

protected int _cfgMaxQuoteCheckChars;

/**
* Lowest-valued character that is safe to output without using
* quotes around value
Expand Down Expand Up @@ -136,6 +138,8 @@ public CsvWriter(IOContext ctxt, Writer out,
_cfgLineSeparatorLength = linefeed.length;

_cfgMinSafeChar = _calcSafeChar();

_cfgMaxQuoteCheckChars = MAX_QUOTE_CHECK;
}

public CsvWriter(CsvWriter base, CsvSchema newSchema)
Expand All @@ -145,6 +149,7 @@ public CsvWriter(CsvWriter base, CsvSchema newSchema)
_bufferRecyclable = base._bufferRecyclable;
_outputEnd = base._outputEnd;
_out = base._out;
_cfgMaxQuoteCheckChars = base._cfgMaxQuoteCheckChars;

_cfgColumnSeparator = newSchema.getColumnSeparator();
_cfgQuoteCharacter = newSchema.getQuoteChar();
Expand Down Expand Up @@ -187,7 +192,7 @@ public int nextColumnIndex() {
/**********************************************************
*/

public void write(int columnIndex, String value) throws IOException
public final void write(int columnIndex, String value) throws IOException
{
// easy case: all in order
if (columnIndex == _nextColumnToWrite) {
Expand All @@ -198,13 +203,13 @@ public void write(int columnIndex, String value) throws IOException
_buffer(columnIndex, BufferedValue.buffered(value));
}

public void write(int columnIndex, char[] ch, int offset, int len) throws IOException
public final void write(int columnIndex, char[] ch, int offset, int len) throws IOException
{
// !!! TODO: optimize
write(columnIndex, new String(ch, offset, len));
}

public void write(int columnIndex, int value) throws IOException
public final void write(int columnIndex, int value) throws IOException
{
// easy case: all in order
if (columnIndex == _nextColumnToWrite) {
Expand All @@ -215,7 +220,7 @@ public void write(int columnIndex, int value) throws IOException
_buffer(columnIndex, BufferedValue.buffered(value));
}

public void write(int columnIndex, long value) throws IOException
public final void write(int columnIndex, long value) throws IOException
{
// easy case: all in order
if (columnIndex == _nextColumnToWrite) {
Expand All @@ -226,7 +231,7 @@ public void write(int columnIndex, long value) throws IOException
_buffer(columnIndex, BufferedValue.buffered(value));
}

public void write(int columnIndex, float value) throws IOException
public final void write(int columnIndex, float value) throws IOException
{
// easy case: all in order
if (columnIndex == _nextColumnToWrite) {
Expand All @@ -237,7 +242,7 @@ public void write(int columnIndex, float value) throws IOException
_buffer(columnIndex, BufferedValue.buffered(value));
}

public void write(int columnIndex, double value) throws IOException
public final void write(int columnIndex, double value) throws IOException
{
// easy case: all in order
if (columnIndex == _nextColumnToWrite) {
Expand All @@ -249,7 +254,7 @@ public void write(int columnIndex, double value) throws IOException
}


public void write(int columnIndex, boolean value) throws IOException
public final void write(int columnIndex, boolean value) throws IOException
{
// easy case: all in order
if (columnIndex == _nextColumnToWrite) {
Expand All @@ -260,7 +265,7 @@ public void write(int columnIndex, boolean value) throws IOException
_buffer(columnIndex, BufferedValue.buffered(value));
}

public void writeColumnName(String name) throws IOException
public final void writeColumnName(String name) throws IOException
{
appendValue(name);
++_nextColumnToWrite;
Expand Down Expand Up @@ -572,14 +577,14 @@ public void close(boolean autoClose) throws IOException
* Helper method that determines whether given String is likely
* to require quoting; check tries to optimize for speed.
*/
protected final boolean _mayNeedQuotes(String value, int length)
protected boolean _mayNeedQuotes(String value, int length)
{
// 21-Mar-2014, tatu: If quoting disabled, don't quote
if (_cfgQuoteCharacter < 0) {
return false;
}
// let's not bother checking long Strings, just quote already:
if (length > MAX_QUOTE_CHECK) {
if (length > _cfgMaxQuoteCheckChars) {
return true;
}
for (int i = 0; i < length; ++i) {
Expand All @@ -599,7 +604,7 @@ protected void _buffer(int index, BufferedValue v)
_buffered[index] = v;
}

protected final void _flushBuffer() throws IOException
protected void _flushBuffer() throws IOException
{
if (_outputTail > 0) {
_charsWritten += _outputTail;
Expand Down

0 comments on commit 5190aaf

Please sign in to comment.