Skip to content

Commit 9cb66ca

Browse files
committed
Reduced the constants in CSVFormat for disabling the features to only one
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1200061 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4a48180 commit 9cb66ca

4 files changed

Lines changed: 32 additions & 23 deletions

File tree

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,31 @@ public class CSVFormat implements Cloneable, Serializable {
2828

2929
private char delimiter = ',';
3030
private char encapsulator = '"';
31-
private char commentStart = COMMENTS_DISABLED;
32-
private char escape = ESCAPE_DISABLED;
31+
private char commentStart = DISABLED;
32+
private char escape = DISABLED;
3333
private boolean leadingSpacesIgnored = true;
3434
private boolean trailingSpacesIgnored = true;
3535
private boolean unicodeEscapesInterpreted = false;
3636
private boolean emptyLinesIgnored = true;
3737
private String lineSeparator = "\n";
3838

39-
// -2 is used to signal disabled, because it won't be confused with
40-
// an EOF signal (-1), and because \ufffe in UTF-16 would be
41-
// encoded as two chars (using surrogates) and thus there should never
42-
// be a collision with a real text char.
43-
public static final char COMMENTS_DISABLED = (char) -2;
44-
public static final char ESCAPE_DISABLED = (char) -2;
45-
public static final char ENCAPSULATOR_DISABLED = (char) -2;
39+
40+
/**
41+
* Constant char to be used for disabling comments, escapes and encapsulation.
42+
* The value -2 is used because it won't be confused with an EOF signal (-1),
43+
* and because the unicode value FFFE would be encoded as two chars (using surrogates)
44+
* and thus there should never be a collision with a real text char.
45+
*/
46+
public static final char DISABLED = '\ufffe';
4647

4748
/** Standard comma separated format. */
48-
public static final CSVFormat DEFAULT = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true);
49-
49+
public static final CSVFormat DEFAULT = new CSVFormat(',', '"', DISABLED, DISABLED, true, true, false, true);
50+
5051
/** Excel file format (using a comma as the value delimiter). */
51-
public static final CSVFormat EXCEL = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false);
52-
52+
public static final CSVFormat EXCEL = new CSVFormat(',', '"', DISABLED, DISABLED, false, false, false, false);
53+
5354
/** Tabulation delimited format. */
54-
public static final CSVFormat TDF = new CSVFormat('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true);
55+
public static final CSVFormat TDF = new CSVFormat('\t', '"', DISABLED, DISABLED, true, true, false, true);
5556

5657

5758
/**
@@ -61,7 +62,7 @@ public CSVFormat() {
6162
}
6263

6364
public CSVFormat(char delimiter, char encapsulator, char commentStart) {
64-
this(delimiter, encapsulator, commentStart, ESCAPE_DISABLED, true, true, false, true);
65+
this(delimiter, encapsulator, commentStart, DISABLED, true, true, false, true);
6566
}
6667

6768
/**
@@ -115,6 +116,10 @@ public CSVFormat withEncapsulator(char encapsulator) {
115116
return format;
116117
}
117118

119+
boolean isEncapsulating() {
120+
return this.encapsulator != DISABLED;
121+
}
122+
118123
public char getCommentStart() {
119124
return commentStart;
120125
}
@@ -126,7 +131,7 @@ public CSVFormat withCommentStart(char commentStart) {
126131
}
127132

128133
public boolean isCommentingDisabled() {
129-
return this.commentStart == COMMENTS_DISABLED;
134+
return this.commentStart == DISABLED;
130135
}
131136

132137
public char getEscape() {
@@ -139,6 +144,10 @@ public CSVFormat withEscape(char escape) {
139144
return format;
140145
}
141146

147+
boolean isEscaping() {
148+
return this.escape != DISABLED;
149+
}
150+
142151
public boolean isLeadingSpacesIgnored() {
143152
return leadingSpacesIgnored;
144153
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ public void print(char[] value, int offset, int len, boolean checkForEscape) thr
127127
out.write(value, offset, len);
128128
return;
129129
}
130-
131-
if (format.getEncapsulator() != CSVFormat.ENCAPSULATOR_DISABLED) {
130+
131+
if (format.isEncapsulating()) {
132132
printAndEncapsulate(value, offset, len);
133-
} else if (format.getEscape() != CSVFormat.ESCAPE_DISABLED) {
133+
} else if (format.isEscaping()) {
134134
printAndEscape(value, offset, len);
135135
} else {
136136
printSep();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public void testBackslashEscaping() throws IOException {
438438
};
439439

440440

441-
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.COMMENTS_DISABLED, '/', false, false, true, true);
441+
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true);
442442

443443
CSVParser parser = new CSVParser(new StringReader(code), format);
444444
String[][] tmp = parser.getRecords();
@@ -466,7 +466,7 @@ public void testBackslashEscaping2() throws IOException {
466466
};
467467

468468

469-
CSVFormat format = new CSVFormat(',', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '/', false, false, true, true);
469+
CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true);
470470

471471
CSVParser parser = new CSVParser(new StringReader(code), format);
472472
String[][] tmp = parser.getRecords();
@@ -493,7 +493,7 @@ public void testDefaultFormat() throws IOException {
493493
};
494494

495495
CSVFormat format = CSVFormat.DEFAULT;
496-
assertEquals(CSVFormat.COMMENTS_DISABLED, format.getCommentStart());
496+
assertEquals(CSVFormat.DISABLED, format.getCommentStart());
497497

498498
CSVParser parser = new CSVParser(new StringReader(code), format);
499499
String[][] tmp = parser.getRecords();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void testRandom() throws Exception {
111111
doRandom(iter);
112112

113113
// Format for MySQL
114-
format = new CSVFormat('\t', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '\\', false, false, false, false);
114+
format = new CSVFormat('\t', CSVFormat.DISABLED, CSVFormat.DISABLED, '\\', false, false, false, false);
115115
doRandom(iter);
116116
}
117117

0 commit comments

Comments
 (0)