Skip to content

Commit d1601d3

Browse files
committed
Organize imports, simpler arrays, simpler if/else.
1 parent a0fcf9a commit d1601d3

8 files changed

Lines changed: 28 additions & 32 deletions

File tree

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,22 +1183,18 @@ public void print(final Object value, final Appendable out, final boolean newRec
11831183
// https://issues.apache.org/jira/browse/CSV-203
11841184
if (null == nullString) {
11851185
charSequence = EMPTY;
1186+
} else if (QuoteMode.ALL == quoteMode) {
1187+
charSequence = quotedNullString;
11861188
} else {
1187-
if (QuoteMode.ALL == quoteMode) {
1188-
charSequence = quotedNullString;
1189-
} else {
1190-
charSequence = nullString;
1191-
}
1189+
charSequence = nullString;
11921190
}
1191+
} else if (value instanceof CharSequence) {
1192+
charSequence = (CharSequence) value;
1193+
} else if (value instanceof Reader) {
1194+
print((Reader) value, out, newRecord);
1195+
return;
11931196
} else {
1194-
if (value instanceof CharSequence) {
1195-
charSequence = (CharSequence) value;
1196-
} else if (value instanceof Reader) {
1197-
print((Reader) value, out, newRecord);
1198-
return;
1199-
} else {
1200-
charSequence = value.toString();
1201-
}
1197+
charSequence = value.toString();
12021198
}
12031199
charSequence = getTrim() ? trim(charSequence) : charSequence;
12041200
print(value, charSequence, out, newRecord);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ final class Constants {
7979
/** ASCII unit separator */
8080
static final char US = 31;
8181

82-
static final String[] EMPTY_STRING_ARRAY = new String[0];
82+
static final String[] EMPTY_STRING_ARRAY = {};
8383

8484
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.apache.commons.csv.Constants.CR;
2222
import static org.apache.commons.csv.Constants.CRLF;
2323
import static org.apache.commons.csv.Constants.LF;
24-
2524
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2625
import static org.junit.jupiter.api.Assertions.assertEquals;
2726
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -34,10 +33,10 @@
3433

3534
import java.io.ByteArrayInputStream;
3635
import java.io.ByteArrayOutputStream;
36+
import java.io.IOException;
3737
import java.io.ObjectInputStream;
3838
import java.io.ObjectOutputStream;
3939
import java.io.Reader;
40-
import java.io.IOException;
4140
import java.io.StringReader;
4241
import java.lang.reflect.Method;
4342
import java.lang.reflect.Modifier;
@@ -175,7 +174,7 @@ public void testEqualsHash() throws Exception {
175174
assertNotEquals(name, type, a, b);
176175
} else if ("java.lang.Character".equals(type)){
177176
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {null});
178-
final Object b = method.invoke(CSVFormat.DEFAULT, new Character('d'));
177+
final Object b = method.invoke(CSVFormat.DEFAULT, Character.valueOf('d'));
179178
assertNotEquals(name, type, a, b);
180179
} else if ("java.lang.String".equals(type)){
181180
final Object a = method.invoke(CSVFormat.DEFAULT, new Object[] {null});
@@ -538,7 +537,7 @@ public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
538537
// Cannot assume that callers won't use different Character objects
539538
assertThrows(
540539
IllegalArgumentException.class,
541-
() -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(new Character('!')));
540+
() -> CSVFormat.DEFAULT.withEscape(Character.valueOf('!')).withCommentMarker(Character.valueOf('!')));
542541
}
543542

544543
@Test
@@ -561,7 +560,7 @@ public void testFormatThrowsNullPointerException() {
561560

562561
@Test
563562
public void testGetHeader() {
564-
final String[] header = new String[]{"one", "two", "three"};
563+
final String[] header = {"one", "two", "three"};
565564
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
566565
// getHeader() makes a copy of the header array.
567566
final String[] headerCopy = formatWithHeader.getHeader();
@@ -940,7 +939,7 @@ public void testWithFirstRecordAsHeader() {
940939

941940
@Test
942941
public void testWithHeader() {
943-
final String[] header = new String[]{"one", "two", "three"};
942+
final String[] header = {"one", "two", "three"};
944943
// withHeader() makes a copy of the header array.
945944
final CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
946945
assertArrayEquals(header, formatWithHeader.getHeader());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ public void testPrintReaderWithoutQuoteToWriter() throws IOException {
13601360

13611361
@Test
13621362
public void testPrintRecordsWithCSVRecord() throws IOException {
1363-
final String[] values = new String[] {"A", "B", "C"};
1363+
final String[] values = {"A", "B", "C"};
13641364
final String rowData = StringUtils.join(values, ',');
13651365
final CharArrayWriter charArrayWriter = new CharArrayWriter(0);
13661366
try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
import static org.apache.commons.csv.TokenMatchers.hasContent;
3030
import static org.apache.commons.csv.TokenMatchers.matches;
3131
import static org.hamcrest.MatcherAssert.assertThat;
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
3233
import static org.junit.jupiter.api.Assertions.assertFalse;
3334
import static org.junit.jupiter.api.Assertions.assertThrows;
3435
import static org.junit.jupiter.api.Assertions.assertTrue;
35-
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
3637
import java.io.IOException;
3738
import java.io.StringReader;
3839

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import static org.apache.commons.csv.TokenMatchers.hasType;
2121
import static org.apache.commons.csv.TokenMatchers.isReady;
2222
import static org.apache.commons.csv.TokenMatchers.matches;
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
2324
import static org.junit.jupiter.api.Assertions.assertFalse;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
25-
import static org.junit.jupiter.api.Assertions.assertEquals;
2626

2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;

src/test/java/org/apache/commons/csv/issues/JiraCsv211Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
*/
1717
package org.apache.commons.csv.issues;
1818

19-
import org.apache.commons.csv.CSVFormat;
20-
import org.apache.commons.csv.CSVParser;
21-
import org.apache.commons.csv.CSVRecord;
22-
import org.junit.jupiter.api.Test;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
2320

2421
import java.io.IOException;
2522
import java.io.StringReader;
2623

27-
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import org.apache.commons.csv.CSVFormat;
25+
import org.apache.commons.csv.CSVParser;
26+
import org.apache.commons.csv.CSVRecord;
27+
import org.junit.jupiter.api.Test;
2828

2929
public class JiraCsv211Test {
3030

3131
@Test
3232
public void testJiraCsv211Format() throws IOException {
33-
final String[] values = new String[] { "1", "Jane Doe", "USA", "" };
33+
final String[] values = { "1", "Jane Doe", "USA", "" };
3434

3535
final CSVFormat printFormat = CSVFormat.DEFAULT.withDelimiter('\t').withHeader("ID", "Name", "Country", "Age");
3636
final String formatted = printFormat.format(values);

src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
import static org.junit.jupiter.api.Assertions.assertNull;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
2424

25-
import org.apache.commons.csv.CSVRecord;
26-
import org.junit.jupiter.api.Test;
27-
2825
import java.io.IOException;
2926
import java.io.InputStream;
3027
import java.io.ObjectInputStream;
3128

29+
import org.apache.commons.csv.CSVRecord;
30+
import org.junit.jupiter.api.Test;
31+
3232
public class JiraCsv248Test {
3333
/**
3434
* Test deserialisation of a CSVRecord created using version 1.6.

0 commit comments

Comments
 (0)