From 53fd86dfbc75633b9820868c6119a75952da9bde Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Fri, 12 Oct 2012 13:15:30 +0000 Subject: [PATCH] More constants clean ups. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397556 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/csv/CSVFormat.java | 22 +++++++++++++------ .../org/apache/commons/csv/Constants.java | 4 ++++ .../org/apache/commons/csv/CSVFormatTest.java | 7 +++--- .../org/apache/commons/csv/CSVParserTest.java | 10 +++++---- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 46bff7f4..6e9ea054 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -18,7 +18,7 @@ package org.apache.commons.csv; import static org.apache.commons.csv.Constants.COMMA; -import static org.apache.commons.csv.Constants.CR; +import static org.apache.commons.csv.Constants.CRLF; import static org.apache.commons.csv.Constants.DOUBLE_QUOTE; import static org.apache.commons.csv.Constants.ESCAPE; import static org.apache.commons.csv.Constants.LF; @@ -36,13 +36,8 @@ import java.io.StringWriter; */ public class CSVFormat implements Serializable { - private static final String LF_STR = "" + LF; - private static final long serialVersionUID = 1L; - /** According to RFC 4180, line breaks are delimited by CRLF */ - public static final String CRLF = "" + CR + LF; - private final char delimiter; private final char encapsulator; private final char commentStart; @@ -136,7 +131,7 @@ public class CSVFormat implements Serializable { PRISTINE .withDelimiter(TAB) .withEscape(ESCAPE) - .withLineSeparator(LF_STR); + .withLineSeparator(LF); /** * Creates a customized CSV format. @@ -405,6 +400,19 @@ public class CSVFormat implements Serializable { return lineSeparator; } + /** + * Returns a copy of this format using the specified output line separator. + * + * @param lineSeparator + * the line separator to be used for output. + * + * @return A copy of this format using the specified output line separator + */ + public CSVFormat withLineSeparator(final char lineSeparator) { + return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces, + ignoreEmptyLines, String.valueOf(lineSeparator), header); + } + /** * Returns a copy of this format using the specified output line separator. * diff --git a/src/main/java/org/apache/commons/csv/Constants.java b/src/main/java/org/apache/commons/csv/Constants.java index 0024f468..9ff093f5 100644 --- a/src/main/java/org/apache/commons/csv/Constants.java +++ b/src/main/java/org/apache/commons/csv/Constants.java @@ -39,6 +39,10 @@ class Constants { /** Undefined state for the lookahead char */ static final int UNDEFINED = -2; + + /** According to RFC 4180, line breaks are delimited by CRLF */ + public static final String CRLF = EMPTY + CR + LF; + } diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 87531472..6ef75b64 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -17,6 +17,7 @@ package org.apache.commons.csv; +import static org.apache.commons.csv.Constants.CRLF; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -34,7 +35,7 @@ public class CSVFormatTest { @Test public void testImmutalibity() { - final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null); + final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null); format.withDelimiter('?'); format.withEncapsulator('?'); @@ -48,7 +49,7 @@ public class CSVFormatTest { assertEquals('!', format.getEncapsulator()); assertEquals('!', format.getCommentStart()); assertEquals('!', format.getEscape()); - assertEquals(CSVFormat.CRLF, format.getLineSeparator()); + assertEquals(CRLF, format.getLineSeparator()); assertTrue(format.getIgnoreSurroundingSpaces()); assertTrue(format.getIgnoreEmptyLines()); @@ -56,7 +57,7 @@ public class CSVFormatTest { @Test public void testMutators() { - final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null); + final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null); assertEquals('?', format.withDelimiter('?').getDelimiter()); assertEquals('?', format.withEncapsulator('?').getEncapsulator()); diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index c6a4d3c7..5013bc56 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -17,6 +17,8 @@ package org.apache.commons.csv; +import static org.apache.commons.csv.Constants.CRLF; +import static org.apache.commons.csv.Constants.LF; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -307,7 +309,7 @@ public class CSVParserTest { final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('\'').withEscape('/') - .withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF); + .withIgnoreEmptyLines(true).withLineSeparator(CRLF); final CSVParser parser = new CSVParser(code, format); final List records = parser.getRecords(); @@ -337,7 +339,7 @@ public class CSVParserTest { final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEscape('/') - .withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF); + .withIgnoreEmptyLines(true).withLineSeparator(CRLF); final CSVParser parser = new CSVParser(code, format); final List records = parser.getRecords(); @@ -584,7 +586,7 @@ public class CSVParserTest { @Test public void testGetLineNumberWithLF() throws Exception { - final CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n")); + final CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator(LF)); assertEquals(0, parser.getLineNumber()); assertNotNull(parser.getRecord()); @@ -598,7 +600,7 @@ public class CSVParserTest { @Test public void testGetLineNumberWithCRLF() throws Exception { - final CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CSVFormat.CRLF)); + final CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CRLF)); assertEquals(0, parser.getLineNumber()); assertNotNull(parser.getRecord());