Skip to content

Commit 0383fd5

Browse files
committed
[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
1 parent 68352ee commit 0383fd5

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public class CSVFormat implements Serializable {
8484
* For example for parsing or generating a CSV file on a French system the following format will be used:
8585
*
8686
* <pre>
87-
* CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');
87+
* CSVFormat fmt = CSVFormat.newBuilder(EXCEL).withDelimiter(';').build();
8888
* </pre>
8989
* Settings are:
9090
* <ul>
@@ -134,6 +134,10 @@ public static CSVFormatBuilder newBuilder(final char delimiter) {
134134
return new CSVFormatBuilder(delimiter);
135135
}
136136

137+
public static CSVFormatBuilder newBuilder(final CSVFormat format) {
138+
return new CSVFormatBuilder(format);
139+
}
140+
137141
/**
138142
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
139143
* <ul>
@@ -422,6 +426,20 @@ public static class CSVFormatBuilder {
422426
this.recordSeparator = lineSeparator;
423427
this.header = header;
424428
}
429+
430+
/**
431+
*
432+
* Creates a CSVFormatBuilder, using the values of the given CSVFormat.
433+
*
434+
* @param format
435+
* The format to use values from
436+
*/
437+
private CSVFormatBuilder(CSVFormat format) {
438+
this(format.delimiter, format.quoteChar, format.quotePolicy,
439+
format.commentStart, format.escape,
440+
format.ignoreSurroundingSpaces, format.ignoreEmptyLines,
441+
format.recordSeparator, format.header);
442+
}
425443

426444
/**
427445
* Creates a basic CSVFormatBuilder.

src/test/java/org/apache/commons/csv/CSVFormatBuilderTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.CSVFormat.RFC4180;
2021
import static org.apache.commons.csv.Constants.CR;
2122
import static org.apache.commons.csv.Constants.CRLF;
2223
import static org.apache.commons.csv.Constants.LF;
24+
import static org.junit.Assert.assertArrayEquals;
2325
import static org.junit.Assert.assertEquals;
2426
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertTrue;
2528

2629
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
2730
import org.junit.Before;
@@ -152,4 +155,29 @@ public void testIgnoreSurroundingSpaces() {
152155
public void testIgnoreEmptyLines() {
153156
assertFalse(builder.withIgnoreEmptyLines(false).build().getIgnoreEmptyLines());
154157
}
158+
159+
@Test
160+
public void testCopiedFormatIsEqualToOriginal() {
161+
CSVFormat copyOfRCF4180 = CSVFormat.newBuilder(RFC4180).build();
162+
assertEqualFormats(RFC4180, copyOfRCF4180);
163+
}
164+
165+
@Test
166+
public void testCopiedFormatWithChanges() {
167+
CSVFormat newFormat = CSVFormat.newBuilder(RFC4180).withDelimiter('!').build();
168+
assertTrue(newFormat.getDelimiter() != RFC4180.getDelimiter());
169+
}
170+
171+
// FIXME implement equals on CSVFormat to allow use of Assert.assertEquals()
172+
private static void assertEqualFormats(CSVFormat expected, CSVFormat acutal) {
173+
assertEquals(expected.getCommentStart(), acutal.getCommentStart());
174+
assertEquals(expected.getDelimiter(), acutal.getDelimiter());
175+
assertEquals(expected.getEscape(), acutal.getEscape());
176+
assertArrayEquals(expected.getHeader(), acutal.getHeader());
177+
assertEquals(expected.getIgnoreEmptyLines(), acutal.getIgnoreEmptyLines());
178+
assertEquals(expected.getIgnoreSurroundingSpaces(), acutal.getIgnoreSurroundingSpaces());
179+
assertEquals(expected.getQuoteChar(), acutal.getQuoteChar());
180+
assertEquals(expected.getQuotePolicy(), acutal.getQuotePolicy());
181+
assertEquals(expected.getRecordSeparator(), acutal.getRecordSeparator());
182+
}
155183
}

0 commit comments

Comments
 (0)