Skip to content

Commit 53fd86d

Browse files
committed
More constants clean ups.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397556 13f79535-47bb-0310-9956-ffa450edef68
1 parent b5c3342 commit 53fd86d

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.commons.csv;
1919

2020
import static org.apache.commons.csv.Constants.COMMA;
21-
import static org.apache.commons.csv.Constants.CR;
21+
import static org.apache.commons.csv.Constants.CRLF;
2222
import static org.apache.commons.csv.Constants.DOUBLE_QUOTE;
2323
import static org.apache.commons.csv.Constants.ESCAPE;
2424
import static org.apache.commons.csv.Constants.LF;
@@ -36,13 +36,8 @@
3636
*/
3737
public class CSVFormat implements Serializable {
3838

39-
private static final String LF_STR = "" + LF;
40-
4139
private static final long serialVersionUID = 1L;
4240

43-
/** According to RFC 4180, line breaks are delimited by CRLF */
44-
public static final String CRLF = "" + CR + LF;
45-
4641
private final char delimiter;
4742
private final char encapsulator;
4843
private final char commentStart;
@@ -136,7 +131,7 @@ public class CSVFormat implements Serializable {
136131
PRISTINE
137132
.withDelimiter(TAB)
138133
.withEscape(ESCAPE)
139-
.withLineSeparator(LF_STR);
134+
.withLineSeparator(LF);
140135

141136
/**
142137
* Creates a customized CSV format.
@@ -405,6 +400,19 @@ public String getLineSeparator() {
405400
return lineSeparator;
406401
}
407402

403+
/**
404+
* Returns a copy of this format using the specified output line separator.
405+
*
406+
* @param lineSeparator
407+
* the line separator to be used for output.
408+
*
409+
* @return A copy of this format using the specified output line separator
410+
*/
411+
public CSVFormat withLineSeparator(final char lineSeparator) {
412+
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
413+
ignoreEmptyLines, String.valueOf(lineSeparator), header);
414+
}
415+
408416
/**
409417
* Returns a copy of this format using the specified output line separator.
410418
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class Constants {
3939

4040
/** Undefined state for the lookahead char */
4141
static final int UNDEFINED = -2;
42+
43+
/** According to RFC 4180, line breaks are delimited by CRLF */
44+
public static final String CRLF = EMPTY + CR + LF;
45+
4246

4347

4448
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.CRLF;
2021
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertFalse;
2223
import static org.junit.Assert.assertNotNull;
@@ -34,7 +35,7 @@ public class CSVFormatTest {
3435

3536
@Test
3637
public void testImmutalibity() {
37-
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
38+
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
3839

3940
format.withDelimiter('?');
4041
format.withEncapsulator('?');
@@ -48,15 +49,15 @@ public void testImmutalibity() {
4849
assertEquals('!', format.getEncapsulator());
4950
assertEquals('!', format.getCommentStart());
5051
assertEquals('!', format.getEscape());
51-
assertEquals(CSVFormat.CRLF, format.getLineSeparator());
52+
assertEquals(CRLF, format.getLineSeparator());
5253

5354
assertTrue(format.getIgnoreSurroundingSpaces());
5455
assertTrue(format.getIgnoreEmptyLines());
5556
}
5657

5758
@Test
5859
public void testMutators() {
59-
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
60+
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
6061

6162
assertEquals('?', format.withDelimiter('?').getDelimiter());
6263
assertEquals('?', format.withEncapsulator('?').getEncapsulator());

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.CRLF;
21+
import static org.apache.commons.csv.Constants.LF;
2022
import static org.junit.Assert.assertArrayEquals;
2123
import static org.junit.Assert.assertEquals;
2224
import static org.junit.Assert.assertFalse;
@@ -307,7 +309,7 @@ public void testBackslashEscaping() throws IOException {
307309

308310

309311
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('\'').withEscape('/')
310-
.withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF);
312+
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
311313

312314
final CSVParser parser = new CSVParser(code, format);
313315
final List<CSVRecord> records = parser.getRecords();
@@ -337,7 +339,7 @@ public void testBackslashEscaping2() throws IOException {
337339

338340

339341
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEscape('/')
340-
.withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF);
342+
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
341343

342344
final CSVParser parser = new CSVParser(code, format);
343345
final List<CSVRecord> records = parser.getRecords();
@@ -584,7 +586,7 @@ public void testGetHeaderMap() throws Exception {
584586

585587
@Test
586588
public void testGetLineNumberWithLF() throws Exception {
587-
final CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n"));
589+
final CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator(LF));
588590

589591
assertEquals(0, parser.getLineNumber());
590592
assertNotNull(parser.getRecord());
@@ -598,7 +600,7 @@ public void testGetLineNumberWithLF() throws Exception {
598600

599601
@Test
600602
public void testGetLineNumberWithCRLF() throws Exception {
601-
final CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CSVFormat.CRLF));
603+
final CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CRLF));
602604

603605
assertEquals(0, parser.getLineNumber());
604606
assertNotNull(parser.getRecord());

0 commit comments

Comments
 (0)