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
This commit is contained in:
parent
04b36452dd
commit
4def868a8e
|
@ -39,6 +39,7 @@
|
||||||
</properties>
|
</properties>
|
||||||
<body>
|
<body>
|
||||||
<release version="1.3" date="2016-MM-DD" description="Feature and bug fix release">
|
<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<? 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-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-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>
|
<action issue="CSV-159" type="add" dev="ggregory" due-to="Yamil Medina">Add IgnoreCase option for accessing header names</action>
|
||||||
|
|
|
@ -1173,6 +1173,39 @@ public final class CSVFormat implements Serializable {
|
||||||
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter);
|
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
* 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.
|
* be printed first, before the headers. This setting is ignored by the parser.
|
||||||
|
|
|
@ -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.CR;
|
||||||
import static org.apache.commons.csv.Constants.CRLF;
|
import static org.apache.commons.csv.Constants.CRLF;
|
||||||
import static org.apache.commons.csv.Constants.LF;
|
import static org.apache.commons.csv.Constants.LF;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.*;
|
||||||
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 java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
@ -376,6 +371,18 @@ public class CSVFormatTest {
|
||||||
assertFalse(Arrays.equals(formatWithHeader.getHeader(), header));
|
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
|
@Test
|
||||||
public void testJiraCsv154_withCommentMarker() throws IOException {
|
public void testJiraCsv154_withCommentMarker() throws IOException {
|
||||||
final String comment = "This is a header comment";
|
final String comment = "This is a header comment";
|
||||||
|
@ -454,4 +461,11 @@ public class CSVFormatTest {
|
||||||
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
|
final CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator(CRLF);
|
||||||
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
|
assertEquals(CRLF, formatWithRecordSeparator.getRecordSeparator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum Header {
|
||||||
|
Name, Email, Phone
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum EmptyEnum {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue