From 4def868a8e2042aba1cea2e5509fbb1ce7f8aab2 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Tue, 3 May 2016 18:21:09 +0000 Subject: [PATCH] CSV-180: Add withHeader(Class) to CSVFormat git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1742169 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 1 + .../org/apache/commons/csv/CSVFormat.java | 33 +++++++++++++++++++ .../org/apache/commons/csv/CSVFormatTest.java | 26 +++++++++++---- 3 files changed, 54 insertions(+), 6 deletions(-) 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 @@ + Add withHeader(Class<? extends Enum>) to CSVFormat Comment line hides next record; update Javadoc to make behaviour clear CSVPrinter doesn't skip creation of header record if skipHeaderRecord is set to true Add IgnoreCase option for accessing header names diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index b303aa2b..d6056e73 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1173,6 +1173,39 @@ public final class CSVFormat implements Serializable { skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter); } + /** + * Returns a new {@code CSVFormat} with the header of the format defined by the enum class: + * + *
+     * 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> 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 { + } }