Skip to content

Commit

Permalink
Adjusting method signature of AbstractWriter to properly handle writi…
Browse files Browse the repository at this point in the history
…ng rows based on collections of objects of any type.
  • Loading branch information
jbax committed Feb 27, 2018
1 parent e64ed68 commit dc2fe57
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/main/java/com/univocity/parsers/common/AbstractWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public final void writeHeaders() {
*
* @param headers the headers to write to the output.
*/
public final void writeHeaders(Collection<String> headers) {
public final void writeHeaders(Collection<?> headers) {
if (headers != null && headers.size() > 0) {
writeHeaders(headers.toArray(new String[headers.size()]));
} else {
Expand All @@ -408,7 +408,7 @@ public final void writeHeaders(String... headers) {
submitRow(headers);

this.headers = headers;
writeRow();
internalWriteRow();
writingHeaders = false;
} else {
throw throwExceptionAndClose("No headers defined.", headers, null);
Expand Down Expand Up @@ -480,6 +480,7 @@ public final void processRecord(Object... record) {
processRecord((Object) record);
}


/**
* Processes the data given for an individual record with the {@link RowWriterProcessor} provided by {@link CommonWriterSettings#getRowWriterProcessor()}, then writes it.
* <p> The output will remain open for further writing.
Expand Down Expand Up @@ -535,7 +536,7 @@ private String[] getRowProcessorHeaders() {
* @param <C> Collection of objects containing values of a row
* @param allRows the rows to be written to the output
*/
public final <C extends Collection<Object>> void writeRowsAndClose(Iterable<C> allRows) {
public final <C extends Collection<?>> void writeRowsAndClose(Iterable<C> allRows) {
try {
writeRows(allRows);
} finally {
Expand Down Expand Up @@ -606,8 +607,8 @@ public final void writeRows(Object[][] rows) {
* @param <C> Collection of objects containing values of a row
* @param rows the rows to be written to the output
*/
public final <C extends Collection<Object>> void writeRows(Iterable<C> rows) {
for (Collection<Object> row : rows) {
public final <C extends Collection<?>> void writeRows(Iterable<C> rows) {
for (Collection<?> row : rows) {
writeRow(row);
}
}
Expand All @@ -633,8 +634,8 @@ public final void writeStringRows(Collection<String[]> rows) {
* @param <C> Collection of objects containing values of a row
* @param rows the rows to be written to the output
*/
public final <C extends Collection<String>> void writeStringRows(Iterable<C> rows) {
for (Collection<String> row : rows) {
public final <C extends Collection<?>> void writeStringRows(Iterable<C> rows) {
for (Collection<?> row : rows) {
writeRow(row.toArray());
}
}
Expand All @@ -659,7 +660,7 @@ public final void writeRows(Collection<Object[]> rows) {
*
* @param row the information of a single record to be written to the output
*/
public final void writeRow(Collection<Object> row) {
public final void writeRow(Collection<?> row) {
if (row == null) {
return;
}
Expand All @@ -680,6 +681,7 @@ public final void writeRow(String[] row) {
writeRow((Object[]) row);
}


/**
* Writes the data given for an individual record.
* <p> The output will remain open for further writing.
Expand Down Expand Up @@ -707,7 +709,7 @@ public final void writeRow(Object... row) {
row = adjustRowLength(row);
submitRow(row);

writeRow();
internalWriteRow();
} catch (Throwable ex) {
throw throwExceptionAndClose("Error writing row.", row, ex);
}
Expand Down Expand Up @@ -813,7 +815,7 @@ private <T> void fillOutputRow(T[] row) {
* <p> The newline character sequence will conform to what is specified in {@link Format#getLineSeparator()}
* The contents of {@link AbstractWriter#rowAppender} depend on the concrete implementation of {@link AbstractWriter#processRow(Object[])}
*/
private void writeRow() {
private void internalWriteRow() {
try {
if (skipEmptyLines && rowAppender.length() == 0) {
return;
Expand Down Expand Up @@ -1004,7 +1006,7 @@ public final void addStringValues(Collection<String> values) {
*
* @param values the values to be written
*/
public final void addValues(Collection<Object> values) {
public final void addValues(Collection<?> values) {
if (values != null) {
try {
for (Object o : values) {
Expand Down Expand Up @@ -1143,7 +1145,7 @@ public final String writeHeadersToString() {
*
* @return a formatted {@code String} containing the given headers
*/
public final String writeHeadersToString(Collection<String> headers) {
public final String writeHeadersToString(Collection<?> headers) {
if (headers != null && headers.size() > 0) {
return writeHeadersToString(headers.toArray(new String[headers.size()]));
} else {
Expand All @@ -1165,7 +1167,7 @@ public final String writeHeadersToString(String... headers) {
submitRow(headers);
writingHeaders = false;
this.headers = headers;
return writeRowToString();
return internalWriteRowToString();
} else {
throw throwExceptionAndClose("No headers defined.");
}
Expand Down Expand Up @@ -1281,10 +1283,10 @@ public final List<String> writeRowsToString(Object[][] rows) {
*
* @return a {@code List} containing the given rows as formatted {@code String}s
*/
public final <C extends Collection<Object>> List<String> writeRowsToString(Iterable<C> rows) {
public final <C extends Collection<?>> List<String> writeRowsToString(Iterable<C> rows) {
try {
List<String> out = new ArrayList<String>(1000);
for (Collection<Object> row : rows) {
for (Collection<?> row : rows) {
out.add(writeRowToString(row));
}
return out;
Expand All @@ -1302,10 +1304,10 @@ public final <C extends Collection<Object>> List<String> writeRowsToString(Itera
*
* @return a {@code List} containing the given rows as formatted {@code String}s
*/
public final <C extends Collection<String>> List<String> writeStringRowsToString(Iterable<C> rows) {
public final <C extends Collection<?>> List<String> writeStringRowsToString(Iterable<C> rows) {
try {
List<String> out = new ArrayList<String>(1000);
for (Collection<String> row : rows) {
for (Collection<?> row : rows) {
String string = writeRowToString(row);
if (string != null) {
out.add(string);
Expand Down Expand Up @@ -1365,7 +1367,7 @@ public final List<String> writeStringRowsToString(Collection<String[]> rows) {
*
* @return a formatted {@code String} containing the information of the given record
*/
public final String writeRowToString(Collection<Object> row) {
public final String writeRowToString(Collection<?> row) {
try {
if (row == null) {
return null;
Expand Down Expand Up @@ -1410,12 +1412,13 @@ public final String writeRowToString(Object... row) {
row = adjustRowLength(row);
submitRow(row);

return writeRowToString();
return internalWriteRowToString();
} catch (Throwable ex) {
throw throwExceptionAndClose("Error writing row.", row, ex);
}
}


private Object[] adjustRowLength(Object[] row) {
if (outputRow != null) {
fillOutputRow(row);
Expand Down Expand Up @@ -1449,7 +1452,7 @@ public final String commentRowToString(String comment) {
*
* @return a formatted {@code String} containing the comment.
*/
private String writeRowToString() {
private String internalWriteRowToString() {
if (skipEmptyLines && rowAppender.length() == 0) {
return null;
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/univocity/parsers/csv/CsvWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.*;
import java.nio.charset.*;
import java.util.*;

import static org.testng.Assert.*;

Expand Down Expand Up @@ -604,4 +605,22 @@ public void testBitsAreNotDiscardedWhenWriting() {
assertEquals(line, "a,b");
}

@Test
public void testCollectionWriting(){
CsvWriterSettings settings = new CsvWriterSettings();
settings.getFormat().setLineSeparator("\n");
StringWriter out = new StringWriter();

CsvWriter writer = new CsvWriter(out, settings);

List<String> row = new ArrayList<String>();
row.add("value 1");
row.add("value 2");
writer.writeRow(row);

writer.close();

assertEquals(out.toString(), "value 1,value 2\n");
}

}

0 comments on commit dc2fe57

Please sign in to comment.