diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 200d5049..dbde654b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -39,6 +39,7 @@
+ * public enum Header { + * Name, Email, Phone + * } + * + * CSVFormat format = aformat.withHeader(Header.class); + *+ *
+ * The header is also used by the {@link CSVPrinter}.. + *
+ * + * @param headerEnum + * the enum defining the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise. + * + * @return A new CSVFormat that is equal to this but with the specified header + * @see #withHeader(String...) + * @see #withSkipHeaderRecord(boolean) + */ + public CSVFormat withHeader(final Class extends Enum>> headerEnum) { + String[] header = null; + if (headerEnum != null) { + Enum>[] enumValues = headerEnum.getEnumConstants(); + header = new String[enumValues.length]; + for (int i = 0; i < enumValues.length; i++) { + header[i] = enumValues[i].name(); + } + } + return withHeader(header); + } + /** * Returns a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will * be printed first, before the headers. This setting is ignored by the parser. diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index d24c4dbf..2bf37534 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -21,12 +21,7 @@ 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.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -376,6 +371,18 @@ public class CSVFormatTest { assertFalse(Arrays.equals(formatWithHeader.getHeader(), header)); } + @Test + public void testWithHeaderEnum() throws Exception { + final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class); + assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader()); + } + + @Test + public void testWithEmptyEnum() throws Exception { + final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class); + Assert.assertTrue(formatWithHeader.getHeader().length == 0); + } + @Test public void testJiraCsv154_withCommentMarker() throws IOException { final String comment = "This is a header comment"; @@ -454,4 +461,11 @@ public class CSVFormatTest { final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF); assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator()); } + + public enum Header { + Name, Email, Phone + } + + public enum EmptyEnum { + } }