Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 56ca585

Browse files
committed
Refactor constants from various classes into a package private Constants class and use static imports.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397541 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9a27bb5 commit 56ca585

8 files changed

Lines changed: 92 additions & 48 deletions

File tree

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.COMMA;
21+
import static org.apache.commons.csv.Constants.CR;
22+
import static org.apache.commons.csv.Constants.DOUBLE_QUOTE;
23+
import static org.apache.commons.csv.Constants.ESCAPE;
24+
import static org.apache.commons.csv.Constants.LF;
25+
import static org.apache.commons.csv.Constants.TAB;
26+
2027
import java.io.IOException;
2128
import java.io.Reader;
2229
import java.io.Serializable;
@@ -29,20 +36,12 @@
2936
*/
3037
public class CSVFormat implements Serializable {
3138

32-
private static final String LF = "\n";
33-
34-
private static final char ESCAPE = '\\';
35-
36-
private static final char TAB = '\t';
37-
38-
private static final char DOUBLE_QUOTE = '"';
39-
40-
private static final char COMMA = ',';
39+
private static final String LF_STR = "" + LF;
4140

4241
private static final long serialVersionUID = 1L;
4342

4443
/** According to RFC 4180, line breaks are delimited by CRLF */
45-
public static final String CRLF = "\r\n";
44+
public static final String CRLF = "" + CR + LF;
4645

4746
private final char delimiter;
4847
private final char encapsulator;
@@ -127,7 +126,7 @@ public class CSVFormat implements Serializable {
127126

128127
/**
129128
* Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and <tt>LOAD DATA INFILE</tt> operations. This is
130-
* a tab-delimited format with a LF character as the line separator. Values are not quoted and special characters
129+
* a tab-delimited format with a LF_STR character as the line separator. Values are not quoted and special characters
131130
* are escaped with '\'.
132131
*
133132
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">
@@ -137,7 +136,7 @@ public class CSVFormat implements Serializable {
137136
PRISTINE
138137
.withDelimiter(TAB)
139138
.withEscape(ESCAPE)
140-
.withLineSeparator(LF);
139+
.withLineSeparator(LF_STR);
141140

142141
/**
143142
* Creates a customized CSV format.

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

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

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.COMMENT;
21+
import static org.apache.commons.csv.Constants.CR;
22+
import static org.apache.commons.csv.Constants.EMPTY;
23+
import static org.apache.commons.csv.Constants.LF;
24+
import static org.apache.commons.csv.Constants.SP;
25+
2026
import java.io.Flushable;
2127
import java.io.IOException;
2228

2329
/**
2430
* Print values as a comma separated list.
2531
*/
2632
public class CSVPrinter {
27-
28-
private static final char COMMENT = '#';
29-
private static final String EMPTY = "";
30-
private static final char SP = ' ';
31-
private static final char CR = '\r';
32-
private static final char LF = '\n';
3333

3434
/** The place that the values get written. */
3535
private final Appendable out;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv;
19+
20+
/**
21+
* Constants for this package.
22+
*/
23+
class Constants {
24+
25+
static final char BELL = '\b';
26+
static final char COMMA = ',';
27+
static final char COMMENT = '#';
28+
static final char CR = '\r';
29+
static final char DOUBLE_QUOTE = '"';
30+
static final char ESCAPE = '\\';
31+
static final char FF = '\f';
32+
static final char LF = '\n';
33+
static final char SP = ' ';
34+
static final char TAB = '\t';
35+
static final String EMPTY = "";
36+
37+
/** The end of stream symbol */
38+
static final int END_OF_STREAM = -1;
39+
40+
/** Undefined state for the lookahead char */
41+
static final int UNDEFINED = -2;
42+
43+
44+
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.CR;
21+
import static org.apache.commons.csv.Constants.END_OF_STREAM;
22+
import static org.apache.commons.csv.Constants.LF;
23+
import static org.apache.commons.csv.Constants.UNDEFINED;
24+
2025
import java.io.BufferedReader;
2126
import java.io.IOException;
2227
import java.io.Reader;
@@ -29,16 +34,6 @@
2934
*/
3035
class ExtendedBufferedReader extends BufferedReader {
3136

32-
private static final char CR = '\r';
33-
34-
private static final char LF = '\n';
35-
36-
/** The end of stream symbol */
37-
static final int END_OF_STREAM = -1;
38-
39-
/** Undefined state for the lookahead char */
40-
static final int UNDEFINED = -2;
41-
4237
/** The last char returned */
4338
private int lastChar = UNDEFINED;
4439

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.BELL;
21+
import static org.apache.commons.csv.Constants.CR;
22+
import static org.apache.commons.csv.Constants.END_OF_STREAM;
23+
import static org.apache.commons.csv.Constants.FF;
24+
import static org.apache.commons.csv.Constants.LF;
25+
import static org.apache.commons.csv.Constants.TAB;
26+
import static org.apache.commons.csv.Constants.UNDEFINED;
27+
2028
import java.io.IOException;
2129

2230
/**
2331
* Abstract lexer class; contains common utility routines shared by lexers
2432
*/
2533
abstract class Lexer {
2634

27-
private static final char FF = '\f';
28-
private static final char BELL = '\b';
29-
private static final char TAB = '\t';
30-
private static final char LF = '\n';
31-
private static final char CR = '\r';
32-
3335
private final boolean isEncapsulating;
3436
private final boolean isEscaping;
3537
private final boolean isCommentEnabled;
@@ -80,7 +82,7 @@ int readEscape() throws IOException {
8082
return BELL;
8183
case 'f':
8284
return FF;
83-
case ExtendedBufferedReader.END_OF_STREAM:
85+
case END_OF_STREAM:
8486
throw new IOException("EOF whilst processing escape sequence");
8587
default:
8688
return c;
@@ -125,14 +127,14 @@ boolean isEndOfLine(int c) throws IOException {
125127
* @return true if the character is at the start of a line.
126128
*/
127129
boolean isStartOfLine(final int c) {
128-
return c == LF || c == CR || c == ExtendedBufferedReader.UNDEFINED;
130+
return c == LF || c == CR || c == UNDEFINED;
129131
}
130132

131133
/**
132134
* @return true if the given character indicates end of file
133135
*/
134136
boolean isEndOfFile(final int c) {
135-
return c == ExtendedBufferedReader.END_OF_STREAM;
137+
return c == END_OF_STREAM;
136138
}
137139

138140
abstract Token nextToken(Token reusableToken) throws IOException;

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

Lines changed: 2 additions & 1 deletion
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.UNDEFINED;
2021
import static org.apache.commons.csv.Token.Type.EOF;
2122
import static org.apache.commons.csv.Token.Type.EORECORD;
2223
import static org.apache.commons.csv.Token.Type.TOKEN;
@@ -60,7 +61,7 @@ Token nextToken(Token tkn) throws IOException {
6061
// empty line detection: eol AND (last char was EOL or beginning)
6162
if (format.getIgnoreEmptyLines()) {
6263
while (eol
63-
&& (lastChar == '\n' || lastChar == '\r' || lastChar == ExtendedBufferedReader.UNDEFINED)
64+
&& (lastChar == '\n' || lastChar == '\r' || lastChar == UNDEFINED)
6465
&& !isEndOfFile(lastChar)) {
6566
// go on char ahead ...
6667
lastChar = c;

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

Lines changed: 2 additions & 1 deletion
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.END_OF_STREAM;
2021
import static org.apache.commons.csv.Token.Type.COMMENT;
2122
import static org.apache.commons.csv.Token.Type.EOF;
2223
import static org.apache.commons.csv.Token.Type.EORECORD;
@@ -74,7 +75,7 @@ private CharType classify(final int intch) {
7475
if (isWhitespace(intch)) { // Must be after EOL check
7576
return CharType.WHITESPACE;
7677
}
77-
if (intch == ExtendedBufferedReader.END_OF_STREAM) {
78+
if (intch == END_OF_STREAM) {
7879
return CharType.EOFCHAR;
7980
}
8081
return CharType.OTHER;

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

Lines changed: 11 additions & 9 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.END_OF_STREAM;
21+
import static org.apache.commons.csv.Constants.UNDEFINED;
2022
import static org.junit.Assert.assertArrayEquals;
2123
import static org.junit.Assert.assertEquals;
2224
import static org.junit.Assert.assertNull;
@@ -30,9 +32,9 @@ public class ExtendedBufferedReaderTest {
3032
@Test
3133
public void testEmptyInput() throws Exception {
3234
final ExtendedBufferedReader br = getBufferedReader("");
33-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
34-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
35-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
35+
assertEquals(END_OF_STREAM, br.read());
36+
assertEquals(END_OF_STREAM, br.lookAhead());
37+
assertEquals(END_OF_STREAM, br.readAgain());
3638
assertNull(br.readLine());
3739
assertEquals(0, br.read(new char[10], 0, 0));
3840
}
@@ -41,7 +43,7 @@ public void testEmptyInput() throws Exception {
4143
public void testReadLookahead1() throws Exception {
4244
final ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
4345
assertEquals('1', br.lookAhead());
44-
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
46+
assertEquals(UNDEFINED, br.readAgain());
4547
assertEquals('1', br.read());
4648
assertEquals('1', br.readAgain());
4749

@@ -79,12 +81,12 @@ public void testReadLookahead1() throws Exception {
7981
assertEquals('\n', br.readAgain());
8082
assertEquals(3, br.getLineNumber());
8183

82-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
84+
assertEquals(END_OF_STREAM, br.lookAhead());
8385
assertEquals('\n', br.readAgain());
84-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
85-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
86-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
87-
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
86+
assertEquals(END_OF_STREAM, br.read());
87+
assertEquals(END_OF_STREAM, br.readAgain());
88+
assertEquals(END_OF_STREAM, br.read());
89+
assertEquals(END_OF_STREAM, br.lookAhead());
8890

8991
}
9092

0 commit comments

Comments
 (0)