Skip to content

Commit 4def868

Browse files
committed
CSV-180: Add withHeader(Class<? extends Enum>) to CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1742169 13f79535-47bb-0310-9956-ffa450edef68
1 parent 04b3645 commit 4def868

3 files changed

Lines changed: 54 additions & 6 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-180" type="add" dev="britter">Add withHeader(Class&lt;? extends Enum&gt;) to CSVFormat</action>
4243
<action issue="CSV-167" type="update" dev="sebb" due-to="Rene">Comment line hides next record; update Javadoc to make behaviour clear</action>
4344
<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>
4445
<action issue="CSV-159" type="add" dev="ggregory" due-to="Yamil Medina">Add IgnoreCase option for accessing header names</action>

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,39 @@ public CSVFormat withHeader(final String... header) {
11731173
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter);
11741174
}
11751175

1176+
/**
1177+
* Returns a new {@code CSVFormat} with the header of the format defined by the enum class:
1178+
*
1179+
* <pre>
1180+
* public enum Header {
1181+
* Name, Email, Phone
1182+
* }
1183+
*
1184+
* CSVFormat format = aformat.withHeader(Header.class);
1185+
* </pre>
1186+
* <p>
1187+
* The header is also used by the {@link CSVPrinter}..
1188+
* </p>
1189+
*
1190+
* @param headerEnum
1191+
* the enum defining the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise.
1192+
*
1193+
* @return A new CSVFormat that is equal to this but with the specified header
1194+
* @see #withHeader(String...)
1195+
* @see #withSkipHeaderRecord(boolean)
1196+
*/
1197+
public CSVFormat withHeader(final Class<? extends Enum<?>> headerEnum) {
1198+
String[] header = null;
1199+
if (headerEnum != null) {
1200+
Enum<?>[] enumValues = headerEnum.getEnumConstants();
1201+
header = new String[enumValues.length];
1202+
for (int i = 0; i < enumValues.length; i++) {
1203+
header[i] = enumValues[i].name();
1204+
}
1205+
}
1206+
return withHeader(header);
1207+
}
1208+
11761209
/**
11771210
* Returns a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will
11781211
* be printed first, before the headers. This setting is ignored by the parser.

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@
2121
import static org.apache.commons.csv.Constants.CR;
2222
import static org.apache.commons.csv.Constants.CRLF;
2323
import static org.apache.commons.csv.Constants.LF;
24-
import static org.junit.Assert.assertArrayEquals;
25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertFalse;
27-
import static org.junit.Assert.assertNotNull;
28-
import static org.junit.Assert.assertNotSame;
29-
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.*;
3025

3126
import java.io.ByteArrayInputStream;
3227
import java.io.ByteArrayOutputStream;
@@ -376,6 +371,18 @@ public void testWithHeader() throws Exception {
376371
assertFalse(Arrays.equals(formatWithHeader.getHeader(), header));
377372
}
378373

374+
@Test
375+
public void testWithHeaderEnum() throws Exception {
376+
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(Header.class);
377+
assertArrayEquals(new String[]{ "Name", "Email", "Phone" }, formatWithHeader.getHeader());
378+
}
379+
380+
@Test
381+
public void testWithEmptyEnum() throws Exception {
382+
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(EmptyEnum.class);
383+
Assert.assertTrue(formatWithHeader.getHeader().length == 0);
384+
}
385+
379386
@Test
380387
public void testJiraCsv154_withCommentMarker() throws IOException {
381388
final String comment = "This is a header comment";
@@ -454,4 +461,11 @@ public void testWithRecordSeparatorCRLF() throws Exception {
454461
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
455462
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
456463
}
464+
465+
public enum Header {
466+
Name, Email, Phone
467+
}
468+
469+
public enum EmptyEnum {
470+
}
457471
}

0 commit comments

Comments
 (0)