Author: britter Date: Tue May 3 18:43:33 2016 New Revision: 1742175 URL: http://svn.apache.org/viewvc?rev=1742175&view=rev Log: CSV-179: Add shortcut method for using first record as header to CSVFormat
Modified: commons/proper/csv/trunk/src/changes/changes.xml commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Modified: commons/proper/csv/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/changes/changes.xml?rev=1742175&r1=1742174&r2=1742175&view=diff ============================================================================== --- commons/proper/csv/trunk/src/changes/changes.xml (original) +++ commons/proper/csv/trunk/src/changes/changes.xml Tue May 3 18:43:33 2016 @@ -39,6 +39,7 @@ </properties> <body> <release version="1.3" date="2016-MM-DD" description="Feature and bug fix release"> + <action issue="CSV-179" type="add" dev="britter">Add shortcut method for using first record as header to CSVFormat</action> <action issue="CSV-180" type="add" dev="britter">Add withHeader(Class<? extends Enum>) to CSVFormat</action> <action issue="CSV-167" type="update" dev="sebb" due-to="Rene">Comment line hides next record; update Javadoc to make behaviour clear</action> <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> Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1742175&r1=1742174&r2=1742175&view=diff ============================================================================== --- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original) +++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Tue May 3 18:43:33 2016 @@ -1075,6 +1075,25 @@ public final class CSVFormat implements } /** + * Returns a new {@code CSVFormat} using the first record as header. + * + * <p> + * Calling this method is equivalent to calling: + * </p> + * <pre> + * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord(); + * </pre> + * + * @return A new CSVFormat that is equal to this but using the first record as header. + * @see #withSkipHeaderRecord(boolean) + * @see #withHeader(String...) + * @since 1.3 + */ + public CSVFormat withFirstRecordAsHeader() { + return withHeader().withSkipHeaderRecord(); + } + + /** * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can * either be parsed automatically from the input file with: * Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1742175&r1=1742174&r2=1742175&view=diff ============================================================================== --- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original) +++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Tue May 3 18:43:33 2016 @@ -436,6 +436,13 @@ public class CSVFormatTest { assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator()); } + @Test + public void testWithFirstRecordAsHeader() throws Exception { + final CSVFormat formatWithFirstRecordAsHeader = CSVFormat.DEFAULT.withFirstRecordAsHeader(); + assertTrue(formatWithFirstRecordAsHeader.getSkipHeaderRecord()); + assertTrue(formatWithFirstRecordAsHeader.getHeader().length == 0); + } + public enum Header { Name, Email, Phone }