Author: britter
Date: Tue May  3 18:21:09 2016
New Revision: 1742169

URL: http://svn.apache.org/viewvc?rev=1742169&view=rev
Log:
CSV-180: Add withHeader(Class<? extends Enum>) 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=1742169&r1=1742168&r2=1742169&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/changes/changes.xml (original)
+++ commons/proper/csv/trunk/src/changes/changes.xml Tue May  3 18:21:09 2016
@@ -39,6 +39,7 @@
   </properties>
   <body>
     <release version="1.3" date="2016-MM-DD" description="Feature and bug fix 
release">
+      <action issue="CSV-180" type="add" dev="britter">Add 
withHeader(Class&lt;? extends Enum&gt;) 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>
       <action issue="CSV-159" type="add" dev="ggregory" due-to="Yamil 
Medina">Add IgnoreCase option for accessing header names</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=1742169&r1=1742168&r2=1742169&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:21:09 2016
@@ -1174,6 +1174,39 @@ public final class CSVFormat implements
     }
 
     /**
+     * Returns a new {@code CSVFormat} with the header of the format defined 
by the enum class:
+     *
+     * <pre>
+     * public enum Header {
+     *     Name, Email, Phone
+     * }
+     *
+     * CSVFormat format = aformat.withHeader(Header.class);
+     * </pre>
+     * <p>
+     * The header is also used by the {@link CSVPrinter}..
+     * </p>
+     *
+     * @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.
      *

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=1742169&r1=1742168&r2=1742169&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:21:09 2016
@@ -21,12 +21,7 @@ import static org.apache.commons.csv.CSV
 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;
@@ -377,6 +372,18 @@ public class CSVFormatTest {
     }
 
     @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";
         final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", 
"H2").withCommentMarker('#').withHeaderComments(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 {
+    }
 }


Reply via email to