[CSV-68] Use the Builder pattern for CSVFormat.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1410045 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
68352ee98f
commit
0383fd51df
|
@ -84,7 +84,7 @@ public class CSVFormat implements Serializable {
|
|||
* For example for parsing or generating a CSV file on a French system the following format will be used:
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');
|
||||
* CSVFormat fmt = CSVFormat.newBuilder(EXCEL).withDelimiter(';').build();
|
||||
* </pre>
|
||||
* Settings are:
|
||||
* <ul>
|
||||
|
@ -134,6 +134,10 @@ public class CSVFormat implements Serializable {
|
|||
return new CSVFormatBuilder(delimiter);
|
||||
}
|
||||
|
||||
public static CSVFormatBuilder newBuilder(final CSVFormat format) {
|
||||
return new CSVFormatBuilder(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
|
||||
* <ul>
|
||||
|
@ -422,6 +426,20 @@ public class CSVFormat implements Serializable {
|
|||
this.recordSeparator = lineSeparator;
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Creates a CSVFormatBuilder, using the values of the given CSVFormat.
|
||||
*
|
||||
* @param format
|
||||
* The format to use values from
|
||||
*/
|
||||
private CSVFormatBuilder(CSVFormat format) {
|
||||
this(format.delimiter, format.quoteChar, format.quotePolicy,
|
||||
format.commentStart, format.escape,
|
||||
format.ignoreSurroundingSpaces, format.ignoreEmptyLines,
|
||||
format.recordSeparator, format.header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a basic CSVFormatBuilder.
|
||||
|
|
|
@ -17,11 +17,14 @@
|
|||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import static org.apache.commons.csv.CSVFormat.RFC4180;
|
||||
import static org.apache.commons.csv.Constants.CR;
|
||||
import static org.apache.commons.csv.Constants.CRLF;
|
||||
import static org.apache.commons.csv.Constants.LF;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
|
||||
import org.junit.Before;
|
||||
|
@ -152,4 +155,29 @@ public class CSVFormatBuilderTest {
|
|||
public void testIgnoreEmptyLines() {
|
||||
assertFalse(builder.withIgnoreEmptyLines(false).build().getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopiedFormatIsEqualToOriginal() {
|
||||
CSVFormat copyOfRCF4180 = CSVFormat.newBuilder(RFC4180).build();
|
||||
assertEqualFormats(RFC4180, copyOfRCF4180);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopiedFormatWithChanges() {
|
||||
CSVFormat newFormat = CSVFormat.newBuilder(RFC4180).withDelimiter('!').build();
|
||||
assertTrue(newFormat.getDelimiter() != RFC4180.getDelimiter());
|
||||
}
|
||||
|
||||
// FIXME implement equals on CSVFormat to allow use of Assert.assertEquals()
|
||||
private static void assertEqualFormats(CSVFormat expected, CSVFormat acutal) {
|
||||
assertEquals(expected.getCommentStart(), acutal.getCommentStart());
|
||||
assertEquals(expected.getDelimiter(), acutal.getDelimiter());
|
||||
assertEquals(expected.getEscape(), acutal.getEscape());
|
||||
assertArrayEquals(expected.getHeader(), acutal.getHeader());
|
||||
assertEquals(expected.getIgnoreEmptyLines(), acutal.getIgnoreEmptyLines());
|
||||
assertEquals(expected.getIgnoreSurroundingSpaces(), acutal.getIgnoreSurroundingSpaces());
|
||||
assertEquals(expected.getQuoteChar(), acutal.getQuoteChar());
|
||||
assertEquals(expected.getQuotePolicy(), acutal.getQuotePolicy());
|
||||
assertEquals(expected.getRecordSeparator(), acutal.getRecordSeparator());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue