Skip to content

Commit b6f0655

Browse files
committed
CSV-179: Add shortcut method for using first record as header to CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1742175 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1813f26 commit b6f0655

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</properties>
4040
<body>
4141
<release version="1.3" date="2016-MM-DD" description="Feature and bug fix release">
42+
<action issue="CSV-179" type="add" dev="britter">Add shortcut method for using first record as header to CSVFormat</action>
4243
<action issue="CSV-180" type="add" dev="britter">Add withHeader(Class&lt;? extends Enum&gt;) to CSVFormat</action>
4344
<action issue="CSV-167" type="update" dev="sebb" due-to="Rene">Comment line hides next record; update Javadoc to make behaviour clear</action>
4445
<action issue="CSV-153" type="update" dev="britter" due-to="Wren">CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true</action>

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,25 @@ public CSVFormat withEscape(final Character escape) {
10741074
allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter);
10751075
}
10761076

1077+
/**
1078+
* Returns a new {@code CSVFormat} using the first record as header.
1079+
*
1080+
* <p>
1081+
* Calling this method is equivalent to calling:
1082+
* </p>
1083+
* <pre>
1084+
* CSVFormat format = aFormat.withHeader().withSkipHeaderRecord();
1085+
* </pre>
1086+
*
1087+
* @return A new CSVFormat that is equal to this but using the first record as header.
1088+
* @see #withSkipHeaderRecord(boolean)
1089+
* @see #withHeader(String...)
1090+
* @since 1.3
1091+
*/
1092+
public CSVFormat withFirstRecordAsHeader() {
1093+
return withHeader().withSkipHeaderRecord();
1094+
}
1095+
10771096
/**
10781097
* Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can
10791098
* either be parsed automatically from the input file with:

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ public void testWithRecordSeparatorCRLF() throws Exception {
436436
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
437437
}
438438

439+
@Test
440+
public void testWithFirstRecordAsHeader() throws Exception {
441+
final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader();
442+
assertTrue(formatWithFirstRecordAsHeader.getSkipHeaderRecord());
443+
assertTrue(formatWithFirstRecordAsHeader.getHeader().length == 0);
444+
}
445+
439446
public enum Header {
440447
Name, Email, Phone
441448
}

0 commit comments

Comments
 (0)