[CSV-129] Add CSVFormat#with 0-arg methods matching boolean arg methods

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1621004 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2014-08-28 00:28:02 +00:00
parent c81ad0328e
commit fe514da465
5 changed files with 96 additions and 30 deletions

View File

@ -39,7 +39,8 @@
</properties>
<body>
<release version="1.1" date="2014-mm-dd" description="Feature and bug fix release">
<action issue="CSV-128" type="fix" dev="britter">CSVFormat.EXCEL should ignore empty header names</action>
<action issue="CSV-128" type="fix" dev="ggregory">CSVFormat.EXCEL should ignore empty header names</action>
<action issue="CSV-129" type="add" dev="ggregory">Add CSVFormat#with 0-arg methods matching boolean arg methods</action>
</release>
<release version="1.0" date="2014-08-14" description="First release">
<action issue="CSV-125" type="fix" dev="britter">No longer works with Java 6</action>

View File

@ -216,7 +216,7 @@ public final class CSVFormat implements Serializable {
* Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}.
* </p>
*/
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames(true);
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false).withAllowMissingColumnNames();
/**
* Tab-delimited format.
@ -234,7 +234,7 @@ public final class CSVFormat implements Serializable {
public static final CSVFormat TDF =
DEFAULT
.withDelimiter(TAB)
.withIgnoreSurroundingSpaces(true);
.withIgnoreSurroundingSpaces();
/**
* Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations.
@ -851,6 +851,17 @@ public final class CSVFormat implements Serializable {
allowMissingColumnNames);
}
/**
* Sets the missing column names behavior of the format to {@code true}
*
* @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
* @see #withAllowMissingColumnNames(boolean)
* @since 1.1
*/
public CSVFormat withAllowMissingColumnNames() {
return this.withAllowMissingColumnNames(true);
}
/**
* Sets the missing column names behavior of the format.
*
@ -865,6 +876,17 @@ public final class CSVFormat implements Serializable {
allowMissingColumnNames);
}
/**
* Sets the empty line skipping behavior of the format to {@code true}.
*
* @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
* @since {@link #withIgnoreEmptyLines(boolean)}
* @since 1.1
*/
public CSVFormat withIgnoreEmptyLines() {
return this.withIgnoreEmptyLines(true);
}
/**
* Sets the empty line skipping behavior of the format.
*
@ -879,6 +901,17 @@ public final class CSVFormat implements Serializable {
allowMissingColumnNames);
}
/**
* Sets the trimming behavior of the format to {@code true}.
*
* @return A new CSVFormat that is equal to this but with the specified trimming behavior.
* @see #withIgnoreSurroundingSpaces(boolean)
* @since 1.1
*/
public CSVFormat withIgnoreSurroundingSpaces() {
return this.withIgnoreSurroundingSpaces(true);
}
/**
* Sets the trimming behavior of the format.
*
@ -993,6 +1026,21 @@ public final class CSVFormat implements Serializable {
allowMissingColumnNames);
}
/**
* Sets skipping the header record to {@code true}.
*
* @param skipHeaderRecord
* whether to skip the header record.
*
* @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
* @see #withSkipHeaderRecord(boolean)
* @see #withHeader(String...)
* @since 1.1
*/
public CSVFormat withSkipHeaderRecord() {
return this.withSkipHeaderRecord(true);
}
/**
* Sets whether to skip the header record.
*

View File

@ -123,8 +123,8 @@ public class CSVFormatTest {
.withCommentMarker('#')
.withEscape('+')
.withHeader("One", "Two", "Three")
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
@ -138,8 +138,8 @@ public class CSVFormatTest {
final CSVFormat right = CSVFormat.newFormat('\'')
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
@ -153,7 +153,7 @@ public class CSVFormatTest {
final CSVFormat right = CSVFormat.newFormat('\'')
.withCommentMarker('#')
.withEscape('+')
.withIgnoreSurroundingSpaces(true)
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
@ -187,8 +187,8 @@ public class CSVFormatTest {
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
@ -203,8 +203,8 @@ public class CSVFormatTest {
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL)
.withNullString("null");
@ -220,12 +220,12 @@ public class CSVFormatTest {
.withRecordSeparator(CR)
.withCommentMarker('#')
.withEscape('+')
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(true)
.withIgnoreEmptyLines()
.withIgnoreSurroundingSpaces()
.withQuote('"')
.withQuoteMode(QuoteMode.ALL)
.withNullString("null")
.withSkipHeaderRecord(true);
.withSkipHeaderRecord();
final CSVFormat left = right
.withSkipHeaderRecord(false);
@ -267,7 +267,7 @@ public class CSVFormatTest {
@Test
public void testNullRecordSeparatorCsv106() {
final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord(true).withHeader("H1", "H2");
final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord().withHeader("H1", "H2");
final String formatStr = format.format("A", "B");
assertNotNull(formatStr);
assertFalse(formatStr.endsWith("null"));
@ -377,13 +377,13 @@ public class CSVFormatTest {
@Test
public void testWithIgnoreEmptyLines() throws Exception {
assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines());
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines().getIgnoreEmptyLines());
}
@Test
public void testWithIgnoreSurround() throws Exception {
assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces());
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces().getIgnoreSurroundingSpaces());
}
@Test

View File

@ -100,7 +100,7 @@ public class CSVParserTest {
{ " 8 ", " \"quoted \"\" /\" / string\" " }, { "9", " \n " }, };
final CSVFormat format = CSVFormat.newFormat(',').withQuote('\'').withRecordSeparator(CRLF).withEscape('/')
.withIgnoreEmptyLines(true);
.withIgnoreEmptyLines();
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
@ -127,7 +127,7 @@ public class CSVParserTest {
};
final CSVFormat format = CSVFormat.newFormat(',').withRecordSeparator(CRLF).withEscape('/')
.withIgnoreEmptyLines(true);
.withIgnoreEmptyLines();
final CSVParser parser = CSVParser.parse(code, format);
final List<CSVRecord> records = parser.getRecords();
@ -299,6 +299,23 @@ public class CSVParserTest {
}
}
// @Test
// public void testStartWithEmptyLinesThenHeaders() throws Exception {
// final String[] codes = { "\r\n\r\n\r\nhello,\r\n\r\n\r\n", "hello,\n\n\n", "hello,\"\"\r\n\r\n\r\n", "hello,\"\"\n\n\n" };
// final String[][] res = { { "hello", "" }, { "" }, // Excel format does not ignore empty lines
// { "" } };
// for (final String code : codes) {
// final CSVParser parser = CSVParser.parse(code, CSVFormat.EXCEL);
// final List<CSVRecord> records = parser.getRecords();
// assertEquals(res.length, records.size());
// assertTrue(records.size() > 0);
// for (int i = 0; i < res.length; i++) {
// assertArrayEquals(res[i], records.get(i).values());
// }
// parser.close();
// }
// }
@Test
public void testEndOfFileBehaviorCSV() throws Exception {
final String[] codes = { "hello,\r\n\r\nworld,\r\n", "hello,\r\n\r\nworld,", "hello,\r\n\r\nworld,\"\"\r\n",
@ -433,7 +450,7 @@ public class CSVParserTest {
@Test
public void testGetLine() throws IOException {
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
for (final String[] re : RESULT) {
assertArrayEquals(re, parser.nextRecord().values());
}
@ -507,7 +524,7 @@ public class CSVParserTest {
@Test
public void testGetRecords() throws IOException {
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final CSVParser parser = CSVParser.parse(CSV_INPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
final List<CSVRecord> records = parser.getRecords();
assertEquals(RESULT.length, records.size());
assertTrue(records.size() > 0);
@ -584,13 +601,13 @@ public class CSVParserTest {
@Test
public void testHeadersMissing() throws Exception {
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames(true).parse(in).iterator();
CSVFormat.DEFAULT.withHeader().withAllowMissingColumnNames().parse(in).iterator();
}
@Test
public void testHeaderMissingWithNull() throws Exception {
final Reader in = new StringReader("a,,c,,d\n1,2,3,4\nx,y,z,zz");
CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames(true).parse(in).iterator();
CSVFormat.DEFAULT.withHeader().withNullString("").withAllowMissingColumnNames().parse(in).iterator();
}
@Test
@ -668,7 +685,7 @@ public class CSVParserTest {
@Test
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord(true)
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").withSkipHeaderRecord()
.parse(in).iterator();
CSVRecord record;
@ -841,7 +858,7 @@ public class CSVParserTest {
@Test
public void testSkipSetHeader() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord(true)
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").withSkipHeaderRecord()
.parse(in).iterator();
final CSVRecord record = records.next();
assertEquals("1", record.get("a"));

View File

@ -59,7 +59,7 @@ public class LexerTest {
@Test
public void testSurroundingSpacesAreDeleted() throws IOException {
final String code = "noSpaces, leadingSpaces,trailingSpaces , surroundingSpaces , ,,";
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces"));
@ -72,7 +72,7 @@ public class LexerTest {
@Test
public void testSurroundingTabsAreDeleted() throws IOException {
final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab"));
@ -98,7 +98,7 @@ public class LexerTest {
"\n"+
"\n"+
"\n";
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(true);
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines();
final Lexer parser = getLexer(code, format);
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
@ -241,7 +241,7 @@ public class LexerTest {
* a, " foo " ,b
*/
final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces());
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));