- Remove unused import.

- Remove trailing white spaces on all lines.
- Use final.
- Use for-each (in test).
- Use try-with-resources (in test).
- Document empty blocks.
This commit is contained in:
Gary Gregory 2019-05-24 08:21:21 -04:00
parent 030fb8e37c
commit 8eb36bd8a7
6 changed files with 64 additions and 56 deletions

View File

@ -2258,7 +2258,7 @@ public final class CSVFormat implements Serializable {
allowDuplicateHeaderNames);
}
public CSVFormat withAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames) {
public CSVFormat withAllowDuplicateHeaderNames(final boolean allowDuplicateHeaderNames) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush,

View File

@ -40,7 +40,6 @@ import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
* Parses CSV files according to the specified format.
@ -410,7 +409,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
this.format = format;
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
this.csvRecordIterator = new CSVRecordIterator();
Headers headers = createHeaders();
final Headers headers = createHeaders();
this.headerMap = headers.headerMap;
this.headerNames = headers.headerNames;
this.characterOffset = characterOffset;
@ -460,7 +459,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
*/
final List<String> headerNames;
Headers(Map<String, Integer> headerMap, List<String> headerNames) {
Headers(final Map<String, Integer> headerMap, final List<String> headerNames) {
this.headerMap = headerMap;
this.headerNames = headerNames;
}

View File

@ -45,6 +45,7 @@ import org.junit.Test;
public class CSVFormatTest {
public enum EmptyEnum {
// empty enum.
}
public enum Header {

View File

@ -84,8 +84,8 @@ public class CSVParserTest {
}
private void parseFully(final CSVParser parser) {
for (final Iterator<CSVRecord> records = parser.iterator(); records.hasNext(); ) {
records.next();
for (final CSVRecord csvRecord : parser) {
Assert.assertNotNull(csvRecord);
}
}
@ -423,6 +423,7 @@ public class CSVParserTest {
/**
* Tests an exported Excel worksheet with a header row and rows that have more columns than the headers
* @throws Exception
*/
@Test
public void testExcelHeaderCountLessThanData() throws Exception {
@ -529,7 +530,7 @@ public class CSVParserTest {
try {
headerNames.add("This is a read-only list.");
fail();
} catch (UnsupportedOperationException e) {
} catch (final UnsupportedOperationException e) {
// Yes.
}
}
@ -785,7 +786,7 @@ public class CSVParserTest {
final String fiveRows = "1\n2\n3\n4\n5\n";
// Iterator hasNext() shouldn't break sequence
CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows));
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
int recordNumber = 0;
final Iterator<CSVRecord> iter = parser.iterator();
recordNumber = 0;
@ -803,10 +804,11 @@ public class CSVParserTest {
recordNumber++;
assertEquals(String.valueOf(recordNumber), record.get(0));
}
}
// Consecutive enhanced for loops shouldn't break sequence
parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows));
recordNumber = 0;
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
int recordNumber = 0;
for (final CSVRecord record : parser) {
recordNumber++;
assertEquals(String.valueOf(recordNumber), record.get(0));
@ -818,10 +820,11 @@ public class CSVParserTest {
recordNumber++;
assertEquals(String.valueOf(recordNumber), record.get(0));
}
}
// Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence
parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows));
recordNumber = 0;
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
int recordNumber = 0;
for (final CSVRecord record : parser) {
recordNumber++;
assertEquals(String.valueOf(recordNumber), record.get(0));
@ -835,6 +838,7 @@ public class CSVParserTest {
assertEquals(String.valueOf(recordNumber), record.get(0));
}
}
}
@Test
public void testLineFeedEndings() throws IOException {

View File

@ -232,6 +232,7 @@ public class CSVPrinterTest {
try (final Writer writer = mock(Writer.class)) {
final CSVFormat csvFormat = CSVFormat.DEFAULT;
try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
// empty
}
verify(writer, never()).flush();
verify(writer, times(1)).close();
@ -242,6 +243,7 @@ public class CSVPrinterTest {
try (final Writer writer = mock(Writer.class)) {
final CSVFormat csvFormat = CSVFormat.DEFAULT.withAutoFlush(false);
try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
// empty
}
verify(writer, never()).flush();
verify(writer, times(1)).close();
@ -254,6 +256,7 @@ public class CSVPrinterTest {
try (final Writer writer = mock(Writer.class)) {
final CSVFormat csvFormat = CSVFormat.DEFAULT.withAutoFlush(true);
try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
// empty
}
verify(writer, times(1)).flush();
verify(writer, times(1)).close();

View File

@ -50,6 +50,7 @@ public class LexerTest {
formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
}
@SuppressWarnings("resource")
private Lexer createLexer(final String input, final CSVFormat format) {
return new Lexer(format, new ExtendedBufferedReader(new StringReader(input)));
}