Skip to content

Commit fe514da

Browse files
committed
[CSV-129] Add CSVFormat#with 0-arg methods matching boolean arg methods
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1621004 13f79535-47bb-0310-9956-ffa450edef68
1 parent c81ad03 commit fe514da

5 files changed

Lines changed: 96 additions & 30 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
</properties>
4040
<body>
4141
<release version="1.1" date="2014-mm-dd" description="Feature and bug fix release">
42-
<action issue="CSV-128" type="fix" dev="britter">CSVFormat.EXCEL should ignore empty header names</action>
42+
<action issue="CSV-128" type="fix" dev="ggregory">CSVFormat.EXCEL should ignore empty header names</action>
43+
<action issue="CSV-129" type="add" dev="ggregory">Add CSVFormat#with 0-arg methods matching boolean arg methods</action>
4344
</release>
4445
<release version="1.0" date="2014-08-14" description="First release">
4546
<action issue="CSV-125" type="fix" dev="britter">No longer works with Java 6</action>

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public final class CSVFormat implements Serializable {
216216
* Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}.
217217
* </p>
218218
*/
219-
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(true);
219+
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames();
220220

221221
/**
222222
* Tab-delimited format.
@@ -234,7 +234,7 @@ public final class CSVFormat implements Serializable {
234234
public static final CSVFormat TDF =
235235
DEFAULT
236236
.withDelimiter(TAB)
237-
.withIgnoreSurroundingSpaces(true);
237+
.withIgnoreSurroundingSpaces();
238238

239239
/**
240240
* Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations.
@@ -851,6 +851,17 @@ public CSVFormat withHeader(final String... header) {
851851
allowMissingColumnNames);
852852
}
853853

854+
/**
855+
* Sets the missing column names behavior of the format to {@code true}
856+
*
857+
* @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
858+
* @see #withAllowMissingColumnNames(boolean)
859+
* @since 1.1
860+
*/
861+
public CSVFormat withAllowMissingColumnNames() {
862+
return this.withAllowMissingColumnNames(true);
863+
}
864+
854865
/**
855866
* Sets the missing column names behavior of the format.
856867
*
@@ -865,6 +876,17 @@ public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNam
865876
allowMissingColumnNames);
866877
}
867878

879+
/**
880+
* Sets the empty line skipping behavior of the format to {@code true}.
881+
*
882+
* @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
883+
* @since {@link #withIgnoreEmptyLines(boolean)}
884+
* @since 1.1
885+
*/
886+
public CSVFormat withIgnoreEmptyLines() {
887+
return this.withIgnoreEmptyLines(true);
888+
}
889+
868890
/**
869891
* Sets the empty line skipping behavior of the format.
870892
*
@@ -879,6 +901,17 @@ public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
879901
allowMissingColumnNames);
880902
}
881903

904+
/**
905+
* Sets the trimming behavior of the format to {@code true}.
906+
*
907+
* @return A new CSVFormat that is equal to this but with the specified trimming behavior.
908+
* @see #withIgnoreSurroundingSpaces(boolean)
909+
* @since 1.1
910+
*/
911+
public CSVFormat withIgnoreSurroundingSpaces() {
912+
return this.withIgnoreSurroundingSpaces(true);
913+
}
914+
882915
/**
883916
* Sets the trimming behavior of the format.
884917
*
@@ -993,6 +1026,21 @@ public CSVFormat withRecordSeparator(final String recordSeparator) {
9931026
allowMissingColumnNames);
9941027
}
9951028

1029+
/**
1030+
* Sets skipping the header record to {@code true}.
1031+
*
1032+
* @param skipHeaderRecord
1033+
* whether to skip the header record.
1034+
*
1035+
* @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
1036+
* @see #withSkipHeaderRecord(boolean)
1037+
* @see #withHeader(String...)
1038+
* @since 1.1
1039+
*/
1040+
public CSVFormat withSkipHeaderRecord() {
1041+
return this.withSkipHeaderRecord(true);
1042+
}
1043+
9961044
/**
9971045
* Sets whether to skip the header record.
9981046
*

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public void testEqualsHeader() {
123123
.withCommentMarker('#')
124124
.withEscape('+')
125125
.withHeader("One", "Two", "Three")
126-
.withIgnoreEmptyLines(true)
127-
.withIgnoreSurroundingSpaces(true)
126+
.withIgnoreEmptyLines()
127+
.withIgnoreSurroundingSpaces()
128128
.withQuote('"')
129129
.withQuoteMode(QuoteMode.ALL);
130130
final CSVFormat left = right
@@ -138,8 +138,8 @@ public void testEqualsIgnoreEmptyLines() {
138138
final CSVFormat right = CSVFormat.newFormat('\'')
139139
.withCommentMarker('#')
140140
.withEscape('+')
141-
.withIgnoreEmptyLines(true)
142-
.withIgnoreSurroundingSpaces(true)
141+
.withIgnoreEmptyLines()
142+
.withIgnoreSurroundingSpaces()
143143
.withQuote('"')
144144
.withQuoteMode(QuoteMode.ALL);
145145
final CSVFormat left = right
@@ -153,7 +153,7 @@ public void testEqualsIgnoreSurroundingSpaces() {
153153
final CSVFormat right = CSVFormat.newFormat('\'')
154154
.withCommentMarker('#')
155155
.withEscape('+')
156-
.withIgnoreSurroundingSpaces(true)
156+
.withIgnoreSurroundingSpaces()
157157
.withQuote('"')
158158
.withQuoteMode(QuoteMode.ALL);
159159
final CSVFormat left = right
@@ -187,8 +187,8 @@ public void testEqualsRecordSeparator() {
187187
.withRecordSeparator(CR)
188188
.withCommentMarker('#')
189189
.withEscape('+')
190-
.withIgnoreEmptyLines(true)
191-
.withIgnoreSurroundingSpaces(true)
190+
.withIgnoreEmptyLines()
191+
.withIgnoreSurroundingSpaces()
192192
.withQuote('"')
193193
.withQuoteMode(QuoteMode.ALL);
194194
final CSVFormat left = right
@@ -203,8 +203,8 @@ public void testEqualsNullString() {
203203
.withRecordSeparator(CR)
204204
.withCommentMarker('#')
205205
.withEscape('+')
206-
.withIgnoreEmptyLines(true)
207-
.withIgnoreSurroundingSpaces(true)
206+
.withIgnoreEmptyLines()
207+
.withIgnoreSurroundingSpaces()
208208
.withQuote('"')
209209
.withQuoteMode(QuoteMode.ALL)
210210
.withNullString("null");
@@ -220,12 +220,12 @@ public void testEqualsSkipHeaderRecord() {
220220
.withRecordSeparator(CR)
221221
.withCommentMarker('#')
222222
.withEscape('+')
223-
.withIgnoreEmptyLines(true)
224-
.withIgnoreSurroundingSpaces(true)
223+
.withIgnoreEmptyLines()
224+
.withIgnoreSurroundingSpaces()
225225
.withQuote('"')
226226
.withQuoteMode(QuoteMode.ALL)
227227
.withNullString("null")
228-
.withSkipHeaderRecord(true);
228+
.withSkipHeaderRecord();
229229
final CSVFormat left = right
230230
.withSkipHeaderRecord(false);
231231

@@ -267,7 +267,7 @@ public void testGetHeader() throws Exception {
267267

268268
@Test
269269
public void testNullRecordSeparatorCsv106() {
270-
final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord(true).withHeader("H1", "H2");
270+
final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2");
271271
final String formatStr = format.format("A", "B");
272272
assertNotNull(formatStr);
273273
assertFalse(formatStr.endsWith("null"));
@@ -377,13 +377,13 @@ public void testWithHeader() throws Exception {
377377
@Test
378378
public void testWithIgnoreEmptyLines() throws Exception {
379379
assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
380-
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines());
380+
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
381381
}
382382

383383
@Test
384384
public void testWithIgnoreSurround() throws Exception {
385385
assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
386-
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces());
386+
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
387387
}
388388

389389
@Test

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void testBackslashEscaping() throws IOException {
100100
{ " 8 ", " \"quoted \"\" /\" / string\" " }, { "9", " \n " }, };
101101

102102
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
103-
.withIgnoreEmptyLines(true);
103+
.withIgnoreEmptyLines();
104104

105105
final CSVParser parser = CSVParser.parse(code, format);
106106
final List<CSVRecord> records = parser.getRecords();
@@ -127,7 +127,7 @@ public void testBackslashEscaping2() throws IOException {
127127
};
128128

129129
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
130-
.withIgnoreEmptyLines(true);
130+
.withIgnoreEmptyLines();
131131

132132
final CSVParser parser = CSVParser.parse(code, format);
133133
final List<CSVRecord> records = parser.getRecords();
@@ -299,6 +299,23 @@ public void testEmptyLineBehaviourExcel() throws Exception {
299299
}
300300
}
301301

302+
// @Test
303+
// public void testStartWithEmptyLinesThenHeaders() throws Exception {
304+
// final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", "hello,\"\"\n\n\n" };
305+
// final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
306+
// { "" } };
307+
// for (final String code : codes) {
308+
// final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
309+
// final List<CSVRecord> records = parser.getRecords();
310+
// assertEquals(res.length, records.size());
311+
// assertTrue(records.size() > 0);
312+
// for (int i = 0; i < res.length; i++) {
313+
// assertArrayEquals(res[i], records.get(i).values());
314+
// }
315+
// parser.close();
316+
// }
317+
// }
318+
302319
@Test
303320
public void testEndOfFileBehaviorCSV() throws Exception {
304321
final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n",
@@ -433,7 +450,7 @@ public void testDuplicateHeaders() throws Exception {
433450

434451
@Test
435452
public void testGetLine() throws IOException {
436-
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
453+
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
437454
for (final String[] re : RESULT) {
438455
assertArrayEquals(re, parser.nextRecord().values());
439456
}
@@ -507,7 +524,7 @@ public void testGetRecordNumberWithLF() throws Exception {
507524

508525
@Test
509526
public void testGetRecords() throws IOException {
510-
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
527+
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
511528
final List<CSVRecord> records = parser.getRecords();
512529
assertEquals(RESULT.length, records.size());
513530
assertTrue(records.size() > 0);
@@ -584,13 +601,13 @@ public void testHeadersMissingException() throws Exception {
584601
@Test
585602
public void testHeadersMissing() throws Exception {
586603
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
587-
CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames(true).parse(in).iterator();
604+
CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in).iterator();
588605
}
589606

590607
@Test
591608
public void testHeaderMissingWithNull() throws Exception {
592609
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
593-
CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames(true).parse(in).iterator();
610+
CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames().parse(in).iterator();
594611
}
595612

596613
@Test
@@ -668,7 +685,7 @@ public void testLineFeedEndings() throws IOException {
668685
@Test
669686
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
670687
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
671-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord(true)
688+
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord()
672689
.parse(in).iterator();
673690
CSVRecord record;
674691

@@ -841,7 +858,7 @@ public void testSkipAutoHeader() throws Exception {
841858
@Test
842859
public void testSkipSetHeader() throws Exception {
843860
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
844-
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord(true)
861+
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord()
845862
.parse(in).iterator();
846863
final CSVRecord record = records.next();
847864
assertEquals("1", record.get("a"));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private Lexer getLexer(final String input, final CSVFormat format) {
5959
@Test
6060
public void testSurroundingSpacesAreDeleted() throws IOException {
6161
final String code = "noSpaces, leadingSpaces,trailingSpaces , surroundingSpaces , ,,";
62-
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
62+
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
6363
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces"));
6464
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces"));
6565
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces"));
@@ -72,7 +72,7 @@ public void testSurroundingSpacesAreDeleted() throws IOException {
7272
@Test
7373
public void testSurroundingTabsAreDeleted() throws IOException {
7474
final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
75-
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
75+
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
7676
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs"));
7777
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab"));
7878
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab"));
@@ -98,7 +98,7 @@ public void testIgnoreEmptyLines() throws IOException {
9898
"\n"+
9999
"\n"+
100100
"\n";
101-
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(true);
101+
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
102102
final Lexer parser = getLexer(code, format);
103103

104104
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
@@ -241,7 +241,7 @@ public void testNextToken4() throws IOException {
241241
* a, " foo " ,b
242242
*/
243243
final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
244-
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
244+
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
245245
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
246246
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
247247
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));

0 commit comments

Comments
 (0)