Skip to content

Commit cbce4da

Browse files
committed
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
1 parent 91058bb commit cbce4da

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,22 @@ public class CSVFormat implements Serializable {
126126
.withEscape(ESCAPE)
127127
.withLineSeparator(LF);
128128

129+
/**
130+
* Creates a basic CSV format.
131+
*
132+
* @param delimiter
133+
* the char used for value separation, must not be a line break character
134+
* @throws IllegalArgumentException if the delimiter is a line break character
135+
*/
136+
public CSVFormat(char delimiter){
137+
this(delimiter, null, null, null, null, false, false, null, null);
138+
}
139+
129140
/**
130141
* Creates a customized CSV format.
131142
*
132143
* @param delimiter
133-
* the char used for value separation
144+
* the char used for value separation, must not be a line break character
134145
* @param quoteChar
135146
* the char used as value encapsulation marker
136147
* @param quotePolicy
@@ -147,10 +158,14 @@ public class CSVFormat implements Serializable {
147158
* the line separator to use for output
148159
* @param header
149160
* the header
161+
* @throws IllegalArgumentException if the delimiter is a line break character
150162
*/
151163
public CSVFormat(final char delimiter, final Character quoteChar, final Quote quotePolicy, final Character commentStart, final Character escape, final
152164
boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator,
153165
final String[] header) {
166+
if (isLineBreak(delimiter)) {
167+
throw new IllegalArgumentException("The delimiter cannot be a line break");
168+
}
154169
this.delimiter = delimiter;
155170
this.quoteChar = quoteChar;
156171
this.quotePolicy = quotePolicy;
@@ -238,9 +253,6 @@ public char getDelimiter() {
238253
* thrown if the specified character is a line break
239254
*/
240255
public CSVFormat withDelimiter(final char delimiter) {
241-
if (isLineBreak(delimiter)) {
242-
throw new IllegalArgumentException("The delimiter cannot be a line break");
243-
}
244256
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
245257
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
246258
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ public void testFormat() {
8989

9090
@Test
9191
public void testValidation() {
92+
try {
93+
new CSVFormat('\n');
94+
fail();
95+
} catch (final IllegalArgumentException e) {
96+
// expected
97+
}
98+
99+
try {
100+
new CSVFormat('\r');
101+
fail();
102+
} catch (final IllegalArgumentException e) {
103+
// expected
104+
}
105+
92106
final CSVFormat format = CSVFormat.DEFAULT;
93107

94108
try {

0 commit comments

Comments
 (0)