Skip to content

Commit 8eb36bd

Browse files
author
Gary Gregory
committed
- 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.
1 parent 030fb8e commit 8eb36bd

6 files changed

Lines changed: 64 additions & 56 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ public static CSVFormat valueOf(final String format) {
723723
private final boolean trim;
724724

725725
private final boolean autoFlush;
726-
726+
727727
private final boolean allowDuplicateHeaderNames;
728728

729729
/**
@@ -1691,7 +1691,7 @@ public CSVFormat withAllowMissingColumnNames() {
16911691
public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
16921692
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
16931693
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
1694-
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush,
1694+
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush,
16951695
allowDuplicateHeaderNames);
16961696
}
16971697

@@ -2257,8 +2257,8 @@ public CSVFormat withTrim(final boolean trim) {
22572257
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush,
22582258
allowDuplicateHeaderNames);
22592259
}
2260-
2261-
public CSVFormat withAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames) {
2260+
2261+
public CSVFormat withAllowDuplicateHeaderNames(final boolean allowDuplicateHeaderNames) {
22622262
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
22632263
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
22642264
skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush,
@@ -2268,7 +2268,7 @@ public CSVFormat withAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames
22682268
public CSVFormat withAllowDuplicateHeaderNames() {
22692269
return withAllowDuplicateHeaderNames(true);
22702270
}
2271-
2271+
22722272
public boolean getAllowDuplicateHeaderNames() {
22732273
return allowDuplicateHeaderNames;
22742274
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import java.util.Map;
4141
import java.util.NoSuchElementException;
4242
import java.util.TreeMap;
43-
import java.util.stream.Collectors;
4443

4544
/**
4645
* Parses CSV files according to the specified format.
@@ -410,7 +409,7 @@ public CSVParser(final Reader reader, final CSVFormat format, final long charact
410409
this.format = format;
411410
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
412411
this.csvRecordIterator = new CSVRecordIterator();
413-
Headers headers = createHeaders();
412+
final Headers headers = createHeaders();
414413
this.headerMap = headers.headerMap;
415414
this.headerNames = headers.headerNames;
416415
this.characterOffset = characterOffset;
@@ -454,18 +453,18 @@ private static final class Headers {
454453
* Header column positions (0-based)
455454
*/
456455
final Map<String, Integer> headerMap;
457-
456+
458457
/**
459458
* Header names in column order
460459
*/
461460
final List<String> headerNames;
462461

463-
Headers(Map<String, Integer> headerMap, List<String> headerNames) {
462+
Headers(final Map<String, Integer> headerMap, final List<String> headerNames) {
464463
this.headerMap = headerMap;
465464
this.headerNames = headerNames;
466465
}
467466
}
468-
467+
469468
/**
470469
* Creates the name to index mapping if the format defines a header.
471470
*
@@ -502,7 +501,7 @@ private Headers createHeaders() throws IOException {
502501
if (!emptyHeader && !this.format.getAllowDuplicateHeaderNames()) {
503502
throw new IllegalArgumentException(
504503
String.format("The header contains a duplicate name: \"%s\" in %s."
505-
+ " If this is valid then use CSVFormat.withAllowDuplicateHeaderNames().",
504+
+ " If this is valid then use CSVFormat.withAllowDuplicateHeaderNames().",
506505
header, Arrays.toString(headerRecord)));
507506
}
508507
if (emptyHeader && !this.format.getAllowMissingColumnNames()) {
@@ -519,7 +518,7 @@ private Headers createHeaders() throws IOException {
519518
}
520519
}
521520
}
522-
}
521+
}
523522
if (headerNames == null) {
524523
headerNames = Collections.emptyList(); //immutable
525524
} else {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
public class CSVFormatTest {
4646

4747
public enum EmptyEnum {
48+
// empty enum.
4849
}
4950

5051
public enum Header {

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

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ private BOMInputStream createBOMInputStream(final String resource) throws IOExce
8484
}
8585

8686
private void parseFully(final CSVParser parser) {
87-
for (final Iterator<CSVRecord> records = parser.iterator(); records.hasNext(); ) {
88-
records.next();
87+
for (final CSVRecord csvRecord : parser) {
88+
Assert.assertNotNull(csvRecord);
8989
}
9090
}
9191

@@ -310,7 +310,7 @@ public void testEmptyFileHeaderParsing() throws Exception {
310310
assertTrue(parser.getHeaderNames().isEmpty());
311311
}
312312
}
313-
313+
314314
@Test
315315
public void testEmptyFile() throws Exception {
316316
try (final CSVParser parser = CSVParser.parse("", CSVFormat.DEFAULT)) {
@@ -423,6 +423,7 @@ public void testExcelFormat2() throws Exception {
423423

424424
/**
425425
* Tests an exported Excel worksheet with a header row and rows that have more columns than the headers
426+
* @throws Exception
426427
*/
427428
@Test
428429
public void testExcelHeaderCountLessThanData() throws Exception {
@@ -529,7 +530,7 @@ public void testGetHeaderNamesReadOnly() throws IOException {
529530
try {
530531
headerNames.add("This is a read-only list.");
531532
fail();
532-
} catch (UnsupportedOperationException e) {
533+
} catch (final UnsupportedOperationException e) {
533534
// Yes.
534535
}
535536
}
@@ -785,54 +786,57 @@ public void testIteratorSequenceBreaking() throws IOException {
785786
final String fiveRows = "1\n2\n3\n4\n5\n";
786787

787788
// Iterator hasNext() shouldn't break sequence
788-
CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows));
789-
int recordNumber = 0;
790-
final Iterator<CSVRecord> iter = parser.iterator();
791-
recordNumber = 0;
792-
while (iter.hasNext()) {
793-
final CSVRecord record = iter.next();
794-
recordNumber++;
795-
assertEquals(String.valueOf(recordNumber), record.get(0));
796-
if (recordNumber >= 2) {
797-
break;
789+
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
790+
int recordNumber = 0;
791+
final Iterator<CSVRecord> iter = parser.iterator();
792+
recordNumber = 0;
793+
while (iter.hasNext()) {
794+
final CSVRecord record = iter.next();
795+
recordNumber++;
796+
assertEquals(String.valueOf(recordNumber), record.get(0));
797+
if (recordNumber >= 2) {
798+
break;
799+
}
800+
}
801+
iter.hasNext();
802+
while (iter.hasNext()) {
803+
final CSVRecord record = iter.next();
804+
recordNumber++;
805+
assertEquals(String.valueOf(recordNumber), record.get(0));
798806
}
799-
}
800-
iter.hasNext();
801-
while (iter.hasNext()) {
802-
final CSVRecord record = iter.next();
803-
recordNumber++;
804-
assertEquals(String.valueOf(recordNumber), record.get(0));
805807
}
806808

807809
// Consecutive enhanced for loops shouldn't break sequence
808-
parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows));
809-
recordNumber = 0;
810-
for (final CSVRecord record : parser) {
811-
recordNumber++;
812-
assertEquals(String.valueOf(recordNumber), record.get(0));
813-
if (recordNumber >= 2) {
814-
break;
810+
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
811+
int recordNumber = 0;
812+
for (final CSVRecord record : parser) {
813+
recordNumber++;
814+
assertEquals(String.valueOf(recordNumber), record.get(0));
815+
if (recordNumber >= 2) {
816+
break;
817+
}
818+
}
819+
for (final CSVRecord record : parser) {
820+
recordNumber++;
821+
assertEquals(String.valueOf(recordNumber), record.get(0));
815822
}
816-
}
817-
for (final CSVRecord record : parser) {
818-
recordNumber++;
819-
assertEquals(String.valueOf(recordNumber), record.get(0));
820823
}
821824

822825
// Consecutive enhanced for loops with hasNext() peeking shouldn't break sequence
823-
parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows));
824-
recordNumber = 0;
825-
for (final CSVRecord record : parser) {
826-
recordNumber++;
827-
assertEquals(String.valueOf(recordNumber), record.get(0));
828-
if (recordNumber >= 2) {
829-
break;
826+
try (CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(fiveRows))) {
827+
int recordNumber = 0;
828+
for (final CSVRecord record : parser) {
829+
recordNumber++;
830+
assertEquals(String.valueOf(recordNumber), record.get(0));
831+
if (recordNumber >= 2) {
832+
break;
833+
}
834+
}
835+
parser.iterator().hasNext();
836+
for (final CSVRecord record : parser) {
837+
recordNumber++;
838+
assertEquals(String.valueOf(recordNumber), record.get(0));
830839
}
831-
}
832-
parser.iterator().hasNext();
833-
for (final CSVRecord record : parser) {
834-
recordNumber++;
835-
assertEquals(String.valueOf(recordNumber), record.get(0));
836840
}
837841
}
838842

