Skip to content

Commit 6fce181

Browse files
committed
Refactor duplicate code
Normalize Javadoc first word
1 parent 0047fb0 commit 6fce181

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ public CSVFormat getFormat() {
998998
.setIgnoreEmptyLines(false)
999999
.setQuote(null)
10001000
.setRecordSeparator(LF)
1001-
.setNullString("\\N")
1001+
.setNullString(Constants.SQL_NULL_STRING)
10021002
.setQuoteMode(QuoteMode.ALL_NON_NULL)
10031003
.build();
10041004
// @formatter:off
@@ -1036,7 +1036,7 @@ public CSVFormat getFormat() {
10361036
.setEscape(BACKSLASH)
10371037
.setIgnoreEmptyLines(false)
10381038
.setQuote(DOUBLE_QUOTE_CHAR)
1039-
.setNullString("\\N")
1039+
.setNullString(Constants.SQL_NULL_STRING)
10401040
.setTrim(true)
10411041
.setRecordSeparator(System.lineSeparator())
10421042
.setQuoteMode(QuoteMode.MINIMAL)
@@ -1114,7 +1114,7 @@ public CSVFormat getFormat() {
11141114
.setIgnoreEmptyLines(false)
11151115
.setQuote(null)
11161116
.setRecordSeparator(LF)
1117-
.setNullString("\\N")
1117+
.setNullString(Constants.SQL_NULL_STRING)
11181118
.setQuoteMode(QuoteMode.ALL_NON_NULL)
11191119
.build();
11201120
// @formatter:off
@@ -1217,6 +1217,14 @@ private static boolean isLineBreak(final Character c) {
12171217
return c != null && isLineBreak(c.charValue());
12181218
}
12191219

1220+
private static boolean isTrimChar(final char ch) {
1221+
return ch <= SP;
1222+
}
1223+
1224+
private static boolean isTrimChar(final CharSequence charSequence, int pos) {
1225+
return isTrimChar(charSequence.charAt(pos));
1226+
}
1227+
12201228
/**
12211229
* Creates a new CSV format with the specified delimiter.
12221230
*
@@ -1256,10 +1264,10 @@ static CharSequence trim(final CharSequence charSequence) {
12561264
int len = count;
12571265
int pos = 0;
12581266

1259-
while (pos < len && charSequence.charAt(pos) <= SP) {
1267+
while (pos < len && isTrimChar(charSequence, pos)) {
12601268
pos++;
12611269
}
1262-
while (pos < len && charSequence.charAt(len - 1) <= SP) {
1270+
while (pos < len && isTrimChar(charSequence, len - 1)) {
12631271
len--;
12641272
}
12651273
return pos > 0 || len < count ? charSequence.subSequence(pos, len) : charSequence;
@@ -1460,7 +1468,7 @@ public String format(final Object... values) {
14601468
}
14611469

14621470
/**
1463-
* Returns true if and only if duplicate names are allowed in the headers.
1471+
* Gets whether duplicate names are allowed in the headers.
14641472
*
14651473
* @return whether duplicate header names are allowed
14661474
* @since 1.7
@@ -1472,7 +1480,7 @@ public boolean getAllowDuplicateHeaderNames() {
14721480
}
14731481

14741482
/**
1475-
* Specifies whether missing column names are allowed when parsing the header line.
1483+
* Gets whether missing column names are allowed when parsing the header line.
14761484
*
14771485
* @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an {@link IllegalArgumentException}.
14781486
*/
@@ -1481,7 +1489,7 @@ public boolean getAllowMissingColumnNames() {
14811489
}
14821490

14831491
/**
1484-
* Returns whether to flush on close.
1492+
* Gets whether to flush on close.
14851493
*
14861494
* @return whether to flush on close.
14871495
* @since 1.6
@@ -1491,7 +1499,7 @@ public boolean getAutoFlush() {
14911499
}
14921500

14931501
/**
1494-
* Returns the character marking the start of a line comment.
1502+
* Gets the character marking the start of a line comment.
14951503
*
14961504
* @return the comment start marker, may be {@code null}
14971505
*/
@@ -1500,7 +1508,7 @@ public Character getCommentMarker() {
15001508
}
15011509

15021510
/**
1503-
* Returns the first character delimiting the values (typically ';', ',' or '\t').
1511+
* Gets the first character delimiting the values (typically ';', ',' or '\t').
15041512
*
15051513
* @return the first delimiter character.
15061514
* @deprecated Use {@link #getDelimiterString()}.
@@ -1558,7 +1566,7 @@ public String[] getHeaderComments() {
15581566
}
15591567

15601568
/**
1561-
* Specifies whether empty lines between records are ignored when parsing input.
1569+
* Gets whether empty lines between records are ignored when parsing input.
15621570
*
15631571
* @return {@code true} if empty lines between records are ignored, {@code false} if they are turned into empty records.
15641572
*/
@@ -1567,7 +1575,7 @@ public boolean getIgnoreEmptyLines() {
15671575
}
15681576

15691577
/**
1570-
* Specifies whether header names will be accessed ignoring case.
1578+
* Gets whether header names will be accessed ignoring case.
15711579
*
15721580
* @return {@code true} if header names cases are ignored, {@code false} if they are case sensitive.
15731581
* @since 1.3
@@ -1577,7 +1585,7 @@ public boolean getIgnoreHeaderCase() {
15771585
}
15781586

15791587
/**
1580-
* Specifies whether spaces around values are ignored when parsing input.
1588+
* Gets whether spaces around values are ignored when parsing input.
15811589
*
15821590
* @return {@code true} if spaces around values are ignored, {@code false} if they are treated as part of the value.
15831591
*/
@@ -1666,7 +1674,7 @@ public int hashCode() {
16661674
}
16671675

16681676
/**
1669-
* Specifies whether comments are supported by this format.
1677+
* Tests whether comments are supported by this format.
16701678
*
16711679
* Note that the comment introducer character is only recognized at the start of a line.
16721680
*
@@ -1677,7 +1685,7 @@ public boolean isCommentMarkerSet() {
16771685
}
16781686

16791687
/**
1680-
* Matches whether the next characters constitute a delimiter
1688+
* Tests whether the next characters constitute a delimiter
16811689
*
16821690
* @param ch
16831691
* the current char
@@ -2088,7 +2096,7 @@ private void printWithQuotes(final Object object, final CharSequence charSeq, fi
20882096
c = charSeq.charAt(pos);
20892097
// Some other chars at the end caused the parser to fail, so for now
20902098
// encapsulate if we end in anything less than ' '
2091-
if (c <= SP) {
2099+
if (isTrimChar(c)) {
20922100
quote = true;
20932101
}
20942102
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ final class Constants {
4242

4343
static final String EMPTY = "";
4444

45+
static final String[] EMPTY_STRING_ARRAY = {};
46+
4547
/** The end of stream symbol */
4648
static final int END_OF_STREAM = -1;
4749

@@ -71,6 +73,8 @@ final class Constants {
7173

7274
static final char SP = ' ';
7375

76+
static final String SQL_NULL_STRING = "\\N";
77+
7478
static final char TAB = '\t';
7579

7680
/** Undefined state for the lookahead char */
@@ -79,8 +83,6 @@ final class Constants {
7983
/** ASCII unit separator */
8084
static final char US = 31;
8185

82-
static final String[] EMPTY_STRING_ARRAY = {};
83-
8486
/** No instances. */
8587
private Constants() {
8688
// noop

0 commit comments

Comments
 (0)