Use final.

This commit is contained in:
Gary Gregory 2020-11-20 22:09:00 -05:00
parent 8c9974d0fd
commit 4dc996e315
7 changed files with 18 additions and 18 deletions

View File

@ -875,8 +875,8 @@ public final class CSVFormat implements Serializable {
final StringWriter out = new StringWriter(); final StringWriter out = new StringWriter();
try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) { try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
csvPrinter.printRecord(values); csvPrinter.printRecord(values);
String res = out.toString(); final String res = out.toString();
int len = recordSeparator != null ? res.length() - recordSeparator.length() : res.length(); final int len = recordSeparator != null ? res.length() - recordSeparator.length() : res.length();
return res.substring(0, len); return res.substring(0, len);
} catch (final IOException e) { } catch (final IOException e) {
// should not happen because a StringWriter does not do IO. // should not happen because a StringWriter does not do IO.

View File

@ -93,7 +93,7 @@ public class CSVFileParserTest {
try (final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format)) { try (final CSVParser parser = CSVParser.parse(new File(BASE, split[0]), Charset.defaultCharset(), format)) {
for (final CSVRecord record : parser) { for (final CSVRecord record : parser) {
String parsed = Arrays.toString(record.values()); String parsed = Arrays.toString(record.values());
String comment = record.getComment(); final String comment = record.getComment();
if (checkComments && comment != null) { if (checkComments && comment != null) {
parsed += "#" + comment.replace("\n", "\\n"); parsed += "#" + comment.replace("\n", "\\n");
} }
@ -138,7 +138,7 @@ public class CSVFileParserTest {
try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) { try (final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format)) {
for (final CSVRecord record : parser) { for (final CSVRecord record : parser) {
String parsed = Arrays.toString(record.values()); String parsed = Arrays.toString(record.values());
String comment = record.getComment(); final String comment = record.getComment();
if (checkComments && comment != null) { if (checkComments && comment != null) {
parsed += "#" + comment.replace("\n", "\\n"); parsed += "#" + comment.replace("\n", "\\n");
} }

View File

@ -1216,14 +1216,14 @@ public class CSVFormatTest {
@Test @Test
public void testWithHeaderEnumNull() { public void testWithHeaderEnumNull() {
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
Class<Enum<?>> simpleName = null; final Class<Enum<?>> simpleName = null;
format.withHeader(simpleName); format.withHeader(simpleName);
} }
@Test @Test
public void testWithHeaderResultSetNull() throws SQLException { public void testWithHeaderResultSetNull() throws SQLException {
final CSVFormat format = CSVFormat.DEFAULT; final CSVFormat format = CSVFormat.DEFAULT;
ResultSet resultSet = null; final ResultSet resultSet = null;
format.withHeader(resultSet); format.withHeader(resultSet);
} }
@ -1231,7 +1231,7 @@ public class CSVFormatTest {
public void testPrintWithQuoteModeIsNONE() throws IOException { public void testPrintWithQuoteModeIsNONE() throws IOException {
final Reader in = new StringReader("a,b,c"); final Reader in = new StringReader("a,b,c");
final Appendable out = new StringBuilder(); final Appendable out = new StringBuilder();
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE); final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NONE);
format.print(in, out, true); format.print(in, out, true);
assertEquals("a?,b?,c", out.toString()); assertEquals("a?,b?,c", out.toString());
} }
@ -1240,7 +1240,7 @@ public class CSVFormatTest {
public void testPrintWithQuotes() throws IOException { public void testPrintWithQuotes() throws IOException {
final Reader in = new StringReader("\"a,b,c\r\nx,y,z"); final Reader in = new StringReader("\"a,b,c\r\nx,y,z");
final Appendable out = new StringBuilder(); final Appendable out = new StringBuilder();
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC); final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
format.print(in, out, true); format.print(in, out, true);
assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString()); assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString());
} }
@ -1249,14 +1249,14 @@ public class CSVFormatTest {
public void testPrintWithoutQuotes() throws IOException { public void testPrintWithoutQuotes() throws IOException {
final Reader in = new StringReader(""); final Reader in = new StringReader("");
final Appendable out = new StringBuilder(); final Appendable out = new StringBuilder();
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC); final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
format.print(in, out, true); format.print(in, out, true);
assertEquals("\"\"", out.toString()); assertEquals("\"\"", out.toString());
} }
@Test @Test
public void testTrim() throws IOException { public void testTrim() throws IOException {
CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF); final CSVFormat formatWithTrim = CSVFormat.DEFAULT.withDelimiter(',').withTrim().withQuote(null).withRecordSeparator(CRLF);
CharSequence in = "a,b,c"; CharSequence in = "a,b,c";
final StringBuilder out = new StringBuilder(); final StringBuilder out = new StringBuilder();

View File

@ -1343,7 +1343,7 @@ public class CSVPrinterTest {
final CharArrayWriter charArrayWriter = new CharArrayWriter(0); final CharArrayWriter charArrayWriter = new CharArrayWriter(0);
try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData)); try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData));
CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) { CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) {
for (CSVRecord record : parser) { for (final CSVRecord record : parser) {
csvPrinter.printRecord(record); csvPrinter.printRecord(record);
} }
} }

View File

@ -51,9 +51,9 @@ public class CSVRecordTest {
SECOND("second"), SECOND("second"),
THIRD("third"); THIRD("third");
private String number; private final String number;
EnumHeader(String number) { EnumHeader(final String number) {
this.number = number; this.number = number;
} }

View File

@ -35,7 +35,7 @@ public class JiraCsv149Test {
testJiraCsv149EndWithEolAtEof(true); testJiraCsv149EndWithEolAtEof(true);
} }
private void testJiraCsv149EndWithEolAtEof(boolean eolAtEof) throws IOException { private void testJiraCsv149EndWithEolAtEof(final boolean eolAtEof) throws IOException {
String source = "A,B,C,D" + CR_LF + "a1,b1,c1,d1" + CR_LF + "a2,b2,c2,d2"; String source = "A,B,C,D" + CR_LF + "a1,b1,c1,d1" + CR_LF + "a2,b2,c2,d2";
if (eolAtEof) { if (eolAtEof) {
source += CR_LF; source += CR_LF;
@ -44,7 +44,7 @@ public class JiraCsv149Test {
final CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader().withQuote('"'); final CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader().withQuote('"');
int lineCounter = 2; int lineCounter = 2;
try (final CSVParser parser = new CSVParser(records, format)) { try (final CSVParser parser = new CSVParser(records, format)) {
for (CSVRecord record : parser) { for (final CSVRecord record : parser) {
assertEquals(lineCounter++, parser.getCurrentLineNumber()); assertEquals(lineCounter++, parser.getCurrentLineNumber());
} }
} }

View File

@ -33,12 +33,12 @@ public class JiraCsv211Test {
final String[] values = new String[] { "1", "Jane Doe", "USA", "" }; final String[] values = new String[] { "1", "Jane Doe", "USA", "" };
final CSVFormat printFormat = CSVFormat.DEFAULT.withDelimiter('\t').withHeader("ID", "Name", "Country", "Age"); final CSVFormat printFormat = CSVFormat.DEFAULT.withDelimiter('\t').withHeader("ID", "Name", "Country", "Age");
String formatted = printFormat.format(values); final String formatted = printFormat.format(values);
assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted); assertEquals("ID\tName\tCountry\tAge\r\n1\tJane Doe\tUSA\t", formatted);
final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader(); final CSVFormat parseFormat = CSVFormat.DEFAULT.withDelimiter('\t').withFirstRecordAsHeader();
CSVParser parser = parseFormat.parse(new StringReader(formatted)); final CSVParser parser = parseFormat.parse(new StringReader(formatted));
for (CSVRecord record : parser) { for (final CSVRecord record : parser) {
assertEquals("1", record.get(0)); assertEquals("1", record.get(0));
assertEquals("Jane Doe", record.get(1)); assertEquals("Jane Doe", record.get(1));
assertEquals("USA", record.get(2)); assertEquals("USA", record.get(2));