@@ -1166,7 +1170,7 @@ public void testTrim() throws Exception {
11661170
assertEquals("3", record.get("Z"));
11671171
Assert.assertEquals(3, record.size());
11681172
}
1169-
1173+
11701174
@Test
11711175
public void testRepeatedHeadersAreReturnedInCSVRecordHeaderNames() throws IOException {
11721176
final Reader in = new StringReader("header1,header2,header1\n1,2,3\n4,5,6");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public void testCloseBackwardCompatibility() throws IOException {
232232
try (final Writer writer = mock(Writer.class)) {
233233
final CSVFormat csvFormat = CSVFormat.DEFAULT;
234234
try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
235+
// empty
235236
}
236237
verify(writer, never()).flush();
237238
verify(writer, times(1)).close();
@@ -242,6 +243,7 @@ public void testCloseWithCsvFormatAutoFlushOff() throws IOException {
242243
try (final Writer writer = mock(Writer.class)) {
243244
final CSVFormat csvFormat = CSVFormat.DEFAULT.withAutoFlush(false);
244245
try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
246+
// empty
245247
}
246248
verify(writer, never()).flush();
247249
verify(writer, times(1)).close();
@@ -254,6 +256,7 @@ public void testCloseWithCsvFormatAutoFlushOn() throws IOException {
254256
try (final Writer writer = mock(Writer.class)) {
255257
final CSVFormat csvFormat = CSVFormat.DEFAULT.withAutoFlush(true);
256258
try (CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat)) {
259+
// empty
257260
}
258261
verify(writer, times(1)).flush();
259262
verify(writer, times(1)).close();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void setUp() {
5050
formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
5151
}
5252

53+
@SuppressWarnings("resource")
5354
private Lexer createLexer(final String input, final CSVFormat format) {
5455
return new Lexer(format, new ExtendedBufferedReader(new StringReader(input)));
5556
}

0 commit comments

Comments
 (0)