Skip to content

Commit f25612f

Browse files
committed
Internal parser ctor refactoring
.
1 parent 4c67ed8 commit f25612f

1 file changed

Lines changed: 18 additions & 35 deletions

File tree

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

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ protected Builder() {
165165
// empty
166166
}
167167

168-
@SuppressWarnings("resource")
169168
@Override
170169
public CSVParser get() throws IOException {
171-
return new CSVParser(getReader(), format != null ? format : CSVFormat.DEFAULT, characterOffset, recordNumber, getCharset(), trackBytes);
170+
return new CSVParser(this);
172171
}
173172

174173
/**
@@ -524,46 +523,30 @@ public CSVParser(final Reader reader, final CSVFormat format) throws IOException
524523
*/
525524
@Deprecated
526525
public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber) throws IOException {
527-
this(reader, format, characterOffset, recordNumber, null, false);
526+
// @formatter:off
527+
this(builder()
528+
.setReader(reader)
529+
.setFormat(Objects.requireNonNull(format, "format")) // requireNonNull for full compatibility
530+
.setCharacterOffset(characterOffset)
531+
.setRecordNumber(recordNumber)
532+
.setCharset((Charset) null).setTrackBytes(false));
533+
// @formatter:off
528534
}
529535

530536
/**
531-
* Constructs a new instance using the given {@link CSVFormat}.
532-
*
533-
* <p>
534-
* If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
535-
* unless you close the {@code reader}.
536-
* </p>
537+
* Constructs a new instance from a builder.
537538
*
538-
* @param reader
539-
* a Reader containing CSV-formatted input. Must not be null.
540-
* @param format
541-
* the CSVFormat used for CSV parsing. Must not be null.
542-
* @param characterOffset
543-
* Lexer offset when the parser does not start parsing at the beginning of the source.
544-
* @param recordNumber
545-
* The next record number to assign.
546-
* @param charset
547-
* The character encoding to be used for the reader when enableByteTracking is true.
548-
* @param trackBytes
549-
* {@code true} to enable byte tracking for the parser; {@code false} to disable it.
550-
* @throws IllegalArgumentException
551-
* If the parameters of the format are inconsistent or if either the reader or format is null.
552-
* @throws IOException
553-
* If there is a problem reading the header or skipping the first record.
554-
* @throws CSVException Thrown on invalid CSV input data.
539+
* @param builder The source builder.
540+
* @throws IOException if an I/O error occurs.
555541
*/
556-
@SuppressWarnings("resource") // reader is managed by lexer.
557-
private CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber, final Charset charset,
558-
final boolean trackBytes) throws IOException {
559-
Objects.requireNonNull(reader, "reader");
560-
Objects.requireNonNull(format, "format");
561-
this.format = format.copy();
562-
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader, charset, trackBytes));
542+
@SuppressWarnings("resource") // Lexer manages ExtendedBufferedReader.
543+
private CSVParser(final Builder builder) throws IOException {
544+
this.format = (builder.format != null ? builder.format : CSVFormat.DEFAULT).copy();
545+
this.lexer = new Lexer(format, new ExtendedBufferedReader(builder.getReader(), builder.getCharset(), builder.trackBytes));
563546
this.csvRecordIterator = new CSVRecordIterator();
564547
this.headers = createHeaders();
565-
this.characterOffset = characterOffset;
566-
this.recordNumber = recordNumber - 1;
548+
this.characterOffset = builder.characterOffset;
549+
this.recordNumber = builder.recordNumber - 1;
567550
}
568551

569552
private void addRecordValue(final boolean lastRecord) {

0 commit comments

Comments
 (0)