Add ctor to create simplest possible CSV parser

Does not make sense to allow delim = EOL in ctor but disable it in withDelimiter()

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398556 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-10-15 22:50:00 +00:00
parent 91058bb95f
commit cbce4da24b
2 changed files with 30 additions and 4 deletions

View File

@ -126,11 +126,22 @@ public class CSVFormat implements Serializable {
.withEscape(ESCAPE)
.withLineSeparator(LF);
/**
* Creates a basic CSV format.
*
* @param delimiter
* the char used for value separation, must not be a line break character
* @throws IllegalArgumentException if the delimiter is a line break character
*/
public CSVFormat(char delimiter){
this(delimiter, null, null, null, null, false, false, null, null);
}
/**
* Creates a customized CSV format.
*
* @param delimiter
* the char used for value separation
* the char used for value separation, must not be a line break character
* @param quoteChar
* the char used as value encapsulation marker
* @param quotePolicy
@ -147,10 +158,14 @@ public class CSVFormat implements Serializable {
* the line separator to use for output
* @param header
* the header
* @throws IllegalArgumentException if the delimiter is a line break character
*/
public CSVFormat(final char delimiter, final Character quoteChar, final Quote quotePolicy, final Character commentStart, final Character escape, final
boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator,
final String[] header) {
if (isLineBreak(delimiter)) {
throw new IllegalArgumentException("The delimiter cannot be a line break");
}
this.delimiter = delimiter;
this.quoteChar = quoteChar;
this.quotePolicy = quotePolicy;
@ -238,9 +253,6 @@ public class CSVFormat implements Serializable {
* thrown if the specified character is a line break
*/
public CSVFormat withDelimiter(final char delimiter) {
if (isLineBreak(delimiter)) {
throw new IllegalArgumentException("The delimiter cannot be a line break");
}
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
}

View File

@ -89,6 +89,20 @@ public class CSVFormatTest {
@Test
public void testValidation() {
try {
new CSVFormat('\n');
fail();
} catch (final IllegalArgumentException e) {
// expected
}
try {
new CSVFormat('\r');
fail();
} catch (final IllegalArgumentException e) {
// expected
}
final CSVFormat format = CSVFormat.DEFAULT;
try